1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-06-30 07:16:37 -04:00

clean up ThemeContext

This commit is contained in:
2022-04-27 10:58:25 -04:00
parent e0d13c874d
commit c4651cb315
2 changed files with 23 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import { createContext, useCallback, useEffect, useState, useRef } from "react"; import { createContext, useCallback, useEffect, useState } from "react";
import { useLocalStorage } from "react-use"; import { useLocalStorage } from "react-use";
import { darkModeQuery, themeStorageKey } from "../lib/config/themes"; import { darkModeQuery, themeStorageKey } from "../lib/config/themes";
import type { Context, PropsWithChildren } from "react"; import type { Context, PropsWithChildren } from "react";
@ -26,69 +26,64 @@ export const ThemeProvider = ({
classNames: { classNames: {
[themeName: string]: string; [themeName: string]: string;
}; };
/** Optionally set `color-scheme` CSS property to change browser appearance. */
enableColorScheme?: boolean;
}>) => { }>) => {
// keep track of if/when the user has set their theme *here*: // keep track of if/when the user has set their theme *here*:
const [preferredTheme, setPreferredTheme] = useLocalStorage(themeStorageKey, null, { raw: true }); const [preferredTheme, setPreferredTheme] = useLocalStorage(themeStorageKey, null, { raw: true });
// save the end result no matter how we got there (by preference or by system): // save the end result no matter how we got there (by preference or by system):
const [resolvedTheme, setResolvedTheme] = useState(""); const [resolvedTheme, setResolvedTheme] = useState("");
// TODO: remove this and do related stuff more gracefully // get the theme names (light, dark) via passed-in classnames' keys
const validThemes = Object.keys(classNames); const themeNames = Object.keys(classNames);
// updates the DOM and optionally saves the new theme to local storage // updates the DOM and optionally saves the new theme to local storage
const changeTheme = useCallback( const changeTheme = useCallback(
(theme: string, updateStorage: boolean) => { (theme: string, updateStorage?: boolean) => {
if (updateStorage) { if (updateStorage) {
setPreferredTheme(theme); setPreferredTheme(theme);
} }
// remove all theme classes first to start fresh // remove all theme classes first to start fresh
const all = Object.values(classNames); const all = Object.values(classNames);
const d = document.documentElement; document.documentElement.classList.remove(...all);
d.classList.remove(...all); document.documentElement.classList.add(classNames[theme]);
d.classList.add(classNames[theme]);
}, },
[classNames, setPreferredTheme] [classNames, setPreferredTheme]
); );
// memoize browser media query handler // memoize browser media query handler
const handleMediaQuery = useCallback( const handleMediaQuery = useCallback(
(e?: MediaQueryList) => { (e: MediaQueryListEvent | MediaQueryList) => {
// get the user's preferred theme via their OS/browser settings // get the user's preferred theme via their OS/browser settings
const media = e || window.matchMedia(darkModeQuery); const systemTheme = e.matches ? "dark" : "light";
const systemTheme = media.matches ? "dark" : "light";
// keep track of the resolved theme whether or not we change it below
setResolvedTheme(systemTheme); setResolvedTheme(systemTheme);
// only actually change the theme if preference is unset (and *don't* save new theme to storage) // only actually change the theme if preference is unset (and *don't* save new theme to storage)
if (!preferredTheme || !validThemes.includes(preferredTheme)) changeTheme(systemTheme, false); if (!preferredTheme || !themeNames.includes(preferredTheme)) {
changeTheme(systemTheme, false);
}
}, },
[changeTheme, preferredTheme, validThemes] [changeTheme, preferredTheme, themeNames]
); );
// ref hack to avoid adding handleMediaQuery as a dependency
const mediaListener = useRef(handleMediaQuery);
mediaListener.current = handleMediaQuery;
// listen for changes in OS preference // listen for changes in OS preference
useEffect(() => { useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handler = (...args: any) => mediaListener.current(...args);
const media = window.matchMedia(darkModeQuery); const media = window.matchMedia(darkModeQuery);
media.addEventListener("change", handleMediaQuery);
media.addEventListener("change", handler); handleMediaQuery(media);
handler(media);
// clean up the event listener // clean up the event listener
return () => { return () => {
media.removeEventListener("change", handler); media.removeEventListener("change", handleMediaQuery);
}; };
}, []); }, [handleMediaQuery]);
// color-scheme handling (tells browser how to render built-in elements like forms, scrollbars, etc.) // color-scheme handling (tells browser how to render built-in elements like forms, scrollbars, etc.)
useEffect(() => { useEffect(() => {
// only "light" and "dark" are valid here // only "light" and "dark" are valid here
const colorScheme = ["light", "dark"].includes(preferredTheme) ? preferredTheme : resolvedTheme; const colorScheme = ["light", "dark"].includes(preferredTheme) ? preferredTheme : resolvedTheme;
document.documentElement.style.setProperty("color-scheme", colorScheme); document.documentElement.style?.setProperty("color-scheme", colorScheme);
}, [preferredTheme, resolvedTheme]); }, [preferredTheme, resolvedTheme]);
return ( return (
@ -101,8 +96,8 @@ export const ThemeProvider = ({
}, },
[changeTheme] [changeTheme]
), ),
preferredTheme: validThemes.includes(preferredTheme) ? preferredTheme : undefined, preferredTheme: themeNames.includes(preferredTheme) ? preferredTheme : undefined,
resolvedTheme: validThemes.includes(preferredTheme) ? preferredTheme : resolvedTheme, resolvedTheme: themeNames.includes(preferredTheme) ? preferredTheme : resolvedTheme,
}} }}
> >
{children} {children}

View File

@ -24,6 +24,7 @@ import type { NoteType } from "../../types";
export const getNoteSlugs = async () => { export const getNoteSlugs = async () => {
// get all files in NOTES_DIR // get all files in NOTES_DIR
const files = await fs.readdir(path.join(process.cwd(), NOTES_DIR)); const files = await fs.readdir(path.join(process.cwd(), NOTES_DIR));
// narrow to only the .mdx files and strip the .mdx extension // narrow to only the .mdx files and strip the .mdx extension
return files.filter((file) => /\.mdx$/.test(file)).map((noteFile) => noteFile.replace(/\.mdx$/, "")); return files.filter((file) => /\.mdx$/.test(file)).map((noteFile) => noteFile.replace(/\.mdx$/, ""));
}; };