CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--react

React renderer for Storybook framework providing TypeScript support, portable stories, and React-specific functionality

Pending
Overview
Eval results
Files

preview-configuration.mddocs/

Preview Configuration

Advanced preview system for configuring the Storybook React environment, including decorators, parameters, and rendering behavior. The preview configuration enables type-safe story creation with addon integration and custom rendering logic.

Type Dependencies

The preview configuration API depends on Storybook core and React types:

// Main preview configuration export
import { __definePreview } from "@storybook/react";

// Core Storybook types
import type {
  Args,
  ProjectAnnotations,
  ComponentAnnotations,
  StoryAnnotations,
  DecoratorFunction,
  ArgsStoryFn,
  PreviewAddon,
  AddonTypes,
  Preview,
  Meta,
  Story,
  InferTypes
} from "storybook/internal/types";

// React types
import type { ComponentType } from "react";

// Utility types
import type { SetOptional, Simplify, UnionToIntersection } from "type-fest";

// React renderer types (see React Renderer Types documentation)
import type { ReactTypes, ReactRenderer } from "./react-renderer.md";

// Story types (see Story Types & Metadata documentation)
import type { AddMocks } from "./story-types.md#addmocks-type-helper";

Capabilities

Define Preview

Creates a strongly-typed preview configuration with addon support and React-specific functionality.

/**
 * Defines a preview configuration with addon integration and type safety.
 * Combines React renderer annotations with custom addons and project configuration.
 * 
 * @param input - Configuration object containing addons and project annotations
 * @returns Strongly-typed ReactPreview instance with addon support
 */
function __definePreview<Addons extends PreviewAddon<never>[]>(
  input: { addons: Addons } & ProjectAnnotations<ReactTypes & InferTypes<Addons>>
): ReactPreview<ReactTypes & InferTypes<Addons>>;

Usage Example:

import { __definePreview } from "@storybook/react";
import type { Preview } from "@storybook/react";

// Define preview with custom configuration
const preview = __definePreview({
  addons: [], // Custom addons would go here
  parameters: {
    actions: { argTypesRegex: "^on[A-Z].*" },
    controls: {
      matchers: {
        color: /(background|color)$/i,
        date: /Date$/,
      },
    },
    backgrounds: {
      default: "light",
      values: [
        { name: "light", value: "#ffffff" },
        { name: "dark", value: "#333333" },
      ],
    },
  },
  decorators: [
    (Story, context) => (
      <div className={`theme-${context.globals.theme}`}>
        <Story />
      </div>
    ),
  ],
});

React Preview Interface

Extended preview interface that provides React-specific functionality and type-safe meta creation.

/**
 * Extended preview interface for React with addon type inference.
 * Provides enhanced meta creation with React-specific typing and story composition.
 */
interface ReactPreview<T extends AddonTypes> extends Preview<ReactTypes & T> {
  /**
   * Creates component metadata with React-specific type safety and addon support.
   * Automatically configures story creation with proper component prop inference.
   */
  meta<
    TArgs extends Args,
    Decorators extends DecoratorFunction<ReactTypes & T, any>,
    TMetaArgs extends Partial<TArgs>,
  >(
    meta: {
      render?: ArgsStoryFn<ReactTypes & T, TArgs>;
      component?: ComponentType<TArgs>;
      decorators?: Decorators | Decorators[];
      args?: TMetaArgs;
    } & Omit<
      ComponentAnnotations<ReactTypes & T, TArgs>,
      'decorators' | 'component' | 'args' | 'render'
    >
  ): ReactMeta<ReactTypes & T, TArgs>;
}

Usage Example:

const preview = __definePreview({
  addons: [],
  // ... other configuration
});

// Create type-safe meta using the preview
const buttonMeta = preview.meta({
  title: "Example/Button",
  component: Button,
  args: {
    primary: false,
    size: "medium",
  },
  argTypes: {
    size: {
      control: { type: "select" },
      options: ["small", "medium", "large"],
    },
  },
});

React Meta Interface

Enhanced meta interface that provides React-specific story creation capabilities.

/**
 * React-specific meta interface that extends standard Meta with React functionality.
 * Provides enhanced story creation with component inference and decorator support.
 */
interface ReactMeta<T extends ReactTypes, MetaInput extends ComponentAnnotations<T>>
  extends Meta<T, MetaInput> {
  /**
   * Creates a story with React-specific functionality and type safety.
   * Supports both functional and object-based story definitions.
   */
  story<
    TInput extends
      | (() => ReactTypes['storyResult'])
      | (StoryAnnotations<T, T['args']> & {
          render: () => ReactTypes['storyResult'];
        }),
  >(
    story?: TInput
  ): ReactStory<T, TInput extends () => ReactTypes['storyResult'] ? { render: TInput } : TInput>;

  /**
   * Creates a story with full configuration and type inference.
   * Handles optional args based on meta configuration.
   */
  story<
    TInput extends Simplify<
      StoryAnnotations<
        T,
        AddMocks<T['args'], MetaInput['args']>,
        SetOptional<T['args'], keyof T['args'] & keyof MetaInput['args']>
      >
    >,
  >(
    story?: TInput
  ): ReactStory<T, TInput>;
}

React Story Interface

Enhanced story interface that provides a composable React component.

/**
 * React-specific story interface that extends standard Story with React functionality.
 * Provides a Component property that can be rendered directly as a React component.
 */
interface ReactStory<T extends ReactTypes, TInput extends StoryAnnotations<T, T['args']>>
  extends Story<T, TInput> {
  /**
   * React component that can be rendered directly.
   * Automatically composed with story configuration and args.
   */
  Component: ComponentType<Partial<T['args']>>;
}

Usage Example:

const meta = preview.meta({
  component: Button,
  args: { size: "medium" },
});

const primaryStory = meta.story({
  args: { primary: true },
});

// Use the Component property to render the story
function MyTest() {
  return (
    <div>
      <primaryStory.Component label="Test Button" />
    </div>
  );
}

Decorator Args Type Helper

Type helper for inferring args from decorator functions.

/**
 * Type helper that extracts and merges args from decorator functions.
 * Ensures proper type inference when multiple decorators provide args.
 */
type DecoratorsArgs<TRenderer extends Renderer, Decorators> = UnionToIntersection<
  Decorators extends DecoratorFunction<TRenderer, infer TArgs> ? TArgs : unknown
>;

Advanced Configuration Patterns

Custom Decorator with Args:

import type { Decorator } from "@storybook/react";

const withTheme: Decorator<{ theme?: string }> = (Story, context) => {
  const theme = context.args.theme || context.parameters.theme || "light";
  return (
    <div className={`theme-${theme}`}>
      <Story />
    </div>
  );
};

const preview = __definePreview({
  addons: [],
  decorators: [withTheme],
  parameters: {
    theme: "light",
  },
});

Preview with Global Types:

import type { Preview } from "@storybook/react";

// Define global types for your project
declare module "@storybook/react" {
  interface Parameters {
    myCustomParameter?: {
      option1: boolean;
      option2: string;
    };
  }
}

const preview: Preview = {
  parameters: {
    myCustomParameter: {
      option1: true,
      option2: "default",
    },
  },
};

React Server Components Configuration:

const preview = __definePreview({
  addons: [],
  parameters: {
    react: {
      rsc: true, // Enable React Server Components
      rootOptions: {
        onRecoverableError: (error) => {
          console.warn("Recoverable error:", error);
        },
      },
    },
  },
});

Internal Configuration

The preview system internally includes several React-specific annotations:

  • reactAnnotations: Core React rendering functionality
  • reactArgTypesAnnotations: Automatic argTypes inference for React props
  • reactDocsAnnotations: Documentation generation for React components

These are automatically included in the preview configuration and provide the foundation for React-specific Storybook functionality.

Install with Tessl CLI

npx tessl i tessl/npm-storybook--react

docs

index.md

portable-stories.md

preview-configuration.md

react-renderer.md

story-types.md

testing-integration.md

tile.json