Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created March 17, 2022 08:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/2414c46b8a561b7416475bffce750178 to your computer and use it in GitHub Desktop.
Save fedek6/2414c46b8a561b7416475bffce750178 to your computer and use it in GitHub Desktop.
Sorybook with dark mode support for docs (using stitches, but this can be edited)
import React from "react";
import { DocsContainer as BaseContainer } from "@storybook/addon-docs/blocks";
import { useDarkMode } from "storybook-dark-mode";
import { themes } from "@storybook/theming";
export const DocsContainer = ({ children, context }) => {
const dark = useDarkMode();
return (
<BaseContainer
context={{
...context,
storyById: (id) => {
const storyContext = context.storyById(id);
return {
...storyContext,
parameters: {
...storyContext?.parameters,
docs: {
...storyContext?.parameters?.docs,
theme: dark ? themes.dark : themes.light,
},
},
};
},
}}
>
{children}
</BaseContainer>
);
};
import React from "react";
import { useDarkMode } from "storybook-dark-mode";
import { themes } from "@storybook/theming";
import { darkTheme } from "@retrolove-games/ui-themes";
import { DocsContainer } from './DocsContainer';
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
viewMode: "docs",
docs: {
// theme: themes.dark,
container: DocsContainer,
},
};
export const decorators = [
(Story) => (
<div className={useDarkMode() ? darkTheme.className : "light"}>
<Story />
</div>
),
];
@CalebBarnes
Copy link

CalebBarnes commented Sep 10, 2023

In StoryBook 7^, addon-docs has been moved into storybook and it seems context has changed for DocsContainer.

I updated import to this:

import { DocsContainer } from "@storybook/blocks";
import { themes } from "@storybook/theming";

and the docs param:

    docs: {
      container: ({ children, context }) => {
        return (
          <DocsContainer
            context={context}
            theme={useDarkMode() ? themes.dark : themes.normal}
          >
            {children}
          </DocsContainer>
        );
      },
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment