|
| 1 | +// This code is taken from the `use-prefers-color-scheme` repo: |
| 2 | +// https://github.com/rfoel/use-prefers-color-scheme/blob/v1.0.0/src/index.ts |
| 3 | +// The code is copied instead of being imported as the package seems to cause |
| 4 | +// issues when used in a NextJS project. |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | + |
| 7 | +const darkQuery = window.matchMedia?.("(prefers-color-scheme: dark)"); |
| 8 | + |
| 9 | +const lightQuery = window.matchMedia?.("(prefers-color-scheme: light)"); |
| 10 | + |
| 11 | +export const usePrefersColorScheme = () => { |
| 12 | + const isDark = darkQuery?.matches; |
| 13 | + const isLight = lightQuery?.matches; |
| 14 | + |
| 15 | + const [preferredColorSchema, setPreferredColorSchema] = useState< |
| 16 | + "dark" | "light" | "no-preference" |
| 17 | + >(isDark ? "dark" : isLight ? "light" : "no-preference"); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (isDark) { |
| 21 | + setPreferredColorSchema("dark"); |
| 22 | + } else if (isLight) { |
| 23 | + setPreferredColorSchema("light"); |
| 24 | + } else { |
| 25 | + setPreferredColorSchema("no-preference"); |
| 26 | + } |
| 27 | + }, [isDark, isLight]); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (typeof darkQuery?.addEventListener === "function") { |
| 31 | + // In modern browsers MediaQueryList should subclass EventTarget |
| 32 | + // https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList |
| 33 | + |
| 34 | + const darkListener = ({ matches }: MediaQueryListEvent) => |
| 35 | + matches && setPreferredColorSchema("dark"); |
| 36 | + const lightListener = ({ matches }: MediaQueryListEvent) => |
| 37 | + matches && setPreferredColorSchema("light"); |
| 38 | + |
| 39 | + darkQuery?.addEventListener("change", darkListener); |
| 40 | + lightQuery?.addEventListener("change", lightListener); |
| 41 | + |
| 42 | + return () => { |
| 43 | + darkQuery?.removeEventListener("change", darkListener); |
| 44 | + lightQuery?.removeEventListener("change", lightListener); |
| 45 | + }; |
| 46 | + } else { |
| 47 | + // In some early implementations MediaQueryList existed, but did not |
| 48 | + // subclass EventTarget |
| 49 | + |
| 50 | + // Closing over isDark here would cause it to not update when |
| 51 | + // `darkQuery.matches` changes |
| 52 | + const listener = () => |
| 53 | + setPreferredColorSchema( |
| 54 | + darkQuery.matches |
| 55 | + ? "dark" |
| 56 | + : lightQuery.matches |
| 57 | + ? "light" |
| 58 | + : "no-preference" |
| 59 | + ); |
| 60 | + |
| 61 | + // This is two state updates if a user changes from dark to light, but |
| 62 | + // both state updates will be consistent and should be batched by React, |
| 63 | + // resulting in only one re-render |
| 64 | + darkQuery?.addEventListener("change", listener); |
| 65 | + lightQuery?.addEventListener("change", listener); |
| 66 | + |
| 67 | + return () => { |
| 68 | + darkQuery?.removeEventListener("change", listener); |
| 69 | + lightQuery?.removeEventListener("change", listener); |
| 70 | + }; |
| 71 | + } |
| 72 | + }, []); |
| 73 | + |
| 74 | + if (typeof window.matchMedia !== "function") { |
| 75 | + return preferredColorSchema; |
| 76 | + } |
| 77 | + |
| 78 | + return preferredColorSchema; |
| 79 | +}; |
0 commit comments