Created
June 6, 2026 01:43
-
-
Save doubleedesign/6ff3645f081ae219edbf5d5d2e7f3dd1 to your computer and use it in GitHub Desktop.
Wrap Storybook MDX docs in a decorator to conditionally load docs-only CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* .storybook/docs.css */ | |
| /* Your custom docs-only styles here */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // .storybook/preview.tsx | |
| const preview = { | |
| // ...other preview config here | |
| docs: { | |
| inlineStories: false, // put stories into an iframe | |
| // The preview.decorators array does not apply to MDX files, but we can wrap them in decorators here | |
| container: ({ children, context }) => withDocsStylesMaybe(() => ( | |
| <DocsContainer context={context}> | |
| <div className="sb-unstyled" data-typography-mode="docs"> | |
| {children} | |
| </div> | |
| </DocsContainer> | |
| ), context) | |
| }, | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // .storybook/decorators/withDocsStylesMaybe.tsx | |
| export const withDocsStylesMaybe = (Story, context) => { | |
| if(context?.channel?.data?.storySpecified?.[0]?.viewMode === 'docs') { | |
| // it is actually there, dunno why TypeScript is confused | |
| // @ts-expect-error TS2307: Cannot find module ../docs.css or its corresponding type declarations. | |
| import('../docs.css').then((result) => { | |
| // do nothing | |
| }).catch((error) => { | |
| console.error('Error loading docs.css:', error); | |
| }); | |
| } | |
| return <Story />; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment