From 5247a23cbe534d79cdf4abe4c200a351dddc5dc2 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 27 May 2021 09:16:14 -0500 Subject: Example: Docs template (#226) * fix: markdown issues * wip: add docs example * example: update doc template * chore: credit Steph for AvatarList * chore: align footer to bottom viewport * chore: feeback R1 * fix: font fallback in firefox * fix merge conflicts * fix: add default value to headers * chore: fix doc example --- examples/doc/src/components/ThemeToggle.tsx | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/doc/src/components/ThemeToggle.tsx (limited to 'examples/doc/src/components/ThemeToggle.tsx') diff --git a/examples/doc/src/components/ThemeToggle.tsx b/examples/doc/src/components/ThemeToggle.tsx new file mode 100644 index 000000000..d26d94d69 --- /dev/null +++ b/examples/doc/src/components/ThemeToggle.tsx @@ -0,0 +1,65 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; + +const themes = [ + 'system', + 'light', + 'dark' +] + +const icons = [ + + +, + + + , + + + +] + +const ThemeToggle: FunctionalComponent = () => { + const [theme, setTheme] = useState(themes[0]); + + useEffect(() => { + const user = localStorage.getItem('theme') + if (!user) return; + setTheme(user); + }, []) + + useEffect(() => { + const root = document.documentElement; + if (theme === 'system') { + localStorage.removeItem('theme'); + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + root.classList.add('theme-dark'); + } else { + root.classList.remove('theme-dark'); + } + } else { + localStorage.setItem('theme', theme); + 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