Storybook is an essential tool for building, testing, and documenting UI components. It opens the doors to faster front-end development, team collaboration, and a consistent design language across websites. In this article, we will cover how Storybook works, how to integrate Storybook in Drupal, how to handle components, and how to use Single Directory Component (SDC) in Drupal 10+.
Think of storybook like a digital catalog for your website's building blocks. It's an open-source tool for developing and documenting UI components in isolation. It runs as a standalone app where you can view, interact with, and test components without needing the full application (Drupal in this case). It supports components like buttons, cards, forms, etc. It uses "stories" to support component attributes such as button in "normal", or button in "hover" mode. Storybook supports frameworks like React, Vue, or plain HTML/Twig.
In Drupal, Storybook bridges the gap between frontend design and backend content management. Drupal themes use Twig templates for rendering content, and Storybook can preview these templates independently, which makes building reusable components consistent, easy, and sick!
Storybook isn't native in Drupal, but integrating it will supercharge theme development.
Components (like LEGO bricks) are small, reusable pieces that snap together to form a structure. In a Drupal theme, a card component is simply a reusable box that contains an image, a title, and some text. This component lives in a folder containing YAML metadata, Twig template, CSS/SCSS, JS, or any related asset. This highly modular approach not only separates all our concerns, but ensures that assets are auto loaded whenever the component is invoked.
In Storybook, a component is represented by "story", each story can have multiple variations (e.g card with image / card without image). Components are implemented by:
For pure frontend, skip the module and use Twig.js directly
parameters:
twig.config:
debug: true
cache: false
storybook.development: true
cors.config:
enabled: true
allowedHeaders: ['*']
allowedMethods: ['*']
allowedOrigins: ['*']
exposedHeaders: false
maxAge: false
supportsCredentials: true
import { defineConfig } from "vite";
import yml from '@modyfi/vite-plugin-yaml';
import twig from 'vite-plugin-twig-drupal';
import { join } from "node:path";
export default defineConfig({
plugins: [
twig({
namespaces: {
components: join(__dirname, "components"),
},
}),
yml(),
],
});
```<my-card data-id="55515" data-type="citation_card"></my-card>
import Twig from 'twig';
import drupalFilters from 'twig-drupal-filters';
function setupFilters(twig) {
twig.cache();
drupalFilters(twig);
return twig;
}
setupFilters(Twig);
For Yarn users: yarn set version berry, then yarn dlx sb init --builder webpack5 --type server
name: Card
props:
type: object
required:
- title
properties:
title:
type: string
title: Title
slots:
content:
title: Content
<div class="card">
<h2>{{ title }}</h2>
{{ content }}
</div>
import parse from 'html-react-parser';
import card from './card.twig';
import data from './card.yml';
export default { title: 'Components/Card' };
export const Default = () => parse(card(data));
components:
namespaces:
atoms: components/01-atoms
molecules: components/02-molecules
card:
css:
component:
dist/css/card.css: {}
```<my-card data-id="55515" data-type="citation_card"></my-card>
Varbase is Storybook production-ready. Varbase Storybook design system and UI component library gives teams a single source of truth for consistent, customizable, and accessible interfaces. It ships with reusable documented components, aligned with WCAG standards and performance best practice.
Q: Is Storybook in Drupal core?
A: No. Storybook isn't in Drupal core. It's an integration between Storybook app and Drupal theme
Q: Can I use Single Directory Components (SDC)?
A: Yes. SDC in Drupal 10+ integrates well with Storybook because each component has metadata, template, style, JS, etc.
Q: Will this affect live sites?
A: Storybook runs separately. It speeds up development without changing production environment.