Code snippets contributions
Add or update the code snippets in the documentation. This page outlines how the code snippets are structured.
Documented frameworks
Storybook maintains code snippets for a variety of frameworks. We try to keep them up to date as framework APIs evolve. But keeping track of every API change in every framework is tricky.
We welcome community contributions to the code snippets. Here's a matrix of the frameworks for which we have snippets. Help us add snippets for your favorite framework.
React | Vue 3 | Angular | Web Components | Svelte | Solid | Ember | HTML | Preact | Qwik |
---|---|---|---|---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
Snippet syntax
The code snippets referenced throughout the Storybook documentation are located in the docs/_snippets
directory inside individual Markdown files, containing the supported frameworks, features and languages (i.e., JavaScript, MDX, TypeScript).
Example
The following code block demonstrates how to structure a code snippet in the Storybook documentation and the attributes you can use to provide additional context to the code snippet.
Common attributes for code snippets
Following are the attributes you'll use most often in the Storybook documentation code snippets, as well as a brief explanation of each to help you understand the context in which they are used.
File name as title
Most code examples should include a file name so readers can understand which file they relate to and where to paste it into their project. For code examples, include the filename
attribute wrapped with quotation marks to indicate the file name. This is not required if the example relates to a terminal command.
Language configuration
Use the language
attribute to define the language to which the code snippet applies. The documentation uses this attribute to determine which variant to display (e.g., JavaScript, TypeScript, TypeScript 4.9, MDX).
Framework-specific code
Use the renderer
attribute to indicate which of the supported frameworks the code snippet belongs to.
Alternatively, if you're documenting examples that apply to multiple frameworks, use the renderer
attribute with the common
value to indicate that the code snippet is framework-agnostic.
Package manager configuration
Use the packageManager
attribute to configure the package manager used in the example from the following options: npm
, yarn
, or pnpm
.
Working with multiple snippets
Use the tabTitle
attribute to indicate the tab title in which the code snippet will be displayed. This attribute should only be used when multiple examples are in a single code snippet file.
Contributing code snippets
You can start contributing to the Storybook documentation by now that you're familiar with how the documentation is organized, the code snippet's structure, and available options. Assuming that you have already set up your local development environment and are ready to contribute, the following steps will guide you through contributing code snippets to the Storybook documentation.
Start by creating a new branch on your local Storybook monorepo with the following command:
Browse the documentation and look for the code snippets you want to contribute. For example, on the setup page you should see the following:
Open the file inside the docs/_snippets
directory and adjust the content to match the code snippet you're willing to contribute. For example:
Go through the rest of the documentation and repeat the process.
Preview your work
Before submitting your contribution, we encourage you to check your work against the Storybook website. Doing this prevents last-minute issues with the documentation and is also an excellent way for the maintainers to merge faster once you submit the pull request. However, failing to do so will lead one of the maintainers to notify you that your contribution has an issue.
Start by forking the Storybook website repository and cloning it locally.
Navigate to the web
directory and install the required dependencies.
We recommend that you generate a website build first to ensure you can preview your changes locally and verify that everything is working as expected. To do so, run the following command:
When executed, this command will retrieve the required files needed to successfully build the Storybook website, including current documentation versions (e.g., 6.5
, 7.6
, 8.x
), and copy them to the apps/frontpage/docs/
directory, organized by version number.
Run the sync-docs
command to connect the documentation from the Storybook monorepo to the Storybook website. When prompted, provide the path to your local fork of the Storybook monorepo and the documentation version you're working on.
Finally, open a new terminal window and run the dev
command to start the Storybook website.
If all goes well, you should see the Storybook website running. Open a browser window to http://localhost:3000
, click the Docs link to open the documentation, and select your framework from the dropdown.
Go through the documentation and check your work.
Submit your contribution
Once you have verified that your changes are working as expected, you're ready to create a "Pull Request". This will let the Storybook maintainers know you have some changes to propose. At this point, we can give you feedback and request changes. To help with the review process, we encourage you to add a clear title and description of your work.
Troubleshooting
Code snippets not displaying
If you're documenting an example that includes the packageManager
attribute combined with another example, the documentation might not display the code snippets correctly. To avoid this, you can divide the examples into separate files and reference them in the documentation.
Learn more about contributing to Storybook
- RFC process for authoring feature requests
- Code for features and bug fixes
- Frameworks to get started with a new framework
- Documentation for documentation improvements, typos, and clarifications
- Examples for new snippets