|
| 1 | +--- |
| 2 | +title: Storybook |
| 3 | +description: Use the Storybook Addon to seamlessly open a story as a sandbox. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Callout } from 'nextra-theme-docs' |
| 7 | +import Video from '../../../../../shared-components/Video' |
| 8 | + |
| 9 | +# Storybook Addon |
| 10 | + |
| 11 | +The [@codesandbox/storybook-addon](https://github.com/codesandbox/storybook-addon) allows you to open the code from a story in a Sandbox with the click of a button. |
| 12 | + |
| 13 | +This integration allows you to transform design system documentation into interactive example. We have found this incredibly useful for interactive learning and for sharing bug reproductions. |
| 14 | + |
| 15 | +<Video src="https://assets.codesandbox.io/videos/storybook-add-on.mp4" /> |
| 16 | + |
| 17 | +## Connecting Storybook to your workspace |
| 18 | + |
| 19 | +All Sandboxes created from the Storybook addon will be created within the workspace that is listed in the [configuration](#configuration). |
| 20 | + |
| 21 | +Anyone opening a Sandbox from the addon will need to be a part of the workspace in order to access the Sandbox. |
| 22 | +Luckily organization domains make it very easy for new users to join automatically. |
| 23 | + |
| 24 | +If a workspace has an organzation domain, when a new user opens a Sandbox from a Storybook addon, they will be asked to join the workspace if they have an account with an email that matches the domain of the organization. |
| 25 | + |
| 26 | +You can find a step-by-step guide for setting a domain on the [Organization](/learn/access/organizations#creating-an-organization) documentation. |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +```js |
| 31 | +// .storybook/main.js |
| 32 | + |
| 33 | +module.exports = { |
| 34 | + // ... |
| 35 | + addons: ['@codesandbox/storybook-addon'], |
| 36 | +}; |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | +<details> |
| 41 | + <summary>Storybook configuration (required)</summary> |
| 42 | + |
| 43 | +<br /> |
| 44 | + |
| 45 | +To run the addon, you'll need to configure it in your Storybook's `.storybook/preview.js` file. |
| 46 | + |
| 47 | +```js |
| 48 | +// .storybook/preview.js |
| 49 | + |
| 50 | +const preview: Preview = { |
| 51 | + parameters: { |
| 52 | + codesandbox: { |
| 53 | + /** |
| 54 | + * @required |
| 55 | + * Workspace API key from codesandbox.io/t/permissions. |
| 56 | + * This sandbox is created inside the given workspace |
| 57 | + * and can be shared with team members. |
| 58 | + */ |
| 59 | + apiToken: <api-token>, |
| 60 | + |
| 61 | + /** |
| 62 | + * @required |
| 63 | + * Dependencies list to be installed in the sandbox. |
| 64 | + * |
| 65 | + * @note You cannot use local modules or packages since |
| 66 | + * this story runs in an isolated environment (sandbox) |
| 67 | + * inside CodeSandbox. As such, the sandbox doesn't have |
| 68 | + * access to your file system. |
| 69 | + * |
| 70 | + * Example: |
| 71 | + */ |
| 72 | + dependencies: { |
| 73 | + "@radix-ui/themes": "latest", |
| 74 | + "@myscope/mypackage": "1.0.0", |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + * @required |
| 79 | + * CodeSandbox will try to import all components by default from |
| 80 | + * the given package, in case `mapComponent` property is not provided. |
| 81 | + * |
| 82 | + * This property is useful when your components imports are predictable |
| 83 | + * and come from a single package and entry point. |
| 84 | + */ |
| 85 | + fallbackImport: "@radix-ui/themes", |
| 86 | + |
| 87 | + /** |
| 88 | + * @optional |
| 89 | + * All required providers to run the sandbox properly, |
| 90 | + * such as themes, i18n, store, and so on. |
| 91 | + * |
| 92 | + * @note Remember to use only the dependencies listed above. |
| 93 | + * |
| 94 | + * Example: |
| 95 | + */ |
| 96 | + provider: `import { Theme } from "@radix-ui/themes"; |
| 97 | + import '@radix-ui/themes/styles.css'; |
| 98 | +
|
| 99 | + export default ThemeProvider = ({ children }) => { |
| 100 | + return ( |
| 101 | + <Theme> |
| 102 | + {children} |
| 103 | + </Theme> |
| 104 | + ) |
| 105 | + }`, |
| 106 | + }, |
| 107 | + }, |
| 108 | +}; |
| 109 | + |
| 110 | +export default preview; |
| 111 | +``` |
| 112 | + |
| 113 | +</details> |
| 114 | + |
| 115 | +<details> |
| 116 | + <summary>Story configuration (recommended)</summary> |
| 117 | + |
| 118 | +````ts |
| 119 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 120 | + |
| 121 | +const meta: Meta<typeof Button> = { |
| 122 | + title: "Example/Button", |
| 123 | + component: Button, |
| 124 | + parameters: { |
| 125 | + codesandbox: { |
| 126 | + /** |
| 127 | + * To import all components used within each story in |
| 128 | + * CodeSandbox, provide all necessary packages and modules. |
| 129 | + * |
| 130 | + * Given the following story: |
| 131 | + * ```js |
| 132 | + * import Provider from "@myscope/mypackage"; |
| 133 | + * import { Button } from "@radix-ui/themes"; |
| 134 | + * import "@radix-ui/themes/styles.css"; |
| 135 | + * ``` |
| 136 | + * |
| 137 | + * You need to map all imports to the following: |
| 138 | + */ |
| 139 | + mapComponent: { |
| 140 | + // Example of default imports |
| 141 | + "@myscope/mypackage": "Provider", |
| 142 | + |
| 143 | + // Example of named functions |
| 144 | + "@radix-ui/themes": ["Button"], |
| 145 | + |
| 146 | + // Example of static imports |
| 147 | + "@radix-ui/themes/styles.css": true, |
| 148 | + }, |
| 149 | + |
| 150 | + /** |
| 151 | + * @note You cannot use local modules or packages since |
| 152 | + * this story runs in an isolated environment (sandbox) |
| 153 | + * inside CodeSandbox. As such, the sandbox doesn't have |
| 154 | + * access to your file system. |
| 155 | + */ |
| 156 | + }, |
| 157 | + }, |
| 158 | +}; |
| 159 | +```` |
| 160 | + |
| 161 | +</details> |
| 162 | + |
| 163 | +<br /> |
| 164 | + |
| 165 | +Make sure to provide the necessary values for [`apiToken`](https://codesandbox.io/t/permissions) and any additional dependencies or providers required for your specific setup. |
| 166 | + |
| 167 | +For now, this addon only supports the [Component Story Format (CSF)](https://storybook.js.org/docs/api/csf) stories format. |
| 168 | + |
| 169 | +### Additional Notes |
| 170 | + |
| 171 | +- Ensure that you have proper permissions and access rights to the CodeSandbox workspace specified in the configuration. |
| 172 | +- Verify the correctness of the dependencies and providers listed in the configuration to ensure the sandbox runs smoothly. |
0 commit comments