Certainly! Let's create a Storybook story for your button in a SvelteKit project. We'll go through the following steps:
- Create the Button component in Svelte.
- Set up Storybook in your SvelteKit project (if not already set up).
- Write a Storybook story for the Button component.
- Run Storybook to see your component in action.
Step 1: Create the Button Component in Svelte
First, we'll create a reusable Svelte component for your button. This allows us to use it throughout your project and in Storybook.
Create a new file Button.svelte in your src/lib/components directory:
<!-- src/lib/components/Button.svelte -->
<script>
export let label = 'Button';
export let onClick = null;
</script>
<button
class="inline-block cursor-pointer rounded-md bg-gray-800 px-4 py-3 text-center text-sm font-semibold uppercase text-white transition duration-200 ease-in-out hover:bg-gray-900"
on:click={onClick}
>
{label}
</button>
Explanation:
- We made the button text customizable via the
label prop.
- Added an
onClick prop to handle click events.
- This makes the component reusable and ready for different scenarios in Storybook.
Step 2: Set Up Storybook in Your SvelteKit Project
If you haven't set up Storybook yet, follow these steps. If you already have Storybook installed, skip to Step 3.
Install Storybook:
Run the following command in your project directory:
npx sb init --builder @storybook/builder-vite --type svelte
Install Svelte-specific dependencies:
npm install -D @storybook/addon-svelte-csf
Update main.js Configuration:
Ensure your .storybook/main.js (or main.cjs if you're using CommonJS) looks like this:
// .storybook/main.js
module.exports = {
stories: ['../src/lib/**/*.stories.@(js|jsx|ts|tsx|svelte)'],
addons: ['@storybook/addon-essentials'],
framework: '@storybook/svelte',
core: {
builder: '@storybook/builder-vite',
},
};
Step 3: Write a Storybook Story for the Button Component
Create a new file Button.stories.svelte in the src/lib/components directory:
<!-- src/lib/components/Button.stories.svelte -->
<script>
import { Meta, Story } from '@storybook/addon-svelte-csf';
import Button from './Button.svelte';
export default {
title: 'Components/Button',
component: Button,
argTypes: {
label: { control: 'text' },
},
} as Meta;
const Template: Story = (args) => ({
Component: Button,
props: args,
});
export const Primary = Template.bind({});
Primary.args = {
label: 'Primary Button',
};
export const Secondary = Template.bind({});
Secondary.args = {
label: 'Secondary Button',
};
</script>
Explanation:
- We use
@storybook/addon-svelte-csf for the Component Story Format (CSF).
argTypes allows us to add controls for the label prop.
Template is a function that returns an instance of the component with the given args.
Primary and Secondary are two stories demonstrating different states of the button.
Step 4: Run Storybook
Now, let's see our button in action.
Start Storybook:
npm run storybook
This should start Storybook and open it in your browser at http://localhost:6006. You should see the Button component under the Components category with the Primary and Secondary stories.
Full Code Overview
Button Component (Button.svelte)
<!-- src/lib/components/Button.svelte -->
<script>
export let label = 'Button';
export let onClick = null;
</script>
<button
class="inline-block cursor-pointer rounded-md bg-gray-800 px-4 py-3 text-center text-sm font-semibold uppercase text-white transition duration-200 ease-in-out hover:bg-gray-900"
on:click={onClick}
>
{label}
</button>
Storybook Story (Button.stories.svelte)
<!-- src/lib/components/Button.stories.svelte -->
<script>
import { Meta, Story } from '@storybook/addon-svelte-csf';
import Button from './Button.svelte';
export default {
title: 'Components/Button',
component: Button,
argTypes: {
label: { control: 'text' },
},
} as Meta;
const Template: Story = (args) => ({
Component: Button,
props: args,
});
export const Primary = Template.bind({});
Primary.args = {
label: 'Primary Button',
};
export const Secondary = Template.bind({});
Secondary.args = {
label: 'Secondary Button',
};
</script>
Additional Tips
-
Adding Actions for Events:
To log events like clicks in Storybook, modify the argTypes in your story:
argTypes: {
label: { control: 'text' },
onClick: { action: 'clicked' },
},
-
Styling and Themes:
Ensure your Tailwind CSS setup is included in Storybook. You might need to import your CSS in .storybook/preview.js:
// .storybook/preview.js
import '../src/app.css'; // Adjust the path to your main CSS file
-
Using Controls:
The controls in Storybook allow you to dynamically change the props and see how the component reacts.
Conclusion
You've now created a Svelte component for your button and written a Storybook story to showcase it. This setup allows you to interactively test and document your UI components.
Feel free to customize the component and stories further to suit your needs. Happy coding!