Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

storybook-addon-code-editor

JeremyRH87.3kMIT5.0.0TypeScript support: included

A Storybook add-on for live editing stories. Supports React and TypeScript.

storybook-addons, code, editor, live, real-time

readme

storybook-addon-code-editor

A Storybook add-on for live editing stories. Supports React and TypeScript.

See it in action.

See an example project using this add-on.

What is it?

This is a Storybook addon that enables live editing of React components with real-time previews. Think of it like a lightweight CodeSandbox, directly in stories or MDX pages.

It uses Monaco Editor (VS Code for the browser) for an excellent TypeScript editing experience.

Get started

  1. Install as a dev dependency:
npm install --save-dev storybook-addon-code-editor
# Or yarn:
yarn add --dev storybook-addon-code-editor
  1. Add storybook-addon-code-editor in your .storybook/main.ts file and ensure the staticDirs, addons, and framework fields contain the following:
// .storybook/main.ts
import type { StorybookConfig } from '@storybook/react-vite';
import { getCodeEditorStaticDirs } from 'storybook-addon-code-editor/getStaticDirs';

const config: StorybookConfig = {
  staticDirs: [...getCodeEditorStaticDirs(__filename)],
  addons: ['storybook-addon-code-editor'],
  framework: {
    name: '@storybook/react-vite',
    options: {},
  },
};

export default config;
<summary>About `staticDirs`</summary>

staticDirs sets a list of directories of static files to be loaded by Storybook. The editor (monaco-editor) requires these extra static files to be available at runtime.

Additional static files can be added using the getExtraStaticDir helper from storybook-addon-code-editor/getStaticDirs:

// .storybook/main.ts
import {
  getCodeEditorStaticDirs,
  getExtraStaticDir,
} from 'storybook-addon-code-editor/getStaticDirs';

const config: StorybookConfig =  {
  staticDirs: [
    ...getCodeEditorStaticDirs(__filename),
    // files will be available at: /monaco-editor/esm/*
    getExtraStaticDir('monaco-editor/esm'),

Important:

@storybook/react-vite is the only supported framework at this time.


API

Playground

Use the Playground component in MDX format.

// MyComponent.stories.mdx
import { Playground } from 'storybook-addon-code-editor'

<Playground code="export default () => <h1>Hello</h1>" />
<summary>More advanced example</summary>
// MyComponent.stories.mdx
import { Playground } from 'storybook-addon-code-editor';
import \* as MyLibrary from './index';
import storyCode from './MyStory.source.tsx?raw';

// TypeScript might complain about not finding this import or
// importing things from .d.ts files wihtout `import type`.
// Ignore this, we need the string contents of this file.
// @ts-ignore
import MyLibraryTypes from '../dist/types.d.ts?raw';

<Playground
  availableImports={{ 'my-library': MyLibrary }}
  code={storyCode}
  height="560px"
  id="unique id used to save edited code until the page is reloaded"
  modifyEditor={(monaco, editor) => {
    // editor docs: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html
    // monaco docs: https://microsoft.github.io/monaco-editor/api/modules/monaco.html
    editor.getModel().updateOptions({ tabSize: 2 });
    monaco.editor.setTheme('vs-dark');
    monaco.languages.typescript.typescriptDefaults.addExtraLib(
      MyLibraryTypes,
      'file:///node_modules/my-library/index.d.ts',
    );
  }}
/>

Playground props:

interface PlaygroundProps {
  availableImports?: {
    [importSpecifier: string]: {
      [namedImport: string]: any;
    };
  };
  code?: string;
  defaultEditorOptions?: Monaco.editor.IEditorOptions;
  height?: string;
  id?: string | number | symbol;
  modifyEditor?: (monaco: Monaco, editor: Monaco.editor.IStandaloneCodeEditor) => any;
}

React is automatically imported if code does not import it. React TypeScript definitions will be automatically loaded if @types/react is available.

makeLiveEditStory

Use the makeLiveEditStory function in traditional stories to show a code editor panel:

// MyComponent.stories.ts
import type { Meta, StoryObj } from '@storybook/react';
import { makeLiveEditStory } from 'storybook-addon-code-editor';
import * as MyLibrary from './index';
import storyCode from './MyStory.source.tsx?raw';

const meta = {
  // Story defaults
} satisfies Meta<typeof MyLibrary.MyComponent>;

export default meta;

type Story = StoryObj<typeof meta>;

export const MyStory: Story = {
  // Story config
};

makeLiveEditStory(MyStory, {
  availableImports: { 'my-library': MyLibrary },
  code: storyCode,
});

makeLiveEditStory options:

interface LiveEditStoryOptions {
  availableImports?: {
    [importSpecifier: string]: {
      [namedImport: string]: any;
    };
  };
  code: string;
  modifyEditor?: (monaco: Monaco, editor: Monaco.editor.IStandaloneCodeEditor) => any;
  defaultEditorOptions?: Monaco.editor.IEditorOptions;
}

setupMonaco

setupMonaco allows customization of monaco-editor.

Use this in your .storybook/preview.ts to add type definitions or integrations.

Check out examples of monaco-editor with different configurations.

// .storybook/preview.ts
import { setupMonaco } from 'storybook-addon-code-editor';

setupMonaco({
  // https://microsoft.github.io/monaco-editor/typedoc/interfaces/Environment.html
  monacoEnvironment: {
    getWorker(moduleId, label) {
      ...
    },
  },
  // onMonacoLoad is called when monaco is first loaded, before an editor instance is created.
  onMonacoLoad(monaco) {
    ...
  },
});

setupMonaco options:

interface MonacoSetup {
  monacoEnvironment?: Monaco.Environment;
  onMonacoLoad?: (monaco: Monaco) => any;
}

Contributing

Install dependencies

npm install

Run example

npm run start-example

When making changes to the library, the server needs to be manually restarted.

Run tests

npm run test

Format code

npm run format

Build library

npm run build

Commits

Use conventional commits to allow automatic versioned releases.

  • fix: represents bug fixes, and correlates to a SemVer patch.
  • feat: represents a new feature, and correlates to a SemVer minor.
  • feat!:, or fix!:, refactor!:, etc., represent a breaking change (indicated by the !) and will result in a SemVer major.

Publishing

The automated release-please PR to the main branch can be merged to deploy a release.

changelog

Changelog

5.0.0 (2025-06-02)

⚠ BREAKING CHANGES

  • support Storybook 9

Features

4.1.2 (2025-05-16)

Bug Fixes

4.1.1 (2025-04-03)

Bug Fixes

  • Clean up examples/docs. (1dccf27)

4.1.0 (2025-04-02)

Features

  • Add makeLiveEditStory to replace createLiveEditStory. createLiveEditStory is still available but deprecated. Storybook adds some features during the build by reading the story object. This means addons should not change the way a story is defined. In order to keep the interface simple, makeLiveEditStory does not create the story object. It takes an already defined story object and modifies it. (613261c)

Bug Fixes

  • Import type directly. (cd1d2d2)
  • React types needed JSX types. (7b7c2f7)

4.0.1 (2025-04-01)

Bug Fixes

  • passing tags to the story (57b2d0e)

4.0.0 (2025-03-30)

⚠ BREAKING CHANGES

  • Replace Playground wrapping component with Container. This breaking change allows complete control of the Playground style.

Features

  • Hide code editor panel tab if no story editor id is found (indicating createLiveEditStory was not used). (3f1799e)
  • Replace Playground wrapping component with Container. This breaking change allows complete control of the Playground style. (a8bcaea)
  • Update all deps to latest. Replace jest with vitest and babel with tsc. (c055c92)

Bug Fixes

3.2.0 (2024-12-13)

Features

Bug Fixes

  • combine post-build scripts (8ba7432)
  • Node 22.12 error with cjs imports (6c8719c)
  • remove top-level getStaticDirs and only rely on package.json exports (31dc7a4)

3.1.0 (2024-07-01)

Features

  • add editor options and wrapping component prop (5d47fd8)

Bug Fixes

3.0.3 (2024-06-25)

Bug Fixes

  • only use window.parent and fall back to window to prevent manager from throwing (cc48522)

3.0.2 (2024-06-24)

Bug Fixes

  • iframed storybook sites fail due to window.top access (02f8255)

3.0.1 (2024-06-12)

Bug Fixes

  • use os path separator for windows support (bb2d73a)

3.0.0 (2024-05-17)

⚠ BREAKING CHANGES

  • Update to storybook 8
  • Drop support for StoryBook version 6 & 7.
  • Drop support for React 16. Use react/jsx-runtime for jsx.
  • createLiveEditStory changed to accept all story fields.

    // Before:
    export const StoryA = createLiveEditStory({ code: StoryASource });
    // Had to mutate the Story
    StoryA.args = { foo: 'foo' };
    // After:
    export const StoryA = createLiveEditStory({
    code: StoryASource,
    args: { foo: 'foo' },
    });
  • Remove automatic configuration for webpack.

  • MDX updated, some breaking changes.
  • Update TypeScript which may cause breaking changes in types.
  • Add package.json "exports" and "type: module".

Features

Bug Fixes

2.2.2 (2024-05-15)

Bug Fixes

2.2.1 (2024-02-13)

Bug Fixes

  • make getStaticDirs work with yarn zero installs (0daab18)

2.2.0 (2023-06-14)

Features

Bug Fixes

  • detect react import default and named (bed3329)
  • resolve package location without require.resolve (b097d3f)

2.1.0 (2023-04-04)

Features

  • Playground: add id to persist code changes until page reload (4a8ae8f)

2.0.2 (2023-04-01)

Bug Fixes

  • update rock-paper-scissors example (a8add75)

2.0.0

⚠ BREAKING CHANGES

  • require staticDirs in .storybook/main
  • drop support for webpack 4 and add support for vite
  • remove setupEditor option and add setupMonaco function
  • rename onCreateEditor option to modifyEditor

Features

  • support vite
  • make react types optional