Component · top layer

Theme menu

The user's choice of light, dark or the device's, kept in this browser and applied before the page is drawn.

Astro site

Both change this whole site, and the choice stays when you reload or open another page. Your own settings page can offer the same choice as a row.

React

React app

Usage

Put the script first in <head> of every page. Without it, a stored choice is applied only when the menu's script runs, so the page can flash in the other theme.

Astro:

---
import { themeScript } from '@subfloor/core'
---
<head>
  <!-- First in <head>: the stored choice, before the page is drawn. -->
  <script is:inline set:html={themeScript()} />
  …
</head>
---
import ThemeMenu from '@subfloor/astro/ThemeMenu.astro'
---
<header class="bar">
  …
  <ThemeMenu id="theme" labels={{ menu: 'Vzhled', system: 'Podle zařízení', light: 'Světlý', dark: 'Tmavý' }} />
</header>

Next.js:

// app/layout.tsx (a server component)
import { themeScript } from '@subfloor/core'

export default function RootLayout({ children }) {
  return (
    // The script changes <html> before React hydrates.
    <html lang="en" suppressHydrationWarning>
      <head>
        <script dangerouslySetInnerHTML={{ __html: themeScript() }} />
      </head>
      <body>{children}</body>
    </html>
  )
}

// In a bar
import { ThemeMenu } from '@subfloor/react'

<ThemeMenu labels={{ menu: 'Theme' }} />

Settings

The same choice as a row of choices, for a settings page. It stays in step with the menu and with other tabs.

'use client'
import { Segmented } from '@subfloor/react'
import { getThemeChoice, isThemeChoice, setThemeChoice } from '@subfloor/core'
import { useSyncExternalStore } from 'react'

const subscribe = (onChange) => {
  document.addEventListener('sf:theme-change', onChange)
  return () => document.removeEventListener('sf:theme-change', onChange)
}

export function Appearance() {
  const choice = useSyncExternalStore(subscribe, getThemeChoice, () => 'system')
  return (
    <Segmented
      label="Appearance"
      value={choice}
      onValueChange={(next) => isThemeChoice(next) && setThemeChoice(next)}
      options={[
        { value: 'system', label: 'System' },
        { value: 'light', label: 'Light' },
        { value: 'dark', label: 'Dark' },
      ]}
    />
  )
}

Markup

<button type="button" class="sf-button sf-theme-menu__trigger" data-variant="quiet"
  data-icon-only popovertarget="theme" aria-haspopup="menu" aria-controls="theme"
  aria-label="Theme" style="anchor-name: --theme">
  <span class="sf-button__label">
    <svg class="sf-theme-menu__icon" data-choice="system" aria-hidden="true">…</svg>
    <svg class="sf-theme-menu__icon" data-choice="light" aria-hidden="true">…</svg>
    <svg class="sf-theme-menu__icon" data-choice="dark" aria-hidden="true">…</svg>
  </span>
</button>
<div id="theme" popover="auto" role="menu" aria-label="Theme"
  class="sf-popover sf-menu sf-theme-menu" data-sf-theme-menu data-placement="bottom-end"
  style="position-anchor: --theme">
  <button type="button" class="sf-menu__item" role="menuitemradio" aria-checked="false"
    tabindex="-1" data-value="system">…System</button>
  <button … data-value="light">…Light</button>
  <button … data-value="dark">…Dark</button>
</div>

<script type="module">
  import { initThemeMenus } from '@subfloor/core'
  initThemeMenus()
</script>

Behaviour

Without JavaScript

The button opens and closes the menu, but choosing needs the script, as there is nowhere to keep the choice. The page follows the device.

Customisation

/* Your own dark colours, beside the kit's (which follow color-scheme by themselves). */
:root[data-theme='dark'] { --brand: #8ea2ff; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) { --brand: #8ea2ff; }
}

/* The button is the kit's quiet icon Button; triggerClass (Astro) or
   triggerProps.className (React) adds your own classes to it. */
.sf-theme-menu__trigger { --sf-radius-button: 999px; }
TokenDefault
--sf-color-focus#2563eb
--sf-color-hoverrgb(9 9 11 / 6%)
--sf-popover-min-width12rem
--sf-popover-max-widthmin(22rem, 100vw - 2 * var(--sf-overlay-gutter))
--sf-popover-max-heightmin(22rem, 70dvh)
--sf-popover-offsetvar(--sf-space-1)
--sf-radius-popovervar(--sf-radius-control)
--sf-shadow-popover0 8px 24px rgb(0 0 0 / 16%)
--sf-button-bgvar(--sf-color-surface)
--sf-button-colorvar(--sf-color-text)
--sf-button-border-colorvar(--sf-field-border-color)
--sf-button-primary-bgvar(--sf-color-text)
--sf-button-primary-colorvar(--sf-color-surface)
--sf-button-weight600
--sf-button-paddingvar(--sf-space-4)
--sf-button-gapvar(--sf-space-2)
--sf-button-spinner-size1rem
--sf-button-spin-duration800ms
--sf-radius-buttonvar(--sf-radius-control)