-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
248 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
examples/pigment-css-vite-ts/src/components/ColorSchemeProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as React from 'react'; | ||
|
||
const ColorSchemeContext = React.createContext<{ | ||
colorScheme: string; | ||
setColorScheme: React.Dispatch<React.SetStateAction<string>>; | ||
}>({ | ||
colorScheme: 'light', | ||
setColorScheme: () => '', | ||
}); | ||
|
||
function setCookie(name: string, value: string, days: number = 100) { | ||
let expires = ''; | ||
if (days) { | ||
const date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = `; expires=${date.toUTCString()}`; | ||
} | ||
document.cookie = `${name}=${value || ''}${expires}; path=/`; | ||
} | ||
|
||
export function ColorSchemeProvider({ | ||
colorScheme: initialColorScheme, | ||
children, | ||
}: React.PropsWithChildren<{ colorScheme: string }>) { | ||
const [colorScheme, setColorScheme] = React.useState<string>(initialColorScheme); | ||
|
||
const contextValue = React.useMemo( | ||
() => ({ colorScheme, setColorScheme }), | ||
[colorScheme, setColorScheme], | ||
); | ||
|
||
// Set the colorScheme in localStorage | ||
React.useEffect(() => { | ||
setCookie('colorScheme', colorScheme); | ||
localStorage.setItem('colorScheme', colorScheme); | ||
}, [colorScheme]); | ||
|
||
// Handle when localStorage has changed | ||
React.useEffect(() => { | ||
const handleStorage = (event: StorageEvent) => { | ||
const value = event.newValue; | ||
if ( | ||
typeof event.key === 'string' && | ||
event.key === 'colorScheme' && | ||
typeof value === 'string' | ||
) { | ||
setColorScheme(value); | ||
} | ||
}; | ||
// For syncing color-scheme changes between iframes | ||
window.addEventListener('storage', handleStorage); | ||
return () => { | ||
window.removeEventListener('storage', handleStorage); | ||
}; | ||
}, [setColorScheme]); | ||
|
||
return <ColorSchemeContext.Provider value={contextValue}>{children}</ColorSchemeContext.Provider>; | ||
} | ||
|
||
export const useColorScheme = () => { | ||
return React.useContext(ColorSchemeContext); | ||
}; |
Oops, something went wrong.