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

Set initial mode based on users prefered color scheme #165

Open
nikkipantony opened this issue Oct 14, 2021 · 2 comments
Open

Set initial mode based on users prefered color scheme #165

nikkipantony opened this issue Oct 14, 2021 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@nikkipantony
Copy link
Contributor

nikkipantony commented Oct 14, 2021

I noticed that the initial mode (light or dark) is hard set using current:.

This means that if a user has their prefered color scheme set to dark but it is set to light using current: Storybook will still render in light mode before being changed by the user.

My solution to detect the users prefered color scheme is:

// .storybook/preview.js

import { themes } from "@storybook/theming"

// Detect if users preferred color scheme is dark
const isDark =
    typeof window !== `undefined`
        ? window.matchMedia &&
          window.matchMedia("(prefers-color-scheme: dark)").matches
        : null

export const parameters = {
    darkMode: {
        // Set initial theme based on users preferred color scheme
        current: isDark ? "dark" : "light",

I wanted to share this solution incase it helps anyone else out.

But also wanted to check if anyone has any better solution they could share?
or if this could somehow be added to the addon?

@hipstersmoothie
Copy link
Owner

@nikkipantony Would you be willing to submit a PR? In my mind if the user changes it back to system settings from then on it should match system settings

@hipstersmoothie hipstersmoothie added the help wanted Extra attention is needed label Dec 5, 2022
@beltranrengifo
Copy link
Contributor

I addressed a related matter in this fix. I opted not to utilize the OS color schema over current prop because I believe it's important for us to maintain a bit of control over the color scheme of our Storybook, rather than totally relying on the user's preferences. This decision ensures that we can serve the Storybook with the specific schema we desire

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants