Storybook for Mithril

Automatic setup

You may have tried to use our quick start guide to setup your project for Storybook.If it failed because it couldn’t detect you’re using mithril, you could try forcing it to use mithril:

  1. npx -p @storybook/cli sb init --type mithril

Manual setup

If you want to set up Storybook manually for your mithril project, this is the guide for you.

Step 1: Add dependencies

Add @storybook/mithril

Add @storybook/mithril to your project. To do that, run:

  1. npm install @storybook/mithril --save-dev

Add mithril, @babel/core and babel-loader

Make sure that you have mithril, @babel/core, and babel-loader in your dependencies as well because we list these as a peer dependencies:

  1. npm install mithril --save
  2. npm install babel-loader @babel/core --save-dev

Step 2: Add a npm script

Then add the following NPM script to your package.json in order to start the storybook later in this guide:

  1. {
  2. "scripts": {
  3. "storybook": "start-storybook"
  4. }
  5. }

Step 3: Create the config file

For a basic Storybook configuration, the only thing you need to do is tell Storybook where to find stories.

To do that, create a file at .storybook/config.js with the following content:

  1. import { configure } from '@storybook/mithril';
  2. configure(require.context('../src', true, /\.stories\.js$/), module);

That will load all the stories underneath your ../src directory that match the pattern *.stories.js. We recommend co-locating your stories with your source files, but you can place them wherever you choose.

Step 4: Write your stories

Now create a ../src/index.stories.js file, and write your first story like this:

  1. /** @jsx m */
  2. import m from 'mithril';
  3. import { Button } from '<your-button>';
  4. export default { title: 'Button' }
  5. export const withText = () => (
  6. <Button>Hello Button</Button>
  7. );
  8. export const withEmoji = () => (
  9. <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>
  10. );

Each story is a single state of your component. In the above case, there are two stories for the demo button component:

  1. Button
  2. ├── With Text
  3. └── With Emoji

Finally: Run your Storybook

Now everything is ready. Run your storybook with:

  1. npm run storybook

Storybook should start, on a random open port in dev-mode.

Now you can develop your components and write stories and see the changes in Storybook immediately since it uses Webpack’s hot module reloading.