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

Package detail

storybook-addon-live-examples

reme3d2y2.7kMIT2.0.3TypeScript support: included

Storybook live examples plugin

storybook-addons, code, test

readme

⚡ Storybook Addon Live Examples ⚡

Code playground with live editing inside your storybook

  • 🧑‍💻 Play with code without 3rd-party service services like codepen
  • 👥 Share examples with others
  • 🐛 Share links to bug reproductions with others
  • 🧱 Check how the components work together
  • Typescript supported

Read docs or Try live demo

Getting started

1. Install addon

yarn add -D storybook-addon-live-examples
# npm install --save-dev storybook-addon-live-examples

2. Register addon in main.js

module.exports = {
    addons: ['storybook-addon-live-examples'],
};

3. Setup addon in preview.js (optional step)

import { addons } from '@storybook/addons';
import { LIVE_EXAMPLES_ADDON_ID } from 'storybook-addon-live-examples';
import theme from 'prism-react-renderer/themes/github';

import AllComponents from '../packages';

addons.setConfig({
    [LIVE_EXAMPLES_ADDON_ID]: {
        // custom theme from prism-react-renderer (optional)
        editorTheme: theme,
        // internationalization (optional)
        copyText: ['Copy', 'Copied'],
        expandText: ['Show code', 'Hide code'],
        shareText: ['Share', 'Shared'],
        // scope (globally accessible components & functions) (optional)
        scope: {
            ...AllComponents,
            someFunction: () => 42
        },
    },
});

4. Configure webpack (for storybook 7 only)

const { patchWebpackConfig } = require('storybook-addon-live-examples/dist/cjs/utils');

module.exports = {
    webpackFinal: (config) => {
        patchWebpackConfig(config);

        return config;
    }
};

Usage

CSF

Live examples will be rendered instead of the default addon-docs canvas.

Your can customize examples by parameters:

export default {
    title: 'Components/Button',
    parameters: {
        scope: {
            scopeValue,
        },
    }
};

const scopeValue = 42;

export const Primary = () => <button>{scopeValue}</button>;

Primary.parameters = {
    expanded: true
};

export const Secondary = () => <button>{scopeValue}</button>;

NOTE: Most likely you will get errors after addon installing. Don't panic, just pass all variables that are used in your story to scope

MDX

Inside MDX-based stories you can write your code examples with plain markdown.

Just put your code inside triple quotes

|```tsx live
|<h4>Wow, so simple</h4>
|

**Or render custom Canvas**

```tsx
// Import custom Canvas from addon
import { Canvas } from 'storybook-addon-live-examples';

<Canvas live={true} scope={{ value: 42 }}>
    <h4>Wow, so simple, {value}</h4>
</Canvas>

Or use Example directly

import { Example } from 'storybook-addon-live-examples';

<Example live={true} code={`<h4>Wow, so simple</h4>`} />

CSF With MDX

// Button.stories.js

import mdx from './Button.mdx';

export default {
    title: 'Components/Button',
    parameters: {
        docs: {
            page: mdx,
        },
    },
};

const scopeValue = 42;

export const Primary = () => <button>{scopeValue}</button>;

Primary.parameters = {
    scope: {
        scopeValue,
    },
};
// Button.mdx

import { ArgsTable, Story } from '@storybook/addon-docs';

import { Button } from './Button';

# Button

<ArgsTable of={Button} />

<Story id='components-button--primary' />

Example props

You can customize the display of examples with props or metastring

live

|```tsx live
|<span>This example can be edited</span>
|

```tsx live
<span>This example can be edited</span>

expanded

|```tsx live expanded
|<span>This example will be rendered with expanded code sources</span>
|

```tsx live expanded
<span>This example will be rendered with expanded code sources</span>

Complex examples

`tsx live expanded render(() => { const [counter, setCounter] = React.useState(0); return ( <>

Super live counter: {counter}

<button type='button' onClick={() => setCounter((c) => c + 1)}> Increment </button> </> ); });


## Scope

Storybook-addon-live-examples uses [react-live](https://github.com/FormidableLabs/react-live) under the hood.

Scope allows you to pass some globals to your code examples.
By default it injects React only, which means that you can use it in code like this:

```tsx
render(() => {
//                                ↓↓↓↓↓
    const [counter, setCounter] = React.useState(0);
    return counter;
}

- Pass your own components to scope by props

import { Canvas } from 'storybook-addon-live-examples';
import MyComponent from '../packages/my-component';

<Canvas live={true} scope={{ MyComponent }}>
    <MyComponent>Amazing</MyComponent>
</Canvas>

- Setup scope globally

This is the easiest way to setup scope once for an entire project

//.storybook/manager.js

import { addons } from '@storybook/addons';
import { LIVE_EXAMPLES_ADDON_ID } from 'storybook-addon-live-examples';

addons.setConfig({
    [LIVE_EXAMPLES_ADDON_ID]: {
        scope: {
            MyComponent,
        },
    },
});
<MyComponent>Now, you can use MyComponent in all examples</MyComponent>

- Setup scope inside monorepo

This is an example of how you can add all used components and helpers to the scope.

// .storybook/scope.ts

import { ComponentType } from 'react';

import * as icons from 'some-icons-pack';
import * as knobs from '@storybook/addon-knobs';

// packages/{componentName}/index.ts
const req = require.context('../packages', true, /^\.\/(.*)\/index.ts$/);

const components = req.keys().reduce((acc: Record<string, ComponentType>, key) => {
    Object.entries(req(key)).forEach(([componentName, component]: [string, any]) => {
        acc[componentName] = component;
    });

    return acc;
}, {});

export default {
    ...components,
    ...icons,
    ...knobs,
};

// .storybook/manager.js

import scope from './scope';

addons.setConfig({
    [LIVE_EXAMPLES_ADDON_ID]: {
        scope,
    },
});

changelog

v2.0.3 (Thu Jul 11 2024)

⚠️ Pushed to main

  • feat: gist sharing, format code on blur (@reme3d2y)

Authors: 1


v2.0.2 (Thu Jun 15 2023)

🐛 Bug Fix

Authors: 1


v1.0.20 (Tue May 23 2023)

🐛 Bug Fix

Authors: 2


v1.0.19 (Sun May 21 2023)

🐛 Bug Fix

Authors: 2


v1.0.18 (Mon Apr 17 2023)

🐛 Bug Fix

Authors: 1


v1.0.17 (Tue Apr 04 2023)

⚠️ Pushed to main

Authors: 1


v1.0.16 (Tue Mar 28 2023)

⚠️ Pushed to main

Authors: 1


v1.0.15 (Wed Mar 22 2023)

⚠️ Pushed to main

Authors: 1


v1.0.14 (Wed Mar 22 2023)

⚠️ Pushed to main

Authors: 1


v1.0.13 (Sun Mar 19 2023)

⚠️ Pushed to main

Authors: 1


v1.0.12 (Sun Mar 19 2023)

⚠️ Pushed to main

Authors: 1


v1.0.11 (Sun Mar 19 2023)

⚠️ Pushed to main

Authors: 1


v1.0.10 (Fri Dec 09 2022)

⚠️ Pushed to main

Authors: 1


v1.0.9 (Tue Nov 22 2022)

⚠️ Pushed to main

  • feat: handleMessage, change separator (@reme3d2y)

Authors: 1


v1.0.8 (Tue Nov 01 2022)

⚠️ Pushed to main

  • fix: remove initial mobile frame render (@reme3d2y)

Authors: 1


v1.0.7 (Mon Oct 31 2022)

⚠️ Pushed to main

Authors: 1


v1.0.6 (Mon Oct 31 2022)

⚠️ Pushed to main

Authors: 1


v1.0.5 (Tue Oct 25 2022)

⚠️ Pushed to main

Authors: 1


v1.0.4 (Tue Oct 25 2022)

⚠️ Pushed to main

Authors: 1


v1.0.3 (Tue Oct 25 2022)

🐛 Bug Fix

Authors: 1


v1.0.2 (Tue Oct 25 2022)

🐛 Bug Fix

Authors: 1


v0.0.26 (Thu Oct 20 2022)

🐛 Bug Fix

Authors: 3


v0.0.25 (Mon Nov 22 2021)

⚠️ Pushed to main

Authors: 1


v0.0.24 (Mon Sep 27 2021)

⚠️ Pushed to main

  • fix: context.parameters.fileName (@reme3d2y)

Authors: 1


v0.0.23 (Mon Sep 27 2021)

⚠️ Pushed to main

Authors: 1


v0.0.22 (Mon Sep 27 2021)

⚠️ Pushed to main

  • feat: add defaultCanvas to config (@reme3d2y)

Authors: 1


v0.0.21 (Sun Sep 12 2021)

⚠️ Pushed to main

  • feat: run prettier after transpile, do not transpile non-live examples (@reme3d2y)

Authors: 1


v0.0.20 (Fri Sep 03 2021)

⚠️ Pushed to main

Authors: 1


v0.0.19 (Sun Aug 29 2021)

⚠️ Pushed to main

  • feat: add defaultCanvas param to restore default canvas (@reme3d2y)

Authors: 1


v0.0.18 (Fri Aug 27 2021)

⚠️ Pushed to main

Authors: 1


v0.0.17 (Fri Aug 27 2021)

⚠️ Pushed to main

  • fix: temporary hack fix, prevent blinking when url change (@reme3d2y)

Authors: 1


v0.0.16 (Fri Aug 27 2021)

⚠️ Pushed to main

Authors: 1


v0.0.15 (Fri Aug 27 2021)

⚠️ Pushed to main

  • fix: remove only default canvas (@reme3d2y)

Authors: 1


v0.0.14 (Fri Aug 27 2021)

⚠️ Pushed to main

  • feat: refactoring, add pure csf support (@reme3d2y)

Authors: 1


v0.0.13 (Thu Aug 26 2021)

⚠️ Pushed to main

  • fix: add code prop, fix direct usage (@reme3d2y)

Authors: 1


v0.0.12 (Thu Aug 26 2021)

⚠️ Pushed to main

Authors: 1


v0.0.11 (Thu Aug 26 2021)

⚠️ Pushed to main

Authors: 1


v0.0.10 (Thu Aug 26 2021)

⚠️ Pushed to main

Authors: 1


v0.0.9 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.8 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.7 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.6 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.5 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.4 (Wed Aug 25 2021)

⚠️ Pushed to main

  • fix: after core-components integration test (@reme3d2y)

Authors: 1


v0.0.3 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1


v0.0.2 (Wed Aug 25 2021)

⚠️ Pushed to main

Authors: 1