useColorMode

Reads the colour mode of the page, and sets and stores it.

Returns the colour mode of the page and a function that sets the mode and stores it. The mode follows a change from any source, including another window of the app.

Import#

import { useColorMode } from '@volue/wave-react';

Example#

The example switches the mode of this site.

useColorMode does not apply the stored mode when a page loads. Render colorModeScript in the <head> for that, as Let people switch the mode shows.

On the server and during hydration, useColorMode returns undefined, because the server cannot know the stored mode. Render a neutral state for undefined, such as a mode switch with no option selected.


API reference#

Returns#

[colorMode: ColorMode | undefined, setColorMode: (mode: ColorMode | string) => void]. A tuple similar to useState. ColorMode is 'light' | 'dark' | 'system'. colorMode is undefined on the server and during hydration, and light when the root element has no data-wave-color-mode attribute. setColorMode sets the mode on the root element and stores it in local storage. It takes any string, such as the value of a select, and ignores one that is not a mode.