chore: fix linting errors and ts bugs.

This commit is contained in:
syntaxbullet
2026-06-17 17:41:21 +02:00
parent 3e04a3c7e3
commit 51494e25a8
41 changed files with 285 additions and 251 deletions

View File

@@ -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}>

View File

@@ -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>