# 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](https://next.wave-design-system.pages.dev/tokens/colors.md#functional-tokens) and of the [elevation](https://next.wave-design-system.pages.dev/tokens/elevation.md) 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
<html lang="en" data-wave-color-mode="system"></html>
```

| Value    | Colours                                             |
| -------- | --------------------------------------------------- |
| `system` | Light or dark, as the operating system is set.      |
| `light`  | Light. This is also the mode without the attribute. |
| `dark`   | Dark.                                               |

`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](https://next.wave-design-system.pages.dev/get-started/developing.md#include-wave-core-assets) 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:

```html
<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.

> **Important**
>
> 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:

```jsx
<Flex>
  <Box
    data-wave-color-mode="light"
    padding="spacingM"
    css={{
      color: '$foregroundNeutral',
      backgroundColor: '$backgroundNeutralMinimal',
      border: '1px solid $borderNeutral',
      borderRadius: '$m'
    }}
  >
    Always light
  </Box>
  <Box
    data-wave-color-mode="dark"
    padding="spacingM"
    css={{
      color: '$foregroundNeutral',
      backgroundColor: '$backgroundNeutralMinimal',
      border: '1px solid $borderNeutral',
      borderRadius: '$m'
    }}
  >
    Always dark
  </Box>
</Flex>
```

Components that open in a layer above the page, such as [Dialog](https://next.wave-design-system.pages.dev/components/dialog.md), [Menu](https://next.wave-design-system.pages.dev/components/menu.md) and [Popover](https://next.wave-design-system.pages.dev/components/popover.md), 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:

```jsx
() => {
  const [colorMode, setColorMode] = useColorMode();

  return (
    <FormField.Root as={Flex} flow="column">
      <FormField.Label as="div">{'Colour mode'}</FormField.Label>
      <SingleSelectMenu.Root value={colorMode} onValueChange={setColorMode}>
        <SingleSelectMenu.Trigger />
        <SingleSelectMenu.Content>
          <SingleSelectMenu.Item value="system">
            {'System'}
          </SingleSelectMenu.Item>
          <SingleSelectMenu.Item value="light">{'Light'}</SingleSelectMenu.Item>
          <SingleSelectMenu.Item value="dark">{'Dark'}</SingleSelectMenu.Item>
        </SingleSelectMenu.Content>
      </SingleSelectMenu.Root>
    </FormField.Root>
  );
};
```

> **Tip**
>
> 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:

```jsx
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:

```js
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:

<Grid
  columns={{
    '@initial': '1',
    '@mqLargeAndUp': '2'
  }}
  columnGap="spacingL"
>
  <GridItem>
    <Card
      href="https://github.com/Volue/wave-vite-example"
      title="Vite example"
      actionIcon="externalLink"
    />
  </GridItem>
  <GridItem>
    <Card
      href="https://github.com/Volue/wave-nextjs-app-router-example"
      title="Next.js with app router example"
      actionIcon="externalLink"
    />
  </GridItem>
</Grid>

### With Wave Core only

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

<CodeBlock language="html">{`<script>\n${colorModeScript}\n</script>`}</CodeBlock>

To switch, set the attribute and store the mode:

```js
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](https://next.wave-design-system.pages.dev/tokens/colors.md#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.
