Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 9x 9x 9x 9x 6x 3x 3x 9x | import { useEffect, useState } from 'react'
import {
applyTheme,
onSystemThemeChange,
readThemeChoice,
saveThemeChoice,
type ThemeChoice,
} from '@/lib/theme'
interface Theme {
choice: ThemeChoice
setChoice: (choice: ThemeChoice) => void
}
export function useTheme(): Theme {
const [choice, setChoiceState] = useState<ThemeChoice>(readThemeChoice)
useEffect(() => {
applyTheme(choice)
// Only "system" follows the OS; a fixed choice ignores it.
if (choice !== 'system') return
return onSystemThemeChange(() => applyTheme('system'))
}, [choice])
function setChoice(next: ThemeChoice): void {
saveThemeChoice(next)
setChoiceState(next)
}
return { choice, setChoice }
}
|