-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[system] Support for prefers-color-scheme with no flashes #15588
Comments
We currently use this media query in our docs to switch to a dark theme on the client. Since we prerender them we always deliver the version with the light theme that gets hydrated with the preferred color scheme causing a flash of outdated styles if a dark theme is preferred. As far as I know the only solution would be to use the CSS media query in our core components which would use the colors in the dark theme. This would add a lot of styles to the core for a query that isn't supported by most of our supported platforms or even some operating systems. I think it's better to write a custom theme that adds the query to all the necessary classes concerning color (which is probably a lot). I guess this is one of those instances where CSS variables would be the best solution (if they can read media queries). |
Notice the introduction of new section in the documentation: https://material-ui.com/customization/palette/#user-preference import React from 'react';
import useMediaQuery from '@material-ui/core/useMediaQuery';
import { ThemeProvider } from '@material-ui/core/styles';
function App() {
const prefersDarkMode = useMediaQuery('(prefers-color-scheme: dark)');
const theme = React.useMemo(
() =>
createMuiTheme({
palette: {
type: prefersDarkMode ? 'dark' : 'light',
},
}),
[prefersDarkMode],
);
return (
<ThemeProvider theme={theme}>
<Routes />
</ThemeProvider>
);
} This topic is close to the discussion we have in #16367. |
We have the odd situation where
The first render both fail, so it refuses to render anything, then it soon corrects and renders a logically consistent result (only one of prefers light or prefers dark) and we get no incorrectly styled flash. |
@elyobo Your workaround is equivalent to the usage of the |
I have no idea, haven't looked at any of the SSR stuff because we're not doing SSR, the issue is happening for us client side. First render both are false, then very soon after it renders in a sane way (can't prefer both light and dark). |
Super interesting, thanks for sharing, I think that we should run the update on the layout effect. +1 for a PR. |
While looking at the code to understand what you were suggesting, I noticed this const {
defaultMatches = false,
matchMedia = supportMatchMedia ? window.matchMedia : null,
noSsr = false,
ssrMatchMedia = null,
} = {
...props,
...options,
};
const [match, setMatch] = React.useState(() => {
if (noSsr && supportMatchMedia) {
return matchMedia(query).matches;
}
if (ssrMatchMedia) {
return ssrMatchMedia(query).matches;
}
// Once the component is mounted, we rely on the
// event listeners to return the correct matches value.
return defaultMatches;
}); Our problem can be solved by passing in Perhaps the PR needed is a documentation one? If you were thinking of using |
@elyobo I had this problem in mind, I would expect |
@oliviertassinari our problem is that unless you pass |
@elyobo Ok, so we might not be able to do anything about it, |
Ok if I make a PR to extend the docs for some of those options?
…On Sat, 21 Dec 2019, 08:41 Olivier Tassinari, ***@***.***> wrote:
@elyobo <https://github.com/elyobo> Ok, so we might not be able to do
anything about it, noSsr seems to be the way.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#15588>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADDR7RHJPOR3RGHGSP4OELQZU3Y5ANCNFSM4HKZBV6A>
.
|
Sure, it would help |
Hey! I just faced this issue and I had come across this blog post. The approach suggested here seems to be to use Theme-UI seems to do this and also provides a I would not mind working on this if you guys think it is a valid approach. It would be my first open source issue though so any help would be appreciated!😅 |
@HiranmayaGundu have you tried using theme-ui with material-ui? I've been trying to find examples but haven't found one. |
@bugzpodder No I have not tried that out, and I'm not entirely sure that is possible? Material-UI uses it's own theme specification. You could use |
I'm using Next.js and I've wrapped my Am I or Next.js screwing something up, or is there still not a concrete solution to this flash of unthemed changes? Another note: Using the CSS media query would be a big pain to write, but would fix this issue, as well as using dynamic themes with AMP. This article is a good read on how a Gatsby site had a dark theme implemented using CSS variables in order to prevent any FoUC. |
@pizzafox The flash comes from the reliance on JavaScript to detect the media query. To solve the flash, there are a couple of options:
|
@oliviertassinari Is it possible to allow strings like |
@HiranmayaGundu We have a running discussion about it in #12827. |
This comment has been minimized.
This comment has been minimized.
@elyobo, this is very interesting, did you find that there was an overlap of CSS style sheets on your elements ? |
I don't recall sorry @fosteman; the We set the |
Thanks for the noSsr solution. This still seems a weird behavior and at first, I thought it didn't work at all. The query gives first false, then true but it didn't render the page with dark mode (idk why). It now works, but I assume that it should work by default noSSr true and if you are using SSR you could enable it as for SPA it's a significant overhead (renders twice, or in my case idk did it actually😅). But I guess someone smarter decided it to be the other way around (no sarcasm). |
What about |
@TheBit Developers can already implement a solution relying on |
I believe that we can close this issue with the experimental API |
@siriwatknp We could close this issue, it sounds like the root of the pain point is solved, proof: https://twitter.com/MUI_hq/status/1582390058099015685. If we close, I think that we should create a new migration issue to have all the pages of https://mui.com/ no longer flash like the homepage, e.g. the docs pages. |
Yep, sounds good. We can get some help from the community to migrate the rest of the pages. |
The new issue in question: #34880. |
Is there a way to support prefers-color-scheme in the future? I guess, it would be possible for every project to make a custom implementation to support it, but maybe it could be a good idea to add support for it by default.
The CSS media feature lets you use a media query to adapt your styling to the light or dark theme requested by the system. If the user's browser is using dark theme, the application could adapt to it and use dark theme as well, instead of showing any eye-hurting white.
It can also be seen on the documentation:
Expected that the page wouldn't switch appearance mode/flash. Figured it was best to describe this FYI issue here?
Benchamark
Related
The text was updated successfully, but these errors were encountered: