|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import Moon from "./moon"; |
| 3 | +import Sun from "./sun"; |
| 4 | + |
| 5 | +const resolveColorScheme = (isDark) => (isDark ? "dark" : "light"); |
| 6 | + |
| 7 | +export default function ThemeToggle() { |
| 8 | + // This is required to prevent the animation from playing on |
| 9 | + // initial mount as well as update the aria-label. |
| 10 | + const changedRef = useRef(false); |
| 11 | + const [colorScheme, setColorScheme] = useState(null); |
| 12 | + const handleChange = useCallback( |
| 13 | + (ev) => { |
| 14 | + const colorScheme = resolveColorScheme(ev.target.checked); |
| 15 | + if (!changedRef.current) { |
| 16 | + changedRef.current = true; |
| 17 | + } |
| 18 | + setColorScheme(colorScheme); |
| 19 | + // Notify the document so it will update the theme. |
| 20 | + // Refer to _document.js for implementation. |
| 21 | + window.dispatchEvent( |
| 22 | + new CustomEvent("colorSchemeChange", { |
| 23 | + detail: { colorScheme, persist: true }, |
| 24 | + }) |
| 25 | + ); |
| 26 | + }, |
| 27 | + [setColorScheme] |
| 28 | + ); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const query = window.matchMedia("(prefers-color-scheme: dark)"); |
| 32 | + const listener = ({ matches }) => { |
| 33 | + setColorScheme(resolveColorScheme(matches)); |
| 34 | + }; |
| 35 | + |
| 36 | + query.addEventListener("change", listener); |
| 37 | + |
| 38 | + // Sets the initial color scheme based on user's preference. |
| 39 | + // Doing this here so that it's ran on client side only. |
| 40 | + setColorScheme( |
| 41 | + resolveColorScheme(document.documentElement.classList.contains("dark")) |
| 42 | + ); |
| 43 | + |
| 44 | + return () => { |
| 45 | + query.removeEventListener("change", listener); |
| 46 | + }; |
| 47 | + }, [setColorScheme]); |
| 48 | + const hasThemeChanged = changedRef.current; |
| 49 | + |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <input |
| 53 | + type="checkbox" |
| 54 | + title="Toggle between light & dark modes" |
| 55 | + // Screen readers should announce changes to aria-label |
| 56 | + // It's set to auto for the first time since the user didn't |
| 57 | + // actually change it and we set the theme automatically based |
| 58 | + // on preference. |
| 59 | + aria-label={hasThemeChanged ? colorScheme : "auto"} |
| 60 | + aria-live="polite" |
| 61 | + id="theme-toggle" |
| 62 | + className={`sr-only peer`} |
| 63 | + onChange={handleChange} |
| 64 | + data-changed={hasThemeChanged} |
| 65 | + checked={colorScheme === "dark"} |
| 66 | + /> |
| 67 | + <div |
| 68 | + className={ |
| 69 | + "overflow-hidden rounded-full w-8 h-8 border-2 isolate " + |
| 70 | + "dark:border-gray-200 border-slate-300 bg-gray-50/10 " + |
| 71 | + "peer-focus-visible:outline-2 peer-focus-visible:outline-white " + |
| 72 | + "peer-focus-visible:outline peer-focus-visible:border-sky-600" |
| 73 | + } |
| 74 | + > |
| 75 | + <label |
| 76 | + htmlFor="theme-toggle" |
| 77 | + className={ |
| 78 | + '[[aria-label="light"]~*_&]:motion-safe:animate-rotate-0-180 ' + |
| 79 | + '[[aria-label="dark"]~*_&]:motion-safe:animate-rotate-180-360 ' + |
| 80 | + "block h-full w-[200%] whitespace-nowrap rotate-180 origin-center " + |
| 81 | + "select-none dark:rotate-0" |
| 82 | + } |
| 83 | + > |
| 84 | + <Moon className="h-full p-1.5 inline-block" aria-hidden="true" /> |
| 85 | + <Sun className="h-full p-1.5 inline-block" aria-hidden="true" /> |
| 86 | + </label> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
0 commit comments