You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, next-themes has two exclusive behaviours:
Respect system theme preference changes, i.e. set theme based on device preference.
Respect the theme set via setTheme from the useTheme() hook.
If the theme has been set using setTheme, the application's theme will no longer be automatically updated when the system's theme changes, e.g. if the user's device changes from dark mode to light mode.
It would be great if you could add the option to always respect system theme preference changes, even after setTheme has been called, for example:
Step
Theme
1. Application opened with 'light mode' system theme
This also seems to be the default behaviour of the useDarkMode package (unmaintained):
Changing the system dark mode state will also change the state of useDarkMode (i.e, change to light mode in the system will change to light mode in your app).
Workaround
A workaround is to manually call setTheme('system') when the device theme changes:
const{ setTheme }=useTheme();consthandleSystemThemeChange=useCallback(()=>setTheme('system'),[setTheme]);// Copied from src/index.tsxuseEffect(()=>{constmedia=window.matchMedia('(prefers-color-scheme: dark)');// Intentionally use deprecated listener methods to support iOS & old browsersmedia.addListener(handleSystemThemeChange);return()=>media.removeListener(handleSystemThemeChange);},[handleSystemThemeChange]);
@pacocoursey I think this would be a great addition. What do you think about it?
If you think you and the community would welcome it and you would merge an implementation of this feature I would try to implement it
Currently,
next-themes
has two exclusive behaviours:setTheme
from theuseTheme()
hook.If the theme has been set using
setTheme
, the application's theme will no longer be automatically updated when the system's theme changes, e.g. if the user's device changes from dark mode to light mode.It would be great if you could add the option to always respect system theme preference changes, even after
setTheme
has been called, for example:'light'
setTheme('dark')
'dark'
'dark'
'light'
A good example of this behaviour is the dark/light mode toggle on @joshwcomeau's blog:
https://www.joshwcomeau.com
This also seems to be the default behaviour of the useDarkMode package (unmaintained):
Workaround
A workaround is to manually call
setTheme('system')
when the device theme changes:Or by using the provided
systemTheme
value:The problem with these workarounds is the theme is always reverted to the system them when reloading the application.
The text was updated successfully, but these errors were encountered: