Integration of Varbase with Storybook
Varbase has been integrated with Storybook to provide a listing of stories for Single Directory Components (SDC) components. This integration allows for easier development and testing of Varbase Components.
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 DDEVThe ddev storybook command
Varbase ships a single DDEV command to manage Storybook. Run ddev storybook help to see everything it does (alias: ddev sb).
ddev storybook <command>The ddev storybook command is a convenience wrapper. The underlying yarn scripts still work, so if you have not updated to the new command yet you can keep using the equivalents in the right column.
Command
What it does
yarn equivalent
ddev storybook init
Full first-time setup (same as ddev init-storybook).
ddev init-storybook
ddev storybook enable
Turn on the development local services (CORS + Twig debug) so the storybook.* subdomain can render stories.
—
ddev storybook disable
Turn them off and keep them off across restarts.
—
ddev storybook list
Print the Storybook URLs / domains to open.
—
ddev storybook status
Show module / dev-services / daemon / port / CORS health.
—
ddev storybook stats
Show how many stories are served, grouped by component group.
—
ddev storybook doctor
Diagnose common problems and print the exact command to fix each.
—
ddev storybook gen
Regenerate *.stories.json from Twig.
ddev yarn storybook:gen (or storybook:gen-new)
ddev storybook build
Build a static Storybook into ./storybook.
ddev yarn storybook:build
— (manual dev server)
Run a foreground dev server on port 6006.
ddev yarn storybook:dev / ddev yarn storybook:ddev
— (free port 6006)
Kill a running Storybook process.
ddev yarn storybook:kill
Note: The Storybook dev server runs automatically as a DDEV
web_extra_daemon(seeweb_extra_daemonsin.ddev/config.yaml). You do not need to start it by hand — afterddev storybook initit is already serving on port6006and on the Storybook subdomain. Allyarnscripts are defined inpackage.jsonand can also be run on the host without DDEV (yarn storybook:build,yarn storybook:dev, …).
Initialize Storybook for DDEV
1. Initialize Storybook for Varbase
or
Either command runs the same full first-time setup, which:
Installs Node.js dependencies via
yarn installEnables the
storybookDrupal moduleGrants
render storybook storiespermission to anonymous and authenticated usersCopies
development.local.services.ymltoweb/sites/default/Enables the development services include in
settings.ddev.php/settings.platformsh.php(viaddev storybook enable)Configures the
storybook.<project>.ddev.sitesubdomain (additional_fqdns+ the Apache proxy)Writes
.env.storybookwithSTORYBOOK_SERVER_URLandSTORYBOOK_SERVER_RENDER_URLpointing 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 storybook and init-storybook commands.
Tip: If
initchanged the subdomain or routing, runddev restartonce to apply it.
2. Generate Stories
Generate all stories using either command:
or
Both run the Drush command:
To generate only new stories (without overwriting existing ones):
3. Open Varbase Storybook
The dev server is already running (as a web_extra_daemon). Print the URLs:
Open the subdomain URL in your browser. The :6006 direct URL also works.
To check everything is healthy:
If a story fails to render (for example a CORS error in the browser console), run the doctor — it tells you exactly what to fix:
4. Restarting DDEV
Story rendering keeps working across ddev stop / ddev start / ddev restart. DDEV regenerates settings.ddev.php on every start, so a post-start hook re-applies the development services (CORS) include automatically by running ddev storybook enable --boot.
To turn Storybook's development services off for a while (and keep them off across restarts):
Re-enable them with:
Starting the dev server manually
You normally never need this — the daemon already runs it. To run a fresh foreground dev server (for example to watch its log), use the yarn script:
This binds to 0.0.0.0 so the subdomain proxy can reach it. To free port 6006:
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/nullvalues and converts bare#URI values to empty strings (Drupal SDC validation rejects both)In development, relies on
middleware.jsto proxy assets — no rewriting neededIn production (static
storybook:buildexport), rewrites relative root-relativehref/src/actionattributes 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 ddev storybook gen (or ddev yarn storybook:gen) 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 storybook init 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.configwithenabled: truein production environments. Keep all changes in thedevelopment.local.services.ymlfile.
Include the Development Services File
Add the following to settings.local.php or settings.ddev.php:
Note: With DDEV,
settings.ddev.phpis regenerated on every start, which would drop this line. Theddev storybookcommand handles that for you by re-adding it from apost-starthook — preferddev storybook enableover editingsettings.ddev.phpby hand.
Install Dependencies and Start
Run
yarn installto install dependenciesRun
yarn storybook:gento generate all storiesRun
yarn storybook:devto 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:
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 using either command:
or
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.compoints at thewebdirectory (Varbase site)storybook.my-staging-site.compoints at thestorybookdirectory (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-varbase11x00 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