Search…
Writing Stories
Writing components in Emulsify follows standard practices. You create your component Twig, .scss and/or .js files in component-specific directories.
To show components in Storybook, however, you will need to create "stories." Storybook is very flexible and extensible, and there are addons that provide additional flexibility. We can't document everything you can do in Storybook, so below you'll find just a few examples of common ways to show your components in the Storybook UI.

Simple annotated example

(From components/01-atoms/buttons/buttons.stories.js with added comments)
1
// Import the Twig file.
2
import button from './button.twig';
3
4
// Import the data file.
5
import buttonData from './button.yml';
6
7
// This defines where the component will be shown in Storybook's navigation.
8
export default { title: 'Atoms/Button' };
9
10
// This will create a story called "Primary" under the above navigation
11
// using your button component and buttonData.
12
export const primary = () => button(buttonData);
13
Copied!

Sharing Data Between Components

(From components/02-molecules/card/cards.stories.js with added comments)
1
import card from './card.twig';
2
3
// Import the data file shared by all cards.
4
import cardData from './card.yml';
5
// Import the data file specific to the 'bg' variation.
6
import cardBgData from './card-bg.yml';
7
8
export default { title: 'Molecules/Cards' };
9
10
// This creates a "Default" card story with the "common" data.
11
export const default = () => card(cardData);
12
13
// This creates a "Card With Background" story which includes the common data from
14
// `card.yml`, and then overrides and/or additional data provided by `card-bg.yml`.
15
export const cardWithBackground = () => card({ ...cardData, ...cardBgData });
16
Copied!

Using JavaScript

(From components/02-molecules/tabs/tabs.stories.js with added comments)
1
import tabs from './tabs.twig';
2
import tabData from './tabs.yml';
3
4
// Importing JavaScript as you normally would in Node.js loads that js in Storybook.
5
// NOTE: This does not load the javascript in Drupal. You will still need to create
6
// a Drupal library and load it where appropriate using Drupal methods.
7
import './tabs';
8
9
export default { title: 'Molecules/Tabs' };
10
11
export const JSTabs = () => tabs(tabData);
12
Copied!

Using Storybook "Controls"

Storybook Controls offers the ability to tweak stories via the Storybook UI for demoing different variations, etc. Here is a simple example below:
1
import React from 'react';
2
3
import button from './button.twig';
4
5
import buttonData from './button.yml';
6
7
export default {
8
title: 'Atoms/Button',
9
argTypes: {
10
// This defines a "Variation" argument that can be used in components.
11
variation: {
12
control: {
13
type: 'select',
14
options: ['primary', 'secondary', 'tertiary'],
15
},
16
},
17
},
18
};
19
20
// We pass "variation" as a destructured object into our component.
21
export const twig = ({ variation }) =>
22
// Finally, we can pass the selected variation as a property of the component.
23
button({ ...buttonData, button_modifiers: [variation] });
24
Copied!
Last modified 2mo ago