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.
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
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
- A choice sets
color-schemeon<html>, so the kit's colours follow it, anddata-theme="light"or"dark"for your own CSS. System removesdata-themeand setscolor-scheme: light dark: the device decides, also when it changes later. - It is stored in
localStorageundersf-theme, and System removes the key. PassstorageKey(and the same key tothemeScript) to keep a key your project already uses forlightanddark. - Where storage is blocked, as in some private windows, the choice still holds on the page until it is left.
- Other tabs of the same site follow a new choice at once. Every change dispatches
sf:theme-changeondocument. - The button shows the current choice's icon, set by CSS from
data-theme, so it is right before any script runs. The menu is the kit's Menu, opening on the current choice: arrow keys, typed letters, Escape and Tab, witharia-checkedon the current choice. - It stores nothing on a server: the server renders the page before it knows the choice.
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; }| Token | Default |
|---|---|
--sf-color-focus | #2563eb |
--sf-color-hover | rgb(9 9 11 / 6%) |
--sf-popover-min-width | 12rem |
--sf-popover-max-width | min(22rem, 100vw - 2 * var(--sf-overlay-gutter)) |
--sf-popover-max-height | min(22rem, 70dvh) |
--sf-popover-offset | var(--sf-space-1) |
--sf-radius-popover | var(--sf-radius-control) |
--sf-shadow-popover | 0 8px 24px rgb(0 0 0 / 16%) |
--sf-button-bg | var(--sf-color-surface) |
--sf-button-color | var(--sf-color-text) |
--sf-button-border-color | var(--sf-field-border-color) |
--sf-button-primary-bg | var(--sf-color-text) |
--sf-button-primary-color | var(--sf-color-surface) |
--sf-button-weight | 600 |
--sf-button-padding | var(--sf-space-4) |
--sf-button-gap | var(--sf-space-2) |
--sf-button-spinner-size | 1rem |
--sf-button-spin-duration | 800ms |
--sf-radius-button | var(--sf-radius-control) |