chore: fix linting errors and ts bugs.
This commit is contained in:
@@ -16,7 +16,9 @@ import { themeLocalStorageKey } from './types'
|
||||
|
||||
export const ThemeSelector: React.FC = () => {
|
||||
const { setTheme } = useTheme()
|
||||
const [value, setValue] = useState('')
|
||||
const [value, setValue] = useState(() =>
|
||||
typeof window === 'undefined' ? 'auto' : window.localStorage.getItem(themeLocalStorageKey) ?? 'auto',
|
||||
)
|
||||
|
||||
const onThemeChange = (themeToSet: Theme & 'auto') => {
|
||||
if (themeToSet === 'auto') {
|
||||
@@ -28,10 +30,6 @@ export const ThemeSelector: React.FC = () => {
|
||||
}
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
const preference = window.localStorage.getItem(themeLocalStorageKey)
|
||||
setValue(preference ?? 'auto')
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Select onValueChange={onThemeChange} value={value}>
|
||||
|
||||
@@ -16,9 +16,14 @@ const initialContext: ThemeContextType = {
|
||||
const ThemeContext = createContext(initialContext)
|
||||
|
||||
export const ThemeProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
const [theme, setThemeState] = useState<Theme | undefined>(
|
||||
canUseDOM ? (document.documentElement.getAttribute('data-theme') as Theme) : undefined,
|
||||
)
|
||||
const [theme, setThemeState] = useState<Theme | undefined>(() => {
|
||||
if (!canUseDOM) return undefined
|
||||
|
||||
const preference = window.localStorage.getItem(themeLocalStorageKey)
|
||||
if (themeIsValid(preference)) return preference
|
||||
|
||||
return getImplicitPreference() || defaultTheme
|
||||
})
|
||||
|
||||
const setTheme = useCallback((themeToSet: Theme | null) => {
|
||||
if (themeToSet === null) {
|
||||
@@ -48,7 +53,6 @@ export const ThemeProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
}
|
||||
|
||||
document.documentElement.setAttribute('data-theme', themeToSet)
|
||||
setThemeState(themeToSet)
|
||||
}, [])
|
||||
|
||||
return <ThemeContext value={{ setTheme, theme }}>{children}</ThemeContext>
|
||||
|
||||
Reference in New Issue
Block a user