21 lines
522 B
TypeScript
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
|
|
);
|
|
}
|