question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add theme and global style for Documentation-only MDX

See original GitHub issue

I’m having trouble applying theme and global styles from styled-components for Documentation-only MDX. I followed the instruction written here to create a Documentation-only MDX. The problems is that since I don’t use <Story></Story> to wrap around my components, I can’t seem to apply the global styles using decorators.

Here is what I have to set decorators:

export const decorators = [
    Story => (
        <ThemeProvider theme={theme}>
            <GlobalStyle />
            <div style={{ backgroundColor: "#000" }}>
                <Story />
            </div>
        </ThemeProvider>
    ),
];

and here is an example of my component in MDX:

<Canvas>
    <Heading variant="h2">H2 Heading</Heading>
</Canvas>

The global styles does get applied if I write:

<Canvas>
    <Story name="h2">
        <Heading variant="h2">H2 Heading</Heading>
    </Story>
</Canvas>

Is there anyway to apply the theme and global style without having the <Story> tag?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ytakayanagicommented, Aug 22, 2020

For those who might be wondering how it is achieved at the end, here is my example:

import React from "react";
import { DocsContainer } from "@storybook/addon-docs/blocks";
import { ThemeProvider } from "styled-components";
import { GlobalStyle } from "../lib/utils/global";
import { theme } from "../lib/utils/theme";
import "normalize.css";

export const parameters = {
    docs: {
        container: ({ children, context }) => (
            <DocsContainer context={context}>
                <ThemeProvider theme={theme}>
                    <GlobalStyle />
                    {children}
                </ThemeProvider>
            </DocsContainer>
        ),
    },
};
2reactions
shilmancommented, Aug 22, 2020

Sure. You can export parameters globally from .storybook/preview.js

export const parameters = { docs: { container: .... } }

https://storybook.js.org/docs/react/writing-stories/parameters#global-parameters

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow MDX theming #12817 - storybookjs/storybook - GitHub
This would allow for fewer side effects from the global styles and ... #346 Make mdx docs readable for dark theme by applying...
Read more >
Styling MDX Content - Theme UI
The theme.styles object combined with useThemeStylesWithMdx hook is the primary way to style content in MDX documents. This allows you to add typographic ......
Read more >
Creating your themes - Docz
The themeConfig allows you to customize fonts, colors, spaces, styles properties and other project global variables. // src/gatsby-theme-docz/index.js import ...
Read more >
Style Components Rendered By MDX with Theme UI in Gatsby
Theme UI provides a wrapper around the MDXProvider so you can add styling to components that are rendered in your MDX documents.
Read more >
MDX Format - Storybook - JS.ORG
MDX Format. MDX is the syntax Storybook Docs uses to capture long-form Markdown documentation and stories in one file. You can also write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found