Skip to content

Pigment: add a "system" mode to the dark mode switch #387

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

Open
maartenschumacher opened this issue Mar 7, 2025 · 1 comment
Open

Pigment: add a "system" mode to the dark mode switch #387

maartenschumacher opened this issue Mar 7, 2025 · 1 comment
Assignees
Labels
package: system Specific to @mui/system status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@maartenschumacher
Copy link

maartenschumacher commented Mar 7, 2025

Summary

With the current colorSchemeSelector API that is available to implement a dark mode switch in a pigment app, it is possible to choose the media option which relies on the prefers-color-scheme media query, or choose class or data which enables a manual toggle. But for a "system" option, we need both the media query selector and the class/data selector (since, if the system option is selected, we fall back to the media query). I've found a workaround to achieve this, but it feels a bit hacky, so it would be nice if there is an official documented way to do this.

To be fair, the Pigment CSS docs also don't specify how to implement a "system" option to the dark mode switch, but they have a more flexible getSelector API which allows you to write something like

getSelector: (colorScheme: string, css) => {
    if (!colorScheme) return ":root";
    return {
        [`@media (prefers-color-scheme: ${colorScheme})`]: {
            ":root": css,
        },
        [`.${colorScheme}`]: css,
    };
}

which achieves the desired effect of both supporting a manual switch, and being able to fall back to the media query if the system option is chosen. So my workaround is to first create my theme with createTheme from material-ui, then pass that to the extendTheme function from @pigment-css/vite-plugin, where I can add this getSelector function. I cannot just create my theme directly with extendTheme because there is functionality in material-ui's createTheme that is missing from extendTheme. Please let me know if there is a better way to do it!

Examples

No response

Motivation

Most dark mode switches have a "system" option, enabled by default, that falls back to the browser preference. It would be great to be able to do this in Pigment apps as well.

Search keywords: pigment, dark mode

@maartenschumacher maartenschumacher added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 7, 2025
@zannager zannager added the package: system Specific to @mui/system label Mar 7, 2025
@brijeshb42 brijeshb42 transferred this issue from mui/material-ui Mar 7, 2025
@brijeshb42
Copy link
Contributor

Added it to post v1 roadmap.

@brijeshb42 brijeshb42 added this to the Post V1 changes milestone Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: system Specific to @mui/system status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants