/** * The theme button: system, light and dark in turn, remembered in the browser. Its icon shows the * current choice, as on quanthea's site. */ import { useState } from 'preact/hooks'; import { applyTheme, nextTheme, storedTheme, storeTheme, type Theme } from '../lib/theme.ts'; /** What each theme is called, for the button's label. */ const themeNames: Readonly> = { light: 'light', dark: 'dark', system: 'the system’s', }; /** * The icon of a theme. * * @param props - The theme. * @returns The icon. */ function ThemeIcon(props: { readonly theme: Theme }) { if (props.theme === 'light') { return ( ); } if (props.theme === 'dark') { return ( ); } return ( ); } /** * The button. * * @returns The control. */ export function ThemeSwitch() { const [theme, setTheme] = useState(() => storedTheme(() => localStorage)); const next = nextTheme(theme); const choose = () => { setTheme(next); applyTheme(document.documentElement, next); storeTheme(() => localStorage, next); }; return ( ); }