CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--preview-api

Storybook's core preview API providing hooks, decorators, story composition utilities, and simulation tools for building UI components in isolation

Pending
Overview
Eval results
Files

story-store.mddocs/

Story Store & Management

Central story management system providing story loading, caching, args management, and context creation. The Story Store serves as the core infrastructure for Storybook's preview environment, handling story lifecycle and state management.

Capabilities

Story Store

Primary class for managing stories, their state, and related configurations.

/**
 * Central story management class handling story lifecycle and state
 */
class StoryStore<TRenderer> {
  /** Index of all available stories */
  storyIndex: StoryIndex;
  /** Global project configuration */
  projectAnnotations: ProjectAnnotations<TRenderer>;
  /** User-defined global values */
  userGlobals: Args;
  /** Args store for managing story arguments */
  args: ArgsStore;
  /** Hook context for story execution */
  hooks: HooksContext<TRenderer>;
  
  constructor(args: StoryStoreArgs<TRenderer>);
  
  /**
   * Initialize the store with story index and project annotations
   * @param storyIndex - Index of all stories
   * @param projectAnnotations - Global project configuration
   */
  initialize(storyIndex: StoryIndex, projectAnnotations: ProjectAnnotations<TRenderer>): Promise<void>;
  
  /**
   * Load and cache a story by ID
   * @param storyId - Unique story identifier
   * @returns Promise resolving to the loaded story
   */
  loadStory(storyId: string): Promise<Story<TRenderer>>;
  
  /**
   * Get story context for rendering
   * @param story - Story object
   * @returns Complete story context
   */
  getStoryContext(story: Story<TRenderer>): StoryContext<TRenderer>;
  
  /**
   * Update story args
   * @param storyId - Story identifier
   * @param newArgs - Args to update
   */
  updateStoryArgs(storyId: string, newArgs: Args): void;
  
  /**
   * Reset story args to defaults
   * @param storyId - Story identifier
   * @param argNames - Specific arg names to reset (optional)
   */
  resetStoryArgs(storyId: string, argNames?: string[]): void;
}

interface StoryStoreArgs<TRenderer> {
  /** Module loading function */
  importFn: (path: string) => Promise<any>;
  /** Cache for loaded stories */
  cache?: Map<string, any>;
  /** Initial story index */
  storyIndex?: StoryIndex;
}

Usage Examples:

import { StoryStore } from "@storybook/preview-api";

// Create story store instance
const storyStore = new StoryStore({
  importFn: (path) => import(path),
  cache: new Map()
});

// Initialize with story index
await storyStore.initialize(storyIndex, projectAnnotations);

// Load specific story
const story = await storyStore.loadStory('example-button--primary');

// Get story context for rendering
const context = storyStore.getStoryContext(story);

// Update story args
storyStore.updateStoryArgs('example-button--primary', { 
  label: 'Updated Label',
  disabled: false 
});

// Reset specific args
storyStore.resetStoryArgs('example-button--primary', ['disabled']);

Args Management

Specialized store for managing story arguments across the application.

/**
 * Store for managing story arguments with persistence and updates
 */
class ArgsStore {
  constructor();
  
  /**
   * Get args for a specific story
   * @param storyId - Story identifier
   * @returns Current args for the story
   */
  get(storyId: string): Args;
  
  /**
   * Update args for a story
   * @param storyId - Story identifier
   * @param newArgs - Args to merge with existing args
   */
  update(storyId: string, newArgs: Args): void;
  
  /**
   * Reset args for a story to defaults
   * @param storyId - Story identifier
   * @param argNames - Specific arg names to reset (optional)
   */
  reset(storyId: string, argNames?: string[]): void;
  
  /**
   * Get all args across all stories
   * @returns Map of story IDs to their args
   */
  getAll(): Record<string, Args>;
}

Globals Management

Store for managing global values that apply across all stories.

/**
 * Store for managing global values across all stories
 */
class GlobalsStore {
  constructor();
  
  /**
   * Get current global values
   * @returns Current globals object
   */
  get(): Args;
  
  /**
   * Update global values
   * @param newGlobals - Globals to merge with existing values
   */
  update(newGlobals: Args): void;
  
  /**
   * Reset globals to defaults
   */
  reset(): void;
}

Story Index Management

System for managing the index of all available stories.

/**
 * Store for managing the story index and metadata
 */
class StoryIndexStore {
  constructor();
  
  /**
   * Initialize with story index data
   * @param storyIndex - Complete story index
   */
  initialize(storyIndex: StoryIndex): void;
  
  /**
   * Get story entry by ID
   * @param storyId - Story identifier
   * @returns Story index entry or undefined
   */
  getStoryEntry(storyId: string): StoryIndexEntry | undefined;
  
  /**
   * Get all stories for a component
   * @param componentId - Component identifier
   * @returns Array of story entries
   */
  getStoriesForComponent(componentId: string): StoryIndexEntry[];
  
  /**
   * Search stories by title or ID
   * @param query - Search query
   * @returns Matching story entries
   */
  searchStories(query: string): StoryIndexEntry[];
}

interface StoryIndex {
  /** Version of the story index format */
  v: number;
  /** Map of story IDs to story entries */
  entries: Record<string, StoryIndexEntry>;
}

interface StoryIndexEntry {
  /** Unique story identifier */
  id: string;
  /** Display name of the story */
  name: string;
  /** Component title/path */
  title: string;
  /** Path to the story file */
  importPath: string;
  /** Story type (story or docs) */
  type: 'story' | 'docs';
  /** Story tags for filtering */
  tags?: string[];
}

Story Loading & Caching

Functions and utilities for efficient story loading and caching.

/**
 * Load CSF file and process its exports
 * @param importFn - Function to import the CSF file
 * @param importPath - Path to the CSF file
 * @param cache - Optional cache for loaded modules
 * @returns Processed CSF file with stories
 */
function loadCSFFile<TRenderer>(
  importFn: (path: string) => Promise<any>,
  importPath: string,
  cache?: Map<string, any>
): Promise<CSFFile<TRenderer>>;

interface CSFFile<TRenderer = any> {
  /** File metadata */
  meta: ComponentAnnotations<TRenderer>;
  /** Map of story names to story annotations */
  stories: Record<string, StoryAnnotations<TRenderer>>;
  /** Component being documented */
  component?: any;
  /** Module exports */
  moduleExports: any;
}

Reporter API

System for collecting and reporting test results and story execution data.

/**
 * API for collecting and reporting test results
 */
class ReporterAPI {
  constructor();
  
  /**
   * Report test results for a story
   * @param storyId - Story identifier
   * @param report - Test report data
   */
  report(storyId: string, report: Report): void;
  
  /**
   * Get all reports
   * @returns Map of story IDs to their reports
   */
  getReports(): Record<string, Report[]>;
  
  /**
   * Clear all reports
   */
  clearReports(): void;
}

interface Report<T = unknown> {
  /** Report type identifier */
  type: string;
  /** Report format version */
  version?: number;
  /** Report data payload */
  result: T;
  /** Execution status */
  status: 'failed' | 'passed' | 'warning';
  /** Error message if failed */
  message?: string;
  /** Timestamp of report creation */
  timestamp?: number;
}

Usage Examples:

import { ReporterAPI } from "@storybook/preview-api";

const reporter = new ReporterAPI();

// Report test success
reporter.report('example-button--primary', {
  type: 'test-result',
  version: 1,
  result: { testsPassed: 5, testsTotal: 5 },
  status: 'passed'
});

// Report test failure
reporter.report('example-button--secondary', {
  type: 'test-result', 
  version: 1,
  result: { error: 'Assertion failed' },
  status: 'failed',
  message: 'Expected button to be disabled'
});

// Get all reports
const allReports = reporter.getReports();
console.log(allReports['example-button--primary']);

Types & Interfaces

interface Story<TRenderer = any> {
  /** Unique story identifier */
  id: string;
  /** Story display name */
  name: string;
  /** Component title */
  title: string;
  /** Story args */
  args: Args;
  /** Story arg types */
  argTypes: ArgTypes;
  /** Story parameters */
  parameters: Parameters;
  /** Component being rendered */
  component?: any;
  /** Story render function */
  renderFn: (args: Args, context: StoryContext<TRenderer>) => any;
  /** Play function for interactions */
  playFn?: (context: PlayFunctionContext<TRenderer>) => Promise<void> | void;
}

interface StoryContext<TRenderer = any> {
  /** Story identifier */
  id: string;
  /** Story name */
  name: string;
  /** Component title */
  title: string;
  /** Current story args */
  args: Args;
  /** Story arg types definition */
  argTypes: ArgTypes;
  /** Story parameters */
  parameters: Parameters;
  /** Global values */
  globals: Args;
  /** Hook execution context */
  hooks: HooksContext<TRenderer>;
  /** Current view mode */
  viewMode: 'story' | 'docs';
  /** Loaded data from loaders */
  loaded: Record<string, any>;
  /** Abort signal for cancellation */
  abortSignal: AbortSignal;
}

interface Args {
  [key: string]: any;
}

interface ArgTypes {
  [key: string]: ArgType;
}

interface Parameters {
  [key: string]: any;
}

Install with Tessl CLI

npx tessl i tessl/npm-storybook--preview-api

docs

decorators.md

hooks.md

index.md

preview-system.md

story-composition.md

story-store.md

testing-simulation.md

tile.json