Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to link to a story with dark theme set #242

Open
Meemaw opened this issue Aug 2, 2023 · 1 comment
Open

Possibility to link to a story with dark theme set #242

Meemaw opened this issue Aug 2, 2023 · 1 comment

Comments

@Meemaw
Copy link

Meemaw commented Aug 2, 2023

Is it possible to somehow link to a story that would have dark mode predefined? e.g via some kind of query parameter.

@d9k
Copy link

d9k commented Nov 18, 2023

Excerpt from my notes:

I think, yes,

1. you can add global control to .storybook/preview.ts: globalTypes which adds URL parameter when you click the switch (all custom global controls changes URL params).

See 🎈 demo with such control having &globals=theme:dark in URL

2. Then you can write custom global storybook decorator to .storybook/preview.ts: decorators with useEffect which will emit storybook-dark-mode action to sync dark state with your custom storybook global control value (see README):

import { useDarkMode } from 'storybook-dark-mode';

// . . .
decorators: [
  (Story, context) => {
   // if we added "theme" global variable earlier
   const theme = context.globals.theme;
   const darkMode = useDarkMode();

    useEffect(() => {
      const darkModeFromTheme = theme === 'dark';
      if (darkModeFromTheme != darkMode) {
        channel.emit(UPDATE_DARK_MODE_EVENT_NAME);
      }
    }, [theme]);

    return <Story />;
  },

  // ...rest decorators
]

(didn't test the snippet, just an idea. If you will succeed please share working code 🙂)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants