ActionsModeToggle
Searchastronomic.dev# ModeToggle
In the navbar, to let a reader switch between light and dark; with nothing chosen the page follows their system.
Interactive: inline script.
## Usage
```astro
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
```
## Examples
### The switch
One button. With nothing chosen the page follows the reader's system. A press shows the other look and saves it; a press back to the system's own look clears the choice. Put the no-flash script in the page's <head> too: <script is:inline set:html={modeScript} />, with modeScript from @astronomic-dev/ui/lib/mode.ts; it applies a saved choice before the first paint.
```astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
---
<div class="flex items-center gap-3">
<ModeToggle />
<span class="text-small text-muted">Press to switch light and dark. Press again to follow your system.</span>
</div>
```
### A light base
For a site that opens in light whatever the reader's system: base="light" here and modeScriptFor("light") in the page's <head>, from @astronomic-dev/ui/lib/mode.ts. A press shows dark and saves it; a press back to light clears the choice. base="dark" works the same way the other round.
```astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
---
<div class="flex items-center gap-3">
<ModeToggle base="light" />
<span class="text-small text-muted">Opens in light. Press for dark; press again to go back to light.</span>
</div>
```
### In the navbar
In the utilities slot with tone="band", so it takes the bar's colors when a preset sets a band.
```astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
import NavbarSimple from "@astronomic-dev/ui/components/NavbarSimple.astro";
import Star from "../../logos/astronomic-star.svg";
// On the Navbar's bar: tone="band", in the utilities slot, beside the action.
const links = [
{ label: "Work", href: "#work", current: true },
{ label: "About", href: "#about" },
{ label: "Journal", href: "#journal" },
];
---
<NavbarSimple links={links} actions={[{ label: "Start a project", href: "#start", variant: "primary" }]} label="Astronomic">
<a slot="brand" href="#" class="inline-flex items-center gap-2 text-inherit no-underline">
<Star class="size-5 shrink-0" aria-hidden="true" />
Astronomic
</a>
<ModeToggle slot="utilities" tone="band" />
</NavbarSimple>
```
## Props
### ModeToggle
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | None | The button's accessible name, "Dark mode" by default. Its pressed state (aria-pressed) says whether dark mode is on, so name the dark mode, not an action. With the default slot it has no default: the visible text is the name. |
| `variant` | `"primary" \| "secondary" \| "ghost"` | `"ghost"` | Button's variants. |
| `size` | `"sm" \| "md" \| "lg"` | `"sm"` | Button's icon sizes. Every size has a 44px hit area on touch. |
| `tone` | `"page" \| "image" \| "band" \| "cta"` | `"page"` | The surface it sits on, as Button's tone: `band` on the Navbar's bar, `image` under a scrim (put focus-on-image on an ancestor), `cta` on the CTA band (put focus-on-cta on the band). |
| `base` | `"system" \| "light" \| "dark"` | `"system"` | The look the site opens in with nothing saved. system follows the reader's system. light or dark open in that look: a press shows the other one and saves it, and a press back clears the choice. Put the same base in the no-flash script: modeScriptFor(base) from lib/mode.ts (modeScript is modeScriptFor("system")). |
| `class` | `string` | None | More classes, merged with cn(), so yours win on conflicts. |
| Slot | Description |
| --- | --- |
| `default` | Visible text after the moon, for a place with room for words, such as a menu panel: it is then a Button, not an icon. The text names it (no aria-label), unless you pass label too: then the label must start with the same words. |
Any other attribute goes on the <button>.
## Accessibility
| Key | Action |
| --- | --- |
| Enter or Space | Switches between light and dark. |
- A native <button> with aria-pressed: pressed means dark mode is on, whichever way it was reached (a press or the system).
- The name is "Dark mode" by default; pass label in your site's language. With visible text, the text is the name, so what a speech user sees is what they say (WCAG 2.5.3).
- Without JavaScript it cannot work, so it is hidden; the page follows the system.
- The choice is saved in localStorage as aui-theme. Blocked storage is fine: the switch still works for the page.
ModeToggle
FreeIn the navbar, to let a reader switch between light and dark; with nothing chosen the page follows their system.
- File
- ModeToggle.astro
- Group
- Actions
- Kind
- Interactive: inline script
- Examples
- 3
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
The switch
One button. With nothing chosen the page follows the reader's system. A press shows the other look and saves it; a press back to the system's own look clears the choice. Put the no-flash script in the page's <head> too: <script is:inline set:html={modeScript} />, with modeScript from @astronomic-dev/ui/lib/mode.ts; it applies a saved choice before the first paint.
examples/modetoggle/main.astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
---
<div class="flex items-center gap-3">
<ModeToggle />
<span class="text-small text-muted">Press to switch light and dark. Press again to follow your system.</span>
</div>A light base
For a site that opens in light whatever the reader's system: base="light" here and modeScriptFor("light") in the page's <head>, from @astronomic-dev/ui/lib/mode.ts. A press shows dark and saves it; a press back to light clears the choice. base="dark" works the same way the other round.
examples/modetoggle/base.astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
---
<div class="flex items-center gap-3">
<ModeToggle base="light" />
<span class="text-small text-muted">Opens in light. Press for dark; press again to go back to light.</span>
</div>In the navbar
In the utilities slot with tone="band", so it takes the bar's colors when a preset sets a band.