initial commit
This commit is contained in:
20
lib/useSystemDark.ts
Normal file
20
lib/useSystemDark.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
"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
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user