Skip to main content

usePrefersColorScheme

function usePrefersColorScheme(): ColorScheme;

type ColorScheme = "light" | "dark";

Returns the colour scheme the user's system asks for, updating when they change it. Anything other than dark, including no preference, counts as "light". It returns "light" during server rendering and hydration, then the real setting.

Live demo​

Light theme

Switch your operating system between light and dark mode.

Example​

import { useEffect, type ReactNode } from "react";
import { usePrefersColorScheme } from "react-honey-hooks";

function ThemeRoot({ children }: { children: ReactNode }) {
const scheme = usePrefersColorScheme();

useEffect(() => {
document.documentElement.dataset.theme = scheme;
}, [scheme]);

return children;
}