Skip to Content
vitus·labs
Tools Storybook

API Reference

Complete API reference for @vitus-labs/tools-storybook.

Storybook Main Config

The default export from @vitus-labs/tools-storybook/storybook/main provides a complete StorybookConfig:

import type { StorybookConfig } from '@storybook/react-vite'

const STORYBOOK_CONFIG: StorybookConfig = {
  framework: resolveFramework(CONFIG.framework),
  stories: CONFIG.storiesDir,
  addons: [/* mapped from CONFIG.addons */],
  experimental_indexers: (existing) => [
    createManualStoryIndexer(existing), // delegates non-rocketstories files
    ...(CONFIG.autoDiscovery ? [autoDiscoveryIndexer] : []),
    ...existing,
  ],
  env: (existingEnv) => ({
    ...existingEnv,
    STORYBOOK_VL_UI_THEME: CONFIG.ui?.theme ?? 'dark',
  }),
  viteFinal: async (config) => {
    // Platform globals, extensions, aliases, plugins
    return config
  },
}

Framework Resolution

Config ValueStorybook Framework
'vite' (default)@storybook/react-vite
'next'@storybook/nextjs-vite
'react-native'@storybook/react-vite

viteFinal

The viteFinal hook:

  1. Defines platform globals (__BROWSER__, __WEB__, __NATIVE__, __NODE__, __CLIENT__) and __VITUS_LABS_STORIES__ (the resolved config, read by the preview)
  2. Resolves platform extensions — .web.* for web frameworks, .native.* for React Native
  3. Aliases react-native to react-native-web (React Native framework only)
  4. Mocks next/font (next/font/local, next/font/google, @next/font/*) via a plugin in optimizeDeps.rolldownOptions.plugins (Next.js framework only)
  5. Adds Vite plugins — vite-tsconfig-paths and rocketstoriesVitePlugin

Indexers

Storybook uses only the first indexer whose test matches a file, so the order matters:

IndexerMatchesPurpose
Manual story indexer*.stories.(js|jsx|ts|tsx|md|mdx)Indexes rocketstories files (default export x.init) itself; delegates every other story file to the first matching default indexer
Auto-discovery indexer/index.(js|jsx|ts|tsx)Only registered when autoDiscovery: true. Creates virtual story entries for component index files without a __stories__/ folder
Default indexers—Storybook's own CSF / MDX indexers

Rocketstories files are handled by the manual indexer because the CSF indexer cannot statically parse export default stories.init. Each export const Name = ... becomes a story; the title is taken from .config({ name }) or derived from the file path.

Addons Map

Only external addons need mapping — Storybook 10 essentials are built into core:

const ADDONS_MAP: Record<string, string> = {
  a11y: '@storybook/addon-a11y',
  chromatic: '@chromatic-com/storybook',
  designs: '@storybook/addon-designs',
  docs: '@storybook/addon-docs',
  mode: '@vueless/storybook-dark-mode',
  pseudoStates: 'storybook-addon-pseudo-states',
  themes: '@storybook/addon-themes',
  vitest: '@storybook/addon-vitest',
}

Storybook Preview Config

The default export from @vitus-labs/tools-storybook/storybook/preview is a Preview object built from the resolved config:

const preview: Preview = {
  tags: ['autodocs'],
  parameters: {
    // every object-valued `addons` entry, e.g. controls, viewport, backgrounds, darkMode
    ...addonParameters,
    // framework: 'next' only
    nextjs: { appDirectory: true },
  },
  initialGlobals: {
    backgrounds: { value: addons.backgrounds.default }, // if set
    ...globals, // explicit globals win
  },
}

Storybook Manager Config

@vitus-labs/tools-storybook/storybook/manager is a side-effect module (no exports). It calls addons.setConfig({ theme }) with themes.light when ui.theme is 'light' and themes.dark otherwise. The value reaches the manager bundle through the STORYBOOK_VL_UI_THEME env variable set in the main config.

StoriesConfig Interface

interface StoriesConfig {
  framework?: 'vite' | 'next' | 'react-native'
  outDir?: string
  port?: number
  storiesDir?: string[]
  monorepoStoriesDir?: string[]
  /** Generate virtual stories from component index files. Opt-in (default: false) */
  autoDiscovery?: boolean
  /** Globs for component index files, used when `autoDiscovery` is on */
  autoDiscoveryDir?: string[]
  monorepoAutoDiscoveryDir?: string[]
  ui?: {
    theme?: 'dark' | 'light'
  }
  globals?: Record<string, any>
  addons?: Partial<{
    controls: boolean | { expanded: boolean }
    actions: boolean
    a11y: boolean
    toolbars: boolean
    docs: boolean
    viewport: boolean | { options: Record<string, any> }
    chromatic: boolean
    designs: boolean
    mode: boolean
    pseudoStates: boolean
    themes: boolean
    vitest: boolean
    measure: boolean
    outline: boolean
    darkMode: { dark: any; light: any }
    backgrounds: {
      default: string
      options: Record<string, { name: string; value: string }>
      grid?: {
        disable?: boolean
        cellSize?: number
        opacity?: number
        cellAmount?: number
        offsetX?: number
        offsetY?: number
      }
    }
  }>
  rocketstories?: {
    module: string
    export?: string
  }
}

interface VLToolsConfig {
  stories?: StoriesConfig
  [key: string]: any
}

Default Configuration

The stories key from vl-tools.config.mjs (loaded through @vitus-labs/tools-core, which cascades config files from the cwd upward) is deep-merged over these defaults — your values win:

{
  outDir: '/docs',
  port: 6006,
  framework: 'vite',
  ui: { theme: 'dark' },
  storiesDir: ['/src/**/*.stories.@(js|jsx|ts|tsx|md|mdx)'],
  monorepoStoriesDir: ['/packages/*/src/**/*.stories.@(js|jsx|ts|tsx|md|mdx)'],
  autoDiscoveryDir: ['/src/**/index.@(js|jsx|ts|tsx)'],
  monorepoAutoDiscoveryDir: ['/packages/*/src/**/index.@(js|jsx|ts|tsx)'],
  autoDiscovery: false,
  globals: {},
  addons: {
    controls: { expanded: true },
    actions: true,
    a11y: true,
    toolbars: true,
    docs: true,
    viewport: { options: { ...MINIMAL_VIEWPORTS, ...INITIAL_VIEWPORTS } },
    chromatic: true,
    designs: true,
    mode: true,
    pseudoStates: true,
    themes: true,
    vitest: true,
    measure: true,
    outline: true,
    darkMode: { dark: themes.dark, light: themes.light },
    backgrounds: {
      default: 'light',
      options: {
        light: { name: 'Light', value: '#fff' },
        dark: { name: 'Dark', value: '#000' },
      },
      grid: {
        disable: false,
        cellSize: 8,
        opacity: 0.5,
        cellAmount: 4,
        offsetX: 16,
        offsetY: 16,
      },
    },
  },
  rocketstories: {
    module: '@vitus-labs/rocketstories',
    export: 'rocketstories',
  },
}

Resolved CONFIG

The CONFIG export is the merged config with paths made absolute:

  • outDir → ${process.cwd()}${outDir}
  • storiesDir → storiesDir (or monorepoStoriesDir when VL_MONOREPO=1), plus autoDiscoveryDir (or monorepoAutoDiscoveryDir) when autoDiscovery is on, each prefixed with process.cwd()

Rocketstories Vite Plugin

An internal Vite plugin (vite-plugin-rocketstories, not exported) that is added in viteFinal and serves the virtual story modules created by the auto-discovery indexer (virtual:rocketstory:<path>, resolved with a .tsx suffix so the generated JSX is parsed correctly). It is configured through the rocketstories option:

OptionDefaultDescription
module@vitus-labs/rocketstoriesModule the generated rocketstyle stories import from
exportrocketstoriesNamed export used as the story factory

For a rocketstyle component it generates:

import { rocketstories } from '@vitus-labs/rocketstories'
import Component from '/abs/path/to/Button/index.tsx'

const stories = rocketstories(Component).attrs({ label: 'Button' })

export default stories.init
export const Default = stories.main()
export const States = stories.dimension('state')   // one per detected dimension
export const PseudoStates = stories.render(/* normal/hover/focus/active/disabled grid */)

Plain React components get a CSF module with title, component and a Default story that renders <Component {...args} />.

CLI Runner Options

@vitus-labs/tools-storybook/storybook exports the option objects the CLI binaries pass to Storybook's build():

import { storybookStandalone, storybookBuild } from '@vitus-labs/tools-storybook/storybook'

storybookStandalone // { mode: 'dev', port: CONFIG.port, configDir }
storybookBuild      // { mode: 'static', outputDir: CONFIG.outDir, configDir }

configDir is the package's own bundled storybook/ directory (containing main, preview and manager).

Exports Summary

// Config and types
import { CONFIG, TS_CONFIG } from '@vitus-labs/tools-storybook'
import type { StoriesConfig, VLToolsConfig } from '@vitus-labs/tools-storybook'

// CLI runner options
import { storybookStandalone, storybookBuild } from '@vitus-labs/tools-storybook/storybook'

// Storybook configs (only needed when running the storybook CLI directly)
export { default } from '@vitus-labs/tools-storybook/storybook/main'    // .storybook/main.ts
export { default } from '@vitus-labs/tools-storybook/storybook/preview' // .storybook/preview.ts
import '@vitus-labs/tools-storybook/storybook/manager'                  // .storybook/manager.ts
Edit on GitHub

Last updated on

On this page