Theme Toggle
One button that cycles the page theme through dark, light and auto, and remembers the choice.
<arc-theme-toggle>Overview
ThemeToggle is one button that cycles through dark, light and auto (the system setting). Each mode has its own icon (a moon, a sun or a monitor), and the icons scale and rotate as they change. The chosen mode is written to data-theme on the document root and to localStorage under arc-theme, so it persists across page loads.
On first mount it uses the theme attribute if you set one. Otherwise it reads arc-theme from localStorage, then the data-theme already on the root, and falls back to auto.
By default the button shows the current mode's name next to the icon. icon-only drops the name for a compact button in a toolbar or header.
Guidelines
Do
- Put it somewhere that's always on screen, such as the top bar or a settings panel
- Use `icon-only` where space is tight
- Listen to `arc-change` to save the choice to a backend preference
- Make sure your styles follow the `data-theme` attribute on the document root, or switching does nothing
- Set `data-theme` on the HTML element during server rendering, so the page doesn't flash the wrong theme
Don't
- Don't change the theme by writing the `arc-theme` key in `localStorage`. The toggle doesn't watch storage. Set a toggle's `theme` property or the root's `data-theme`, and every toggle on the page picks it up
- Don't use it for settings other than the theme. Use Toggle
Features
- Fires `arc-change` with the new mode in `detail.value` on every change
- Enter and Space cycle the mode
- The button scales to 0.95 while pressed
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-theme-toggle></arc-theme-toggle>import { ThemeToggle } from '@arclux/arc-ui-react';
export default function Example() {
return (
<ThemeToggle />
);
}<script setup>
import { ThemeToggle } from '@arclux/arc-ui-vue';
</script>
<template>
<ThemeToggle />
</template><script>
import { ThemeToggle } from '@arclux/arc-ui-svelte';
</script>
<ThemeToggle />import { Component } from '@angular/core';
import { ThemeToggle } from '@arclux/arc-ui-angular';
@Component({
imports: [ThemeToggle],
template: `
<arc-theme-toggle></arc-theme-toggle>
`,
})
export class MyComponent {}import { ThemeToggle } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<ThemeToggle />
);
}import { ThemeToggle } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<ThemeToggle />
);
}API
theme'dark' | 'light' | 'auto''auto'- The current theme mode. Synced in both directions: changing it (by click, by key, or by assigning the property) writes the document root's
data-themeand localStorage, and a change to that attribute from anywhere else is adopted back, so every toggle on the page agrees. disabledbooleanfalse- Prevents cycling and reduces opacity to 40%.
iconOnlybooleanfalse- Renders the button as a compact square without the theme name label, matching an icon-only arc-icon-button of the same size. Attribute name is
icon-only. size'xs' | 'sm' | 'md' | 'lg''md'- Box size when
icon-only, on the same scale as arc-icon-button: xs=28px, sm=32px, md=36px, lg=44px. Set both controls to the same value when they sit side by side. Ignored by the labeled form, which is sized by its text.
Events
arc-changedetail:{ value: 'dark' | 'light' | 'auto' }- Fired when the theme is toggled, with { theme } detail