Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Storybook

Security covered Under active development 5,287 sites

This module integrates Storybook with Drupal by allowing you to write Storybook stories directly in Twig templates using special Twig tags. It simplifies the process of documenting and showcasing frontend templates by enabling the creation and management of stories within your existing Drupal theme files.

โœจ The integration between Drupal and Storybook โœจ

The Storybook Drupal module enhances the Twig templating language by introducing two new Twig tags: stories and story, so you can write Storybook stories in Twig. With this module, you can easily create and manage Storybook stories directly in your Twig templates, making it a powerful tool for documenting and showcasing your frontend templates.

๐Ÿ“– Read the article about the module


Watch the video tutorial

5 min ๐Ÿ“น The full set up on both apps, Drupal and Storybook


Watch the video tutorial

5 min ๐Ÿ“น Single Directory Components and Storybook.

This module lets you put any arbitrary Twig into your Storybook stories. You don't need Single Directory Components to integrate with Storybook (they work as well, but they are not required).

Creating Stories

Once the Twig Storybook extension is registered, you can start creating stories within your Twig templates. We recommend writing the stories in a file with name file-name.stories.twig.

  • Use the {% stories %} tag to define a group of stories.
  • Use the {% story %} tag to define an individual story.

Here's an example:

{# some/path/in/your/code/base/my-card.stories.twig #}
{% stories my_card with { title: 'Components/Examples/Card' } %}

  {% story default with {
    name: '1. Default',
    args: { cardHeader: 'I am a header!', buttonText: 'Learn more', buttonIconType: 'power' }
  } %}
    {# Write any Twig for the "default" story. The `args` above will be #}
    {# made available as variables for the template ๐Ÿ‘‡ #}
    {% embed '../examples/my-card.html.twig' with { header: cardHeader } %}
      {% block card_body %}
        <p>I am the <em>card</em> contents.</p>
        {# You can use *any* valid Twig here, including SDCs #}
        {{ include('my-theme:my-button', { text: buttonText, iconType: buttonIconType }) }}
      {% endblock %}
    {% endembed %}
  {% endstory %}

{% endstories %}

This will render as:

Drupal setup

In development.services.yml you want to add some configuration for Twig, so you don't need to clear caches so often, and to enable development mode.

You also need to enable CORS, so the Storybook application can talk to your Drupal site. You want this CORS configuration to be in development.services.yml so it does not get changed in your production environment.

Remember

Make sure to grant permission to Render Storybook stories for anonymous users. Keep this permission disabled in production.

drush role:perm:add anonymous 'render storybook stories'

The configuration you want looks like this:

parameters:
  # ...
  # Remember to disable development mode in production!
  storybook.development: true
  cors.config:
    enabled: true
    allowedHeaders: ['*']
    allowedMethods: ['*']
    allowedOrigins: ['*']
    exposedHeaders: false
    maxAge: false
    supportsCredentials: true
services:
  # ...

Disable render cache and twig cache:

drush state:set twig_debug 1
drush state:set twig_cache_disable 1
drush state:set disable_rendered_output_cache_bins 1

Storybook setup

Are you using ddev?

No necessary extra steps are required, but check out this section in the README.md for some optional improvements.

Install Storybook as usual:

# Make use of modern versions of yarn.
yarn set version berry
# Avoid pnp.
echo 'nodeLinker: node-modules' >> .yarnrc.yml
# Install and configure stock Storybook.
yarn dlx sb init --builder webpack5 --type server

Then update .storybook/main.js to scan for JSON stories where your application stores them.

Troubleshooting

If you use a self-signed certificate in your local, for instance using ddev, you will need to accept the browser exception for Storybook to work. Open the Drupal site in your browser, and accept the exception. Then Storybook should work.

Compiling Twig stories into JSON

The Storybook application will does not understand stories in Twig format. It will fail to render them. You need to compile them into a *.stories.json. To do so you can run:

drush storybook:generate-all-stories

If you want to monitor story changes to compile Twig stories into JSON, execute it with watch. Like so:

watch --color drush storybook:generate-all-stories

Are you using macOS?

In this case the watch command may not be installed. Use homebrew to install it.

brew install watch
Drupal 9โ€“11

Ask your assistant about Storybook

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
12
Tracked since
Jan 2024
Latest release
3 months ago
Releases (12 mo)
2
Maintenance
Active

Release Timeline

Releases

Version Type Core PHP Release date
1.0.0-alpha1 Pre-release 9โ€“10 Jan 10, 2024
1.x-dev Dev 9โ€“11 Jan 10, 2024