Fix bug in colorMode when doing SSR
This commit is contained in:
parent
67d4d84094
commit
a973f8a8c6
1 changed files with 6 additions and 3 deletions
|
@ -19,7 +19,10 @@ const effectiveColorModeState = derived(colorModeState, ($mode) => {
|
|||
return mediaMatcher.matches ? 'dark' : 'light';
|
||||
});
|
||||
|
||||
const colorBc = new BroadcastChannel('raider-color-mode');
|
||||
const colorBc =
|
||||
typeof window.BroadcastChannel !== 'undefined'
|
||||
? new BroadcastChannel('raider-color-mode')
|
||||
: undefined;
|
||||
|
||||
export const saveColorMode = (newMode: ColorMode) => {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
|
@ -28,10 +31,10 @@ export const saveColorMode = (newMode: ColorMode) => {
|
|||
|
||||
helperColorMode = newMode;
|
||||
colorModeState.set(newMode);
|
||||
colorBc.postMessage(newMode);
|
||||
colorBc?.postMessage(newMode);
|
||||
};
|
||||
|
||||
colorBc.addEventListener('message', (ev) => {
|
||||
colorBc?.addEventListener('message', (ev) => {
|
||||
const mode = ev.data as ColorMode;
|
||||
|
||||
helperColorMode = mode;
|
||||
|
|
Loading…
Add table
Reference in a new issue