Files
blog-manager/lib/useSystemDark.ts
2026-11-01 13:23:44 -05:00

21 lines
522 B
TypeScript

"use client";
import { useSyncExternalStore } from "react";
const QUERY = "(prefers-color-scheme: dark)";
function subscribe(onChange: () => void): () => void {
const mq = window.matchMedia(QUERY);
mq.addEventListener("change", onChange);
return () => mq.removeEventListener("change", onChange);
}
/** Whether the OS is in dark mode (false during SSR). */
export function useSystemDark(): boolean {
return useSyncExternalStore(
subscribe,
() => window.matchMedia(QUERY).matches,
() => false
);
}