Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: Video.js (HTML5 Video Player) 10.0.0-alpha1 First alpha version released for module videojs (10.0.0-alpha1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Storybook

4,594 sites Security covered Drupal 9–11
View on drupal.org

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

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
4
Tracked since
Nov 2024
Latest release
1 month ago
Releases (12 mo)
2
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
1.0.4 Stable 9–11 Jul 7, 2026
1.0.3 Stable 9–11 Mar 17, 2026
1.0.2 Stable 9–11 Nov 13, 2024
1.0.1 Stable 9–11 Nov 13, 2024