fluentui-storybook-addons/storybook-addon-export-to-c...
Jiří Vyhnálek 4d26e2ae8e
fix: dependency matching for multi-line imports (#34)
* fix: Fix dependency matching for multi-line imports

* tests
2023-02-09 15:26:23 +01:00
..
.storybook docs: use full sourcecode for storybook examples 2023-02-01 19:58:48 +01:00
src fix: dependency matching for multi-line imports (#34) 2023-02-09 15:26:23 +01:00
stories docs: add example of stories with break line in source code 2023-02-01 19:54:14 +01:00
.babelrc.js add hidden files 2021-09-24 10:57:57 +02:00
.gitignore remove .env from .gitignore 2021-09-24 16:51:21 +02:00
.prettierignore add hidden files 2021-09-24 10:57:57 +02:00
CONTRIBUTING.md add contributing.md 2021-09-29 13:31:47 +02:00
README.md optional dependencies 2022-05-30 18:07:55 +02:00
jest.config.ts clean up jest config 2021-11-05 14:21:04 +01:00
package.json Release 0.8.3 (#33) 2023-02-08 17:22:08 +01:00
preset.js add export-to-codesandbox 2021-09-24 10:12:46 +02:00
tsconfig.json feat(babel-plugin): Add internal `modifyImports` babel plugin 2021-10-29 15:48:30 +02:00
yarn.lock feat: format source in snippets (#30) 2023-02-01 09:35:20 +01:00

README.md

Export to CodeSandbox

This Storybook plugin adds "Open in CodeSandbox" button to each story displayed in Docs mode.

Installation

  1. Install the plugin npm i storybook-addon-export-to-codesandbox.
  2. Register the plugin in .storybook/main.js - Add 'storybook-addon-export-to-codesandbox' to the list of addons.
// .storybook/main.js

module.exports = {
  // ...
  addons: ['storybook-addon-export-to-codesandbox' /* ... */],
};
  1. Use .storybook/babel.plugin.js in your Storybook.
  2. Define required parameters in your .storybook/preview.js:

⚠️ Make sure all necessary dependencies are included in the requiredDependencies config.

export const parameters = {
  exportToCodeSandbox: {
    // Dependencies that should be included with every story
    requiredDependencies: {
      react: 'latest',
      'react-dom': 'latest', // for React
      'react-scripts': 'latest', // necessary when using typescript in CodeSandbox
    },
    // Dependencies that should be included in the story if it exists in the code
    optionalDependencies: {
      '@fluentui/react-icons': '^9.0.0-beta',
    },
    // Content of index.tsx in CodeSandbox
    indexTsx: dedent`
            import * as ReactDOM from 'react-dom';
            import { FluentProvider, webLightTheme } from '@fluentui/react-components';
            import { STORY_NAME as Example } from './example';
            //
            // You can edit this example in "example.tsx".
            //
            ReactDOM.render(
                <FluentProvider theme={webLightTheme}>
                    <Example />
                </FluentProvider>,
                document.getElementById('root'),
            );`,
  },
};

Recommended story setup

Each story should be put into its own file with a .stories.tsx extension. This file should not contain a default export, only a single named export, which is the story. All story files should then be re-exported from a file .stories.tsx with a default export. See RFC: Authoring storybook stories for more details and examples.

This practice is recommended so that the "Open in CodeSandbox" button would export a single story.

Dependency replacement with Babel plugin

Configure dependency replacement

It's possible to further configure dependency replacement using babel plugin options. By default only dependencies that are declared in the babel plugin options will be replaced, any other dependencies will remain as is.

By default all dependencies will be replaced with @fluentui/react-components

// main.js
module.exports = {
  stories: [],
  addons: [],
  webpackFinal: config => {
    if (config.module && config.module.rules) {
      config.module.rules.unshift({
        test: /\.stories\.tsx$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            plugins: [
              [
                require('storybook-addon-export-to-codesandbox').babelPlugin,
                {
                  '@fluentui/react-button': { replace: '@fluentui/react-components' },
                  // imports of @fluentui/react-unstable-component will be replaced with @fluentui/react-components/unstable
                  '@fluentui/react-unstable-component': { replace: '@fluentui/react-components/unstable'}
                }
              ]
            ],
          },
        },
      });
    }
};


Relative imports

This step runs before any other dependency replacement

When the addon encounters relative imports in a story, the package name of the closest package.json in the file tree will be used.

{
  "name": "@fluentui/react-button",
  "version": "9.0.0-rc.11"
}
// Before
import { Button } from '../index';

// After
import { Button } from '@fluentui/react-button';