Storybook for Ember

You may have tried to use our quick start guide to setup your project for Storybook. If you want to set up Storybook manually, this is the guide for you.

This will also help you to understand how Storybook works.

Starter Guide Ember

Storybook has its own Webpack setup and a dev server.

In this guide, we will set up Storybook for your Ember project.

Table of contents

Add @storybook/ember

First of all, you need to add @storybook/ember to your project. To do that, simply run:

  1. ember install @storybook/ember-cli-storybook

If you don’t have package.json in your project, you’ll need to init it first:

  1. npm init

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

In order for your storybook to run properly be sure to be either run ember serve or ember build before running any storybook commands. Running ember serve before storybook will enable live reloading.

  1. {
  2. "scripts": {
  3. "build-storybook": "ember build && build-storybook -p 9001 -s dist",
  4. "storybook": "ember serve & start-storybook -p 9001 -s dist"
  5. }
  6. }

Setup environment

Your environment will be preconfigured using ember-cli-storybook. This will add a preview-head.html, a .env and make sure that your environment is configured to work with live reload.

Create the config file

Storybook can be configured in several different ways.That’s why we need a config directory. We’ve added a -c option to the above NPM script mentioning .storybook as the config directory.

For the basic Storybook configuration file, you don’t need to do much, but simply tell Storybook where to find stories.

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

  1. import { configure } from '@storybook/ember';
  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.

Write your stories

Now you can write some stories inside the ../stories/index.stories.js file, like this:

It is important that you import the hbs function that is provided by a babel plugin in @storybook/ember

  1. import hbs from 'htmlbars-inline-precompile';
  2. export default { title: 'Demo' };
  3. export const heading = () => hbs`<h1>Hello World</h1>`;
  4. export const button = () => {
  5. return {
  6. template: hbs`<button {{action onClick}}>
  7. Hello Button
  8. </button>`,
  9. context: {
  10. onClick: (e) => console.log(e)
  11. }
  12. }
  13. };
  14. export const component = () => {
  15. return {
  16. template: hbs`{{foo-bar
  17. click=onClick
  18. }}`,
  19. context: {
  20. onClick: (e) => console.log(e)
  21. }
  22. }
  23. };

If you are using an older version of ember <= 3.1 please use this story style

  1. import { compile } from 'ember-source/dist/ember-template-compiler';
  2. export default { title: 'Demo' };
  3. export const heading = () => compile(`<h1>Hello World</h1>`);
  4. export const button = () => {
  5. return {
  6. template: compile(`<button {{action onClick}}>
  7. Hello Button
  8. </button>`),
  9. context: {
  10. onClick: (e) => console.log(e)
  11. }
  12. }
  13. };
  14. export const component = () => {
  15. return {
  16. template: compile(`{{foo-bar
  17. click=onClick
  18. }}`),
  19. context: {
  20. onClick: (e) => console.log(e)
  21. }
  22. }
  23. };

A story is either:

  • A single handlebars fragment generated using the hbs function
  • An object that contains template and context that will be bound to the resulting element

In order to get your storybook to get new changes made to the foo-bar or any other components that are defined in your Ember app you must run ember serve as a sidecar for the build files to get generated.

Run your Storybook

Now everything is ready. Simply run your storybook with:

  1. npm run storybook

Now you can change components and write stories whenever you need to.