githubEdit

Integration of Varbase with Storybook

Varbase has been integrated with Storybookarrow-up-right to provide a listing of stories for Single Directory Components (SDC)arrow-up-right components. This integration allows for easier development and testing of Varbase Componentsarrow-up-right.

Warning: Not for production. Only use for development or staging environments.

Prerequisites

Follow the Varbase installation guide to build and install Varbase with DDEV before initializing Storybook.

Installing Varbase locally with DDEVchevron-right

Initialize Storybook for DDEV

1. Initialize Storybook for Varbase

ddev init-storybook

The ddev init-storybook command is a custom DDEV command that:

  • Installs Node.js dependencies via yarn install

  • Enables the storybook Drupal module

  • Grants render storybook stories permission to anonymous and authenticated users

  • Copies development.local.services.yml to web/sites/default/

  • Adds the development services configuration to settings.ddev.php or settings.platformsh.php

  • Writes .env.storybook with STORYBOOK_SERVER_URL and STORYBOOK_SERVER_RENDER_URL pointing at the active Drupal site URL, used by the Storybook dev server and middleware to reach Drupal

Have a look at the content of the init-storybookarrow-up-right command.

2. Generate Stories

Generate all stories using the following command:

This runs the Drush command:

To generate only new stories (without overwriting existing ones):

3. Start Varbase Storybook in DDEV

This starts Storybook on port 6006. Open your site domain with :6006 to access it.

For DDEV remote access (e.g. when accessing Storybook from another device on the network or from the host browser when DDEV runs in a container), use the DDEV-bound variant which binds to 0.0.0.0:

To kill a running Storybook process and free port 6006:

4. Verify Installation

How Storybook Connects to Drupal

Storybook runs on its own port (6006) but renders SDC components by calling the Drupal site. Two pieces glue this together:

.storybook/middleware.js

An Express middleware that proxies Drupal static assets (CSS, JS, fonts, images) through the Storybook dev server. The Storybook iframe runs at :6006, but Drupal assets are served from a different origin (e.g. :8443); browsers block cross-origin sub-resources loaded via innerHTML because static files have no CORS headers. The middleware routes paths under /themes/, /modules/, /core/, /libraries/, /sites/, and /storybook/ to the Drupal base URL read from process.env.STORYBOOK_SERVER_URL so the browser sees them as same-origin requests.

.storybook/preview.ts fetchStoryHtml

Custom fetch function for @storybook/server that:

  • Cleans up Drupal SDC params before sending — strips undefined/null values and converts bare # URI values to empty strings (Drupal SDC validation rejects both)

  • In development, relies on middleware.js to proxy assets — no rewriting needed

  • In production (static storybook:build export), rewrites relative root-relative href/src/action attributes to absolute Drupal URLs so assets load from the live Drupal server instead of 404-ing on the static host

When Adding or Changing Stories

Run the ddev yarn storybook:gen command whenever stories are added or changed to regenerate all stories.

Manual Setup (Without DDEV Commands)

If you prefer to set up Storybook manually instead of using the ddev init-storybook command:

Enable the Storybook Module

Enable the storybook module through the site interface or with Drush:

Warning: The Storybook module should not be kept running on a production site.

Grant Permissions

Navigate to /admin/people/permissions/module/storybook to grant the Render storybook stories permission, or use Drush:

Warning: Give to trusted roles only; this permission has security implications. Allows a user to access the Twig Storybook endpoint to render a template with stories.

To revoke the permission later:

Configure Development Services

Create or update the development.local.services.yml file in web/sites/default/ with the following content:

Warning: Not recommended to keep cors.config with enabled: true in production environments. Keep all changes in the development.local.services.yml file.

Include the Development Services File

Add the following to settings.local.php or settings.ddev.php:

Install Dependencies and Start

  1. Run yarn install to install dependencies

  2. Run yarn storybook:gen to generate all stories

  3. Run yarn storybook:dev to start Storybook on port 6006

Customizing Varbase Storybook for a Project

Switching Between Themes

To showcase a custom cloned generated theme, uncomment and modify the following line in the .storybook/preview.ts file:

Show Vartheme BS5 Components

By default, the .storybook/main.ts file includes components from Vartheme BS5:

Show Custom Theme Components

To include components from a custom cloned generated theme, uncomment and modify the following lines in the .storybook/main.ts file:

Creating Your Own Themechevron-right

Ensure the path to the custom theme is correct. It should be located in ../web/themes/custom/.

Show Custom Module Components

To include components from a custom module, uncomment and modify the following lines in the .storybook/main.ts file:

Bootstrap Color Modes

Varbase Storybook supports Bootstrap 5.3+ color modes. Use the root attributes addon in the Storybook toolbar to switch between Light and Dark themes, as well as LTR and RTL text direction.

Storybook Build

Build a static version of Storybook for demos, staging, or hosted development environments:

Danger: Not for production environments. Only for development, staging, or demo.

This outputs the built Storybook to the storybook directory. A subdomain can then point to this directory:

  • my-staging-site.com points at the web directory (Varbase site)

  • storybook.my-staging-site.com points at the storybook directory (Storybook)

Run Varbase Storybook on UPSUN

For development, testing, or staging environments on Platform.sh:

Danger: NOT for production environments.

Select the Varbase Template

Use the TEMP development for Vardot/upsun-varbase11x00arrow-up-right from the pre-existing code base template to start a project.

Use Development Services

Add the following to settings.platformsh.php:

After committing and starting the development environment, the Storybook link will be available at:

Last updated