Storybook for Riot

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 riot, you could try forcing it to use riot:

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

Manual setup

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

Step 1: Add dependencies

Add @storybook/riot

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

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

Add riot, @babel/core, and babel-loader

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

  1. npm install riot 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/riot';
  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: Storybook TypeScript configuration

@storybook/riot is using ForkTsCheckerWebpackPlugin to boost the build performance.This makes it necessary to create a tsconfig.json file at .storybook/tsconfig.json with the following content:

  1. {
  2. "extends": "../tsconfig.json",
  3. "exclude": [
  4. "../src/test.ts",
  5. "../src/**/*.spec.ts",
  6. "../projects/**/*.spec.ts"
  7. ],
  8. "include": [
  9. "../src/**/*",
  10. "../projects/**/*"
  11. ]
  12. }

Step 5: Write your stories

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

  1. import { tag, mount } from '@storybook/riot';
  2. import SimpleTestRaw from './SimpleTest.txt'; //can be loaded as string if you prefer
  3. import './AnotherTest.tag';
  4. //if you need to import .tag files as text, just use the raw-loader instead of the riot-tag-loader
  5. export default { title: 'My Component' };
  6. // the template is compiled below
  7. export const builtWithTag = () => (
  8. tag('test', '<div>simple test ({ opts.value })</div>', '', '', () => {}) &&
  9. mount('test', { value: 'with a parameter' })
  10. );
  11. // tags[0] will be the parent tag, always
  12. // you can leave out the root tag, if we find out that the new root tag
  13. // is a built-in html tag, it will be wrapped
  14. export const builtAsString = () => ({
  15. tags: ['<test><div>simple test</div></test>']
  16. });
  17. // the component is a string, it will be instantiated without params
  18. // e.g. <SimpletestRaw/>
  19. export const builtFromRawImport = () => SimpleTestRaw;
  20. // the comprehensive form is this one
  21. // list all the possible tags (the root element is in the content)
  22. // then scenario is compiled and executed
  23. // WARN : the tag file root element must have exactly the same name (or else you will see nothing)
  24. export const builtFromTagsAndScenario = () => ({
  25. tags: [{ content: SimpleTestRaw, boundAs: 'mustBeUniquePlease' }],
  26. scenario:
  27. '<SimpleTest test={ "with a parameter" } value={"value is mapped to riotValue"}></SimpleTest>',
  28. });
  29. // the tag is already compiled before running the js
  30. // the tag name 'anothertest' must match exactly the root tag inside the tag file
  31. // must be lower case
  32. export const builtFromThePrecompilation = () => mount('anothertest', {});

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

  1. My Component
  2. ├── Built With Tag
  3. ├── Built As String
  4. ├── Built From Raw Import
  5. ├── Built From Tags And Scenario
  6. └── Built From The Precompilation

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.