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

[system] useColorScheme should return null during hydration #41918

Open
oliviertassinari opened this issue Apr 16, 2024 · 4 comments
Open

[system] useColorScheme should return null during hydration #41918

oliviertassinari opened this issue Apr 16, 2024 · 4 comments
Assignees
Labels
breaking change package: pigment-css Specific to @pigment-css/* package: system Specific to @mui/system status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 16, 2024

Steps to reproduce

No response

Current behavior

useColorScheme() returns a default value during hydration leading to confusion: it's not accurate and prevents combinatory logic like below.

Expected behavior

I would expect it to return null, see:

// TODO replace with useColorScheme once all pages support css vars
const { mode, systemMode, setMode } = useColorSchemeShim();
const calculatedMode = mode === 'system' ? systemMode : mode;
const theme = useTheme();
// Server-side hydration
if (mode === null) {
return <IconButton color="primary" disableTouchRipple />;
}

for a use case.

Context

I saw this in #41223.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: -

@oliviertassinari oliviertassinari added breaking change package: system Specific to @mui/system status: waiting for maintainer These issues haven't been looked at yet by a maintainer package: pigment-css Specific to @pigment-css/* labels Apr 16, 2024
@yamahmed
Copy link

Hey Olivier, i suggest we create a custom hook that provides more control over the initialization process. i'll provide an example of how to do it

@yamahmed
Copy link

Capture d’écran 2024-04-23 193500

@yamahmed
Copy link

Capture d’écran 2024-04-23 193542

@yamahmed
Copy link

In this custom hook 'useAccurateColorScheme', we use the useState and useEffect hooks to manage the state of the accurate color scheme. The useEffect hook runs after the component mounts and fetches the accurate color scheme (e.g., from local storage) and updates the state accordingly.

Then, in your component, you can use useAccurateColorScheme to retrieve the accurate color scheme and perform combinatory logic based on it. This ensures that the color scheme used in your component reflects the accurate value after hydration, preventing confusion caused by inaccurate default values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change package: pigment-css Specific to @pigment-css/* 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