-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Fix vite example & add dark mode support (#340)
- Loading branch information
Showing
7 changed files
with
234 additions
and
131 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
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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
import * as React from 'react'; | ||
|
||
const ColorSchemeContext = React.createContext<{ | ||
colorScheme: string; | ||
setColorScheme: React.Dispatch<React.SetStateAction<string>>; | ||
}>({ | ||
colorScheme: 'light', | ||
setColorScheme: () => '', | ||
}); | ||
|
||
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(() => { | ||
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.