From b7ada11ddbabe4dd2f0798e140e5b280de4d6952 Mon Sep 17 00:00:00 2001 From: Okiki Date: Sat, 14 Aug 2021 00:58:00 +0000 Subject: WIP update examples/docs/ --- .../components/RightSidebar/ThemeToggleButton.tsx | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/docs/src/components/RightSidebar/ThemeToggleButton.tsx (limited to 'examples/docs/src/components/RightSidebar/ThemeToggleButton.tsx') diff --git a/examples/docs/src/components/RightSidebar/ThemeToggleButton.tsx b/examples/docs/src/components/RightSidebar/ThemeToggleButton.tsx new file mode 100644 index 000000000..75ea775f4 --- /dev/null +++ b/examples/docs/src/components/RightSidebar/ThemeToggleButton.tsx @@ -0,0 +1,71 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; +import './ThemeToggleButton.css'; + +const themes = ['light', 'dark']; + +const icons = [ + + + , + + + , +]; + +const ThemeToggle: FunctionalComponent = () => { + const [theme, setTheme] = useState(() => { + if (import.meta.env.SSR) { + return undefined; + } + if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) { + return localStorage.getItem('theme'); + } + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + return 'dark'; + } + return 'light'; + }); + + useEffect(() => { + const root = document.documentElement; + if (theme === 'light') { + root.classList.remove('theme-dark'); + } else { + root.classList.add('theme-dark'); + } + }, [theme]); + + return ( +
+ {themes.map((t, i) => { + const icon = icons[i]; + const checked = t === theme; + return ( + + ); + })} +
+ ); +}; + +export default ThemeToggle; -- cgit v1.2.3