-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
feat: sync color mode between browser tabs #6723
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,8 @@ type ColorModeContextValue = { | |
readonly setDarkTheme: () => void; | ||
}; | ||
|
||
const ThemeStorage = createStorageSlot('theme'); | ||
const ThemeStorageKey = 'theme'; | ||
const ThemeStorage = createStorageSlot(ThemeStorageKey); | ||
|
||
const themes = { | ||
light: 'light', | ||
|
@@ -45,7 +46,7 @@ const getInitialTheme = (defaultMode: Themes | undefined): Themes => { | |
}; | ||
|
||
const storeTheme = (newTheme: Themes) => { | ||
createStorageSlot('theme').set(coerceToTheme(newTheme)); | ||
ThemeStorage.set(coerceToTheme(newTheme)); | ||
}; | ||
|
||
function useColorModeContextValue(): ColorModeContextValue { | ||
|
@@ -69,17 +70,25 @@ function useColorModeContextValue(): ColorModeContextValue { | |
|
||
useEffect(() => { | ||
if (disableSwitch) { | ||
return; | ||
return undefined; | ||
} | ||
|
||
try { | ||
const storedTheme = ThemeStorage.get(); | ||
if (storedTheme !== null) { | ||
setTheme(coerceToTheme(storedTheme)); | ||
const onChange = (e: StorageEvent) => { | ||
if (e.key !== ThemeStorageKey) { | ||
return; | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
try { | ||
const storedTheme = ThemeStorage.get(); | ||
if (storedTheme !== null) { | ||
setTheme(coerceToTheme(storedTheme)); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a good candidate for |
||
} | ||
}; | ||
window.addEventListener('storage', onChange); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's going to be a problem but it's worth double-checking if this storage event listener could throw in some cases? (like iframe + blocking cookies, see #4501) |
||
return () => { | ||
window.removeEventListener('storage', onChange); | ||
}; | ||
}, [disableSwitch, setTheme]); | ||
|
||
// PCS is coerced to light mode when printing, which causes the color mode to | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I have mitigated feeling because we now have the toggle state in 2 places: localstorage + react state
but it's probably fine because localstorage can't be considered reliable in all cases (and we want the toggle to keep working on such cases)
that should be fine for now 👍 but I have some refactoring ideas for later