@canonical/storybook-config

中文文档
0.45.0发布于 2026/10/23LGPL-3.0TypeScriptES Module构建来源已验证近 30 天下载 9919

Canonical's standard Storybook configuration factory

原始使用文档
文档目录6 节
  1. Getting Started
  2. Notes
  3. Theming: two documents, not one
  4. Why a container rather than parameters.docs.theme
  5. Why the container lives here and not in the shell-theme add…
  6. Caveats

Canonical Storybook Configuration

This package provides a reusable configuration factory for Canonical's Storybook projects.

This package, at the moment, solely exports the shared config. We might in the future, leverage the factory pattern to provide more customization.

Getting Started

  1. In your React, Svelte or Web Components Storybook project, install this package with bun add -d @canonical/storybook-config
  2. Replace the contents of .storybook/main.ts by
import { createConfig } from "@canonical/storybook-config";

export default createConfig("react"); // or one of "svelte", "sveltekit", "lit"

Notes

The autodocs feature is enabled project-wide by setting tags: ["autodocs"] at preview level, so story files do not need to declare the tag themselves (a story can still opt out with tags: ["!autodocs"]).

This package exports the shared preview configuration (@canonical/storybook-config/preview), which includes that default. However, Storybook statically parses each project's own .storybook/preview.ts and does not reliably pick up tags (or storySort) spread from an imported preview (storybookjs/storybook#31842). Consuming projects must therefore declare tags: ["autodocs"] inline in their own .storybook/preview.ts:

import previewConfig from "@canonical/storybook-config/preview";

const preview = {
  ...previewConfig,
  tags: ["autodocs"],
};

export default preview;

Theming: two documents, not one

Storybook renders into two separate documents, and this is the single fact that most theming confusion here reduces to.

  • The manager — sidebar and toolbar — is themed by addons.setConfig({ theme }), which @canonical/storybook-addon-shell-theme does from its manager entry point.
  • Documentation pages render inside the preview iframe. The manager theme never reaches them. Given no theme of their own, Storybook falls back to its built-in light theme, so every docs page renders as unbranded Storybook grey no matter what the frame around it looks like. That was #962.

This package closes that gap: previewConfig sets parameters.docs.container to src/DocsContainer.tsx, which passes the Canonical theme into the docs page and follows the reader's OS light/dark preference.

Story content theming is not part of this — that belongs to @canonical/storybook-addon-utils, which owns the scheme toolbar and the .light / .dark classes.

Why a container rather than parameters.docs.theme

docs.theme is a plain parameter, read once at render. The page would be correct on load but would not follow a live OS change — it would need a reload while the manager around it flipped instantly. A container can subscribe to prefers-color-scheme.

Why the container lives here and not in the shell-theme addon

It looks like it belongs in the addon, and moving it is the obvious future refactor. It breaks. That addon's Vite build externalises storybook/* but nothing else, so bundling React and @storybook/addon-docs into it ships a second React — and the container calls a hook, so the duplicate copy raises an invalid hook call. This package already carries @storybook/addon-docs, React types and a React TypeScript config, so the component belongs here.

Note the failure mode precisely, because the obvious guess is wrong: it is not a lost docs context. @storybook/addon-docs parks its context on globalThis.__DOCS_CONTEXT__, first loader winning, specifically so duplicate bundles share one instance. React is the singleton that does not survive being duplicated.

React is safe for the Svelte and Lit Storybooks: @storybook/addon-docs declares React as a hard dependency, and the container renders through the docs renderer rather than as a story decorator.

Caveats

  • At the moment the factory is not configurable. We are not sure what the best api to pass custom config parameters would be, if any.
  • This storybook config for the time being only implementing a factory for react/vite, svelte/vite and lit/vite. We imagine this might change to accomodate other frameworks and build tools.

版本与发布记录

@canonical/storybook-config 共发布 55 个版本,首次发布于 2025/02/13;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.45.0latest2026/10/2317 天前
0.44.12026/10/081 个月前
0.44.02026/10/051 个月前
0.43.02026/10/011 个月前
0.42.02026/09/281 个月前
0.40.02026/09/211 个月前

关于 @canonical/storybook-config 的常见问题

@canonical/storybook-config 是什么?

Canonical's standard Storybook configuration factory

如何安装 @canonical/storybook-config?

在项目目录运行 npm install @canonical/storybook-config,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5.9.3 || ^6.0.0 || ^7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@canonical/storybook-config 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@canonical/storybook-config 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@canonical/storybook-config 可以商用吗?

@canonical/storybook-config 使用 LGPL-3.0 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@canonical/storybook-config 的下载量有多少?

根据 npm 官方下载统计,@canonical/storybook-config 在 2026/10/09 – 2026/11/07 这 30 天内被下载 9,919 次,日均约 331 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canonical/storybook-config 还在维护吗?

最新版本 0.45.0 发布于 2026/10/23(17 天前);至今共发布 55 个版本,其中过去 12 个月发布了 44 个;首次发布于 2025/02/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。