docs/api/doc-blocks/doc-block-source.mdx
The Source block is used to render a snippet of source code directly.
import { Meta, Source } from '@storybook/addon-docs/blocks';
import * as ButtonStories from './Button.stories';
<Meta of={ButtonStories} />
<Source of={ButtonStories.Primary} />
import { Source } from '@storybook/addon-docs/blocks';
ℹ️ Like most blocks, the Source block is configured with props in MDX. Many of those props derive their default value from a corresponding parameter in the block's namespace, parameters.docs.source.
The following language configurations are equivalent:
<Source of={ButtonStories.Basic} language="tsx" />
The example above applied the parameter at the story level, but it could also be applied at the component (or meta) level or project level.
</details>codeType: string
Default: parameters.docs.source.code
Provides the source code to be rendered.
import { Meta, Source } from '@storybook/addon-docs/blocks';
import * as ButtonStories from './Button.stories';
<Meta of={ButtonStories} />
<Source code={`const thisIsCustomSource = true;
if (isSyntaxHighlighted) {
console.log('syntax highlighting is working');
}`} />
darkType: boolean
Default: parameters.docs.source.dark
Determines if the snippet is rendered in dark mode.
<Callout variant="info" icon="💡">Light mode is only supported when the Source block is rendered independently. When rendered as part of a Canvas block—like it is in autodocs—it will always use dark mode.
<IfRenderer renderer={['angular', 'react', 'html', 'web-components' ]}>
excludeDecoratorsType: boolean
Default: parameters.docs.source.excludeDecorators
Determines if decorators are rendered in the source code snippet. </IfRenderer>
languageType:
'jsextra' | 'jsx' | 'json' | 'yml' | 'md' | 'bash' | 'css' | 'html' | 'tsx' | 'typescript' | 'graphql'
Default: parameters.docs.source.language or 'jsx'
Specifies the language used for syntax highlighting.
ofType: Story export
Specifies which story's source is rendered.
transformType: (code: string, storyContext: StoryContext) => string | Promise<string>
Default: parameters.docs.source.transform
An async function to dynamically transform the source before being rendered, based on the original source and any story context necessary. The returned string is displayed as-is.
If both code and transform are specified, transform will be ignored.
This example shows how to use Prettier to format all source code snippets in your documentation. The transform function is applied globally through the preview configuration, ensuring consistent code formatting across all stories.
typeType: 'auto' | 'code' | 'dynamic'
Default: parameters.docs.source.type or 'auto'
Specifies how the source code is rendered.
render function accepts args inputs and dynamic is supported by the framework in use; otherwise same as codecode prop, otherwise renders static story sourceNote that dynamic snippets will only work if the story uses args and the Story block for that story is rendered along with the Source block.