UNPKG

3.01 kBMarkdownView Raw
1# storybook-addon-code
2
3[![NPM](https://img.shields.io/npm/l/%40jls-digital%2Fstorybook-addon-code)](https://github.com/jls-digital/storybook-addon-code/blob/main/LICENSE)
4[![npm](https://img.shields.io/npm/v/%40jls-digital%2Fstorybook-addon-code)](https://www.npmjs.com/package/@jls-digital/storybook-addon-code)
5[![install size](https://packagephobia.com/badge?p=@jls-digital/storybook-addon-code)](https://packagephobia.com/result?p=@jls-digital/storybook-addon-code)
6
7This storybook addon provides the ability to display a panel on stories with any
8code you want. It's mostly a wrapper for
9[storybook's syntax highlighter](https://github.com/storybookjs/storybook/tree/main/code/ui/components/src/components/syntaxhighlighter)
10, which is a wrapper for [prism.js](https://www.npmjs.com/package/prismjs).
11
12![Screenshot of Addon](docs/screenshot_1.png)
13
14## Installation
15
161. `npm install --save-dev @jls-digital/storybook-addon-code`
172. Add `storybook-addon-code` to your storybook's config
183. Add the `sourceCode` parameter to your stories
19
20```ts
21// .storybook/main.ts
22const config: StorybookConfig = {
23 addons: ['@jls-digital/storybook-addon-code'],
24};
25```
26
27## Usage
28
29In your story files, you can now import code as string (with vite by using the
30'?raw' query parameter) and pass it to the `sourceCode` parameter. You can even
31import the file you're currently in:
32
33```ts
34// button.stories.ts
35import rawStories from './button.stories?raw';
36import rawComponent from './button?raw';
37const meta: Meta<IButtonStory> = {
38 parameters: {
39 sourceCode: [
40 { name: 'Component', code: rawComponent },
41 { name: 'Stories', code: rawStories },
42 ]
43 },
44 title: 'Components/Button',
45}
46```
47
48### Arguments
49
50| Name | Description | Values |
51| --- | --- | --- |
52| `name` | The name of the sub tab. Default: index of tab. | `string` |
53| `code` | The code to display | `string` |
54| `language` | The language of the code that should be used for syntax highlighting. Default: `typescript`. | "jxextra", "jsx", "json", "yml", "md", "bash", "css", "html", "tsc", "typescript", "graphql" |
55
56## FAQ
57
58(Click on a question to see the answer.)
59
60<details>
61<summary><b>Q: When I add a new file to the list of source code, I get an error saying
62"The final argument passed to useMemo changed size between renders."</b></summary>
63
64A: No worries, this is an issue with storybook's TabsState component. You can
65simply reload the page and it should work fine.
66</details>
67
68<details>
69<summary><b>Q: Can I customize the order of my tabs?</b></summary>
70
71A: Yes, the order is determined by the order of the array you pass to the
72`sourceCode` parameter.
73</details>
74
75## Contributing
76
77We encourage you to contribute to this project! Please check out the
78[Contributing guide](CONTRIBUTING.md) for guidelines about how to proceed and
79how to set up your dev environment. Join us!
80
81## License
82
83Storybook-Addon-Code is released under the [MIT License](LICENSE).