-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
161 additions
and
47 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
import {useCallback, useEffect, useState} from 'react' | ||
import {useSearchParams} from '@remix-run/react' | ||
|
||
const THEME = 'theme' | ||
const supported = new Set(['dark', 'light', undefined]) | ||
const isSupported = (theme: string | null | undefined): theme is string | undefined => | ||
theme !== null && supported.has(theme) | ||
|
||
export const useTheme = () => { | ||
const [searchParams] = useSearchParams() // url parameter for iframe usage: ...?embedded&theme=dark | ||
const [savedTheme, setSavedTheme] = useState(() => { | ||
const fromParams = searchParams.get(THEME) | ||
if (isSupported(fromParams)) return fromParams | ||
|
||
if (typeof window === 'undefined') return | ||
const fromStorage = window.localStorage.getItem(THEME) | ||
if (isSupported(fromStorage)) return fromStorage | ||
}) | ||
|
||
// remember UI theme toggle state in localStorage | ||
const setStorageTheme = useCallback((theme: string | undefined) => { | ||
if (isSupported(theme)) { | ||
if (!theme) { | ||
window.localStorage.removeItem(THEME) | ||
} else { | ||
window.localStorage.setItem(THEME, theme) | ||
} | ||
setSavedTheme(theme) | ||
} | ||
}, []) | ||
|
||
// if no preference is saved, use the system preferences | ||
useEffect(() => { | ||
if (savedTheme) return | ||
|
||
const media = window.matchMedia('(prefers-color-scheme: dark)') | ||
const updateHtmlClass = () => { | ||
const classList = document.documentElement.classList | ||
if (media.matches) { | ||
classList.remove('light') | ||
classList.add('dark') | ||
} else { | ||
classList.remove('dark') | ||
classList.add('light') | ||
} | ||
} | ||
media.addEventListener('change', updateHtmlClass) | ||
updateHtmlClass() | ||
|
||
return () => media.removeEventListener('change', updateHtmlClass) | ||
}, [savedTheme]) | ||
|
||
return {savedTheme, setStorageTheme} | ||
} |
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