Problems to implement a specific behaviour of color-scheme #1584
-
I am currently struggeling to implement the following behaviour: I want to read the current browser-preference (done via useColorScheme()) to use this as color-scheme. The first problems that occurs is that "listening" via useEffect() (see code-below) in the dependency-list has a noticable-delay. So if the browser is set to dark-mode and my app is started for the first time, useEffect is called noticable later. This results in a "flickering" from light to dark.
My app should always first use the browser default settings. If the color-scheme is changed via a switch in the application, this value should be used and stored in the browser-storage. If the setting was changed for the first time, this value should always be used (and therefore is the prefered color-scheme over the browser-settings). And last but not least, this setting should again be overwritten when the browser-preference is changed once again. So as summary: the app should always respect the user-settings set in the browser-preferences but when the user changes the color-scheme in the app itself this settings should prioritized. Can somebody help me with the combination of these 3 hooks involved (useColorScheme, useToggle, useLocalStorage)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
To avoid flickering, you need to store value in cookie – https://mantine.dev/theming/dark-theme/#save-color-scheme-in-cookie |
Beta Was this translation helpful? Give feedback.
To avoid flickering, you need to store value in cookie – https://mantine.dev/theming/dark-theme/#save-color-scheme-in-cookie
There is no way of knowing user preferred color scheme with initial request, so you will have to save this value in a cookie on initial render and then use this value for next requests. There is no way to avoid initial flickering if you want to rely on user preferred color scheme, I can only advise to pick color scheme that you like and use it as initial value and then let user decide which color scheme he wants.