| 1 | "use client"; |
| 2 | |
| 3 | import { useTheme } from |
| 4 | import { useEffect, useState |
| 5 | |
| 6 | import { |
| 7 | applyCustomThemeVars, |
| 8 | clearCustomThemeVars, |
| 9 | CUSTOM_THEME_KEY, |
| 10 | DEFAULT_CUSTOM_THEME, |
| 11 | loadCustomTheme, |
| 12 | themeKind, |
| 13 | type CustomThemeVars, |
| 14 | } from "@/lib/themes"; |
| 15 | |
| 16 | export const CUSTOM_THEME_UPDATED_EVENT = |
| 17 | |
| 18 | export function useCustomTheme() |
| 19 | const [custom, |
| 20 | |
| 21 | useEffect(() => |
| 22 | // localStorage only exists in the browser, so the stored custom theme is read after mount. |
| 23 | // eslint-disable-next-line react-hooks/set-state-in-effect |
| 24 | setCustom(loadCustomTheme()); |
| 25 | }, []); |
| 26 | |
| 27 | useEffect(() => |
| 28 | const reload = |
| 29 | const onStorage = |
| 30 | if (e.key |
| 31 | }; |
| 32 | window.addEventListener("storage |
| 33 | window.addEventListener(CUSTOM_THEME_UPDATED_EVENT |
| 34 | return () => |
| 35 | window.removeEventListener("storage |
| 36 | window.removeEventListener(CUSTOM_THEME_UPDATED_EVENT |
| 37 | }; |
| 38 | }, []); |
| 39 | |
| 40 | return { custom |
| 41 | } |
| 42 | |
| 43 | /** Keeps Tailwind `dark:` utilities working for every dark-kind theme by mirroring the `dark` class, and applies user custom-theme variables. */ |
| 44 | export function ThemeEffects() |
| 45 | const { theme, |
| 46 | const { custom |
| 47 | |
| 48 | useEffect(() => |
| 49 | const active = |
| 50 | const kind = |
| 51 | const root = |
| 52 | |
| 53 | if (active === |
| 54 | applyCustomThemeVars(custom); |
| 55 | } else { |
| 56 | clearCustomThemeVars(); |
| 57 | } |
| 58 | |
| 59 | // next-themes owns the theme class itself; we only mirror `dark` so `dark:` variants keep working on dark-kind themes like vesper. |
| 60 | if (active !== |
| 61 | if (kind === |
| 62 | else root.classList. |
| 63 | } |
| 64 | }, [theme, |
| 65 | |
| 66 | return null; |
| 67 | } |