useDarkMode

Use this hook to control the user dark mode preference. By default it will rely only on the users settings via css media queries. But it is also possible to override and control this behaviour.

Usage

import React from 'react';
import { useDarkMode } from '@fransvilhelm/hooks';
const Example = () => {
const [enabled, setEnabled, isUserOverridden] = useDarkMode();
return <p />;
};

Example

Parameters

useDarkMode accepts ...

ParamTypeRequiredDefaultDescription
classNamestringfalse-Optional class name that will be appended to the body element

Returns

useDarkMode returns a tuple with three items:

PropTypeDescription
enabledbooleanIndicating if dark mode should be used or not
setEnabled(enable: boolean | null) => voidFunction to override dark mode preference. Call with null to revert back on relying on users OS settings
isUserOverridenbooleanFlag indicating if user has overridden system settings