Colour modes

Switch Wave between its light and dark colours, for a whole page or for a part of it.

Wave has a light and a dark colour mode. The mode sets the values of the functional colour tokens and of the elevation shadows. Wave Core and Wave React read the same tokens, so both follow the mode.

Set the colour mode#

Add the data-wave-color-mode attribute to the root element:

<html lang="en" data-wave-color-mode="system"></html>
ValueColours
systemLight or dark, as the operating system is set.
lightLight. This is also the mode without the attribute.
darkDark.

system is the recommended value. It gives people the mode that they chose for their device.

The mode needs no JavaScript. The Wave core styles hold the values of every mode, and the attribute selects them. Form controls and scrollbars follow the mode as well.

Until the stylesheet has loaded, the browser shows an empty page in its light colours. To avoid that in the dark mode, declare the colour scheme in the <head> as well:

<meta name="color-scheme" content="light dark" />

Use light dark if the page follows the system or lets people switch the mode. Use dark if the page is always dark.

Add the tag only together with the data-wave-color-mode attribute. Without the attribute, Wave has its light colours, but on a dark system the tag makes the browser treat the page as dark.

Use another mode for a part of the page#

The attribute works on any element. It sets the mode of that element and of everything inside it:

Components that open in a layer above the page, such as Dialog, Menu and Popover, render at the end of the document. They take the mode of the root element, not the mode of the section that opens them.

Let people switch the mode#

With Wave React#

useColorMode returns the mode of the page and a function that sets and stores it. This example switches the mode of this site:

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

The hook stores the mode, but only colorModeScript applies it, when a page loads and when another window of the app changes it. Without the script, the stored mode is ignored. Render it at the start of the <head>.

In the Next.js App Router:

import { colorModeScript } from '@volue/wave-react/color-mode';
export default function RootLayout({ children }) {
return (
<html
lang="en"
data-wave-color-mode="system"
// the script changes the attribute before React hydrates the page
suppressHydrationWarning
>
<head>
<meta name="color-scheme" content="light dark" />
<script dangerouslySetInnerHTML={{ __html: colorModeScript }} />
</head>
<body>{children}</body>
</html>
);
}

With Vite:

import { colorModeScript } from '@volue/wave-react/color-mode';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
{
name: 'color-mode',
transformIndexHtml: () => [
{ tag: 'script', children: colorModeScript, injectTo: 'head-prepend' }
]
}
]
});

The starter projects have this setup:

With Wave Core only#

Put this script at the start of the <head>:

<script>
(function () {
function apply(mode) {
if (mode)
document.documentElement.setAttribute("data-wave-color-mode", mode);
}
try {
apply(localStorage.getItem("@wave.colorMode"));
} catch (error) {}
window.addEventListener('storage', function (event) {
if (event.key === "@wave.colorMode") apply(event.newValue);
});
})();
</script>

To switch, set the attribute and store the mode:

function setColorMode(mode) {
document.documentElement.setAttribute('data-wave-color-mode', mode);
localStorage.setItem('@wave.colorMode', mode);
}

Know what follows the mode#

The functional colour tokens and the elevation tokens follow the mode. The colour scale tokens, such as $colorGray100, are the same in both modes, so use functional tokens for anything that should change.

A token from theme follows the mode when you use it as it is, because it stands for its CSS variable. Its value, as in theme.colors.foregroundNeutral.value, is the light value in every mode.