Components
Switch
On or off, with no words: a #262626 track with a gray thumb when off, White with a Black thumb when on. The thumb slides on the strong ease-out.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add switch<script type="module" src="/spyglass-ds/elements/switch.js"></script>Weekly digest
New ads from the brands you track, every Monday.
New creative alerts
An email the day a tracked brand launches an ad.
Share outside Acme
Available on the Team plan.
Usage
A setting that takes effect at once: a digest, an alert, a sharing option. The row beside it says what it turns.
Never
- On and Off written beside it
- A switch with no line saying what it does
- A spectrum color for the on state
- A choice that waits for a Save button: use a checkbox
Props
The web component takes the same as attributes or properties (<spyglass-switch>); title is heading there.
<Switch>
| Prop | Type | Default |
|---|---|---|
label | string What it turns, for assistive tech (the row beside it says it for the eye). | |
disabled | boolean | |
checked | boolean | |
defaultChecked | boolean | false |
onCheckedChange | (checked: boolean) => void | |
className | string |
Examples
The same example in React and as web components; both render identically.
import { Switch } from '@/react/components/ui/Switch'
// Notification settings: each row says what the switch does, one on, one off, and one the plan locks.
const rows = [
{ title: 'Weekly digest', text: 'New ads from the brands you track, every Monday.', on: true },
{ title: 'New creative alerts', text: 'An email the day a tracked brand launches an ad.', on: false },
{ title: 'Share outside Acme', text: 'Available on the Team plan.', on: false, locked: true },
]
export default function Settings() {
return (
<div className="flex w-120 max-w-full flex-col divide-y divide-edge rounded-container bg-surface-raised px-6">
{rows.map((r) => (
<div key={r.title} className="flex items-center justify-between gap-6 py-4">
<div className="flex min-w-0 flex-col gap-1">
<p className="type-ui-strong text-fg">{r.title}</p>
<p className="type-caption text-fg-muted">{r.text}</p>
</div>
<Switch label={r.title} defaultChecked={r.on} disabled={r.locked} />
</div>
))}
</div>
)
}<!-- Notification settings: each row says what the switch does, one on, one off, and one the plan locks. -->
<div class="flex w-120 max-w-full flex-col divide-y divide-edge rounded-container bg-surface-raised px-6">
<div class="flex items-center justify-between gap-6 py-4">
<div class="flex min-w-0 flex-col gap-1">
<p class="type-ui-strong text-fg">Weekly digest</p>
<p class="type-caption text-fg-muted">New ads from the brands you track, every Monday.</p>
</div>
<spyglass-switch label="Weekly digest" checked></spyglass-switch>
</div>
<div class="flex items-center justify-between gap-6 py-4">
<div class="flex min-w-0 flex-col gap-1">
<p class="type-ui-strong text-fg">New creative alerts</p>
<p class="type-caption text-fg-muted">An email the day a tracked brand launches an ad.</p>
</div>
<spyglass-switch label="New creative alerts"></spyglass-switch>
</div>
<div class="flex items-center justify-between gap-6 py-4">
<div class="flex min-w-0 flex-col gap-1">
<p class="type-ui-strong text-fg">Share outside Acme</p>
<p class="type-caption text-fg-muted">Available on the Team plan.</p>
</div>
<spyglass-switch label="Share outside Acme" disabled></spyglass-switch>
</div>
</div>Source
'use client'
import { useControllable } from '@/react/hooks/useControllable'
// On or off, with no words: a 36 by 20 track with the brand corner and a 14px thumb (the small corner, nested inside).
// Off is the secondary #262626 fill with a #666666 hairline and a gray thumb; on is the primary White with a Black
// thumb, as the brand's one White action. The thumb slides over 320ms on the strong ease-out (reduced motion keeps only
// the color change). The row beside it says what the state means. Disabled is the disabled fill, never faded.
// `checked` with `onCheckedChange` controls it (or `defaultChecked` alone).
export interface SwitchProps {
/** What it turns, for assistive tech (the row beside it says it for the eye). */
label: string
disabled?: boolean
checked?: boolean
defaultChecked?: boolean
onCheckedChange?: (checked: boolean) => void
className?: string
}
export function Switch({ label, disabled, checked, defaultChecked = false, onCheckedChange, className }: SwitchProps) {
const [on, setOn] = useControllable(checked, defaultChecked, onCheckedChange)
return (
<>
<style href="ds-switch" precedence="ds">{css}</style>
<button data-ds="switch" type="button" role="switch" className={className ? `ds-switch ${className}` : 'ds-switch'} aria-checked={on} aria-label={label} disabled={disabled} onClick={() => setOn(!on)}><span aria-hidden="true" /></button>
</>
)
}
export default Switch
const css = `
.ds-switch { --w: 36px; --h: 20px; position: relative; display: inline-flex; width: var(--w); height: var(--h); flex-shrink: 0; align-items: center; padding: var(--ds-space-2xs); border: var(--ds-border-hairline) solid var(--ds-color-edge-strong); border-radius: var(--ds-radius-control); background: var(--ds-color-action-secondary); cursor: pointer; touch-action: manipulation; transition: border-color var(--ds-duration-micro) var(--ds-easing-standard), background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-switch > span { width: calc(var(--h) - 6px); height: calc(var(--h) - 6px); border-radius: var(--ds-radius-sm); background: var(--ds-color-fg-muted); transition: transform var(--ds-duration-glide) var(--ds-easing-out), background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-switch[aria-checked="true"] { border-color: var(--ds-color-action-primary); background: var(--ds-color-action-primary); }
.ds-switch[aria-checked="true"] > span { background: var(--ds-color-action-primary-fg); transform: translateX(calc(var(--w) - var(--h))); }
.ds-switch:disabled { border-color: var(--ds-color-edge); background: var(--ds-color-action-disabled); cursor: not-allowed; }
.ds-switch:disabled > span { background: var(--ds-color-action-disabled-fg); }
.ds-switch:focus-visible { outline: var(--ds-border-focus) solid var(--ds-color-edge-focus); outline-offset: var(--ds-border-focus); }
@media (hover: hover) and (pointer: fine) {
.ds-switch:hover:not(:disabled, [aria-checked="true"]) { border-color: var(--ds-color-action-secondary-edge-hover); }
.ds-switch[aria-checked="true"]:hover:not(:disabled) { border-color: var(--ds-color-action-primary-hover); background: var(--ds-color-action-primary-hover); }
}
/* A 44px place to land on phones, the switch keeping its size. */
@media (pointer: coarse), (max-width: 767px) { .ds-switch::after { position: absolute; inset: -12px; content: ""; } }
@media (prefers-reduced-motion: reduce) { .ds-switch > span { transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); } }
`'use client'
import { useCallback, useRef, useState } from 'react'
// Vue's defineModel in React: a value that is controlled when the caller passes it (value + onValueChange, as Radix
// does) and kept inside otherwise (defaultValue). setValue calls onChange either way.
export function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (value: T) => void) {
const [inner, setInner] = useState<T>(defaultValue)
const controlled = value !== undefined
const current = controlled ? value : inner
const ref = useRef(onChange)
ref.current = onChange
const set = useCallback((next: T) => {
if (!controlled) setInner(next)
ref.current?.(next)
}, [controlled])
return [current, set] as const
}import { html, LitElement, type PropertyValues } from 'lit'
import { DsFormElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Switch.tsx as <spyglass-switch>: on or off, with no words: a 36 by 20 track with the brand corner and a 14px thumb.
// Off is the secondary #262626 fill with a gray thumb; on is White with a Black thumb. The thumb slides over 320ms on
// the strong ease-out. `checked` (an attribute, as a checkbox's) with `input` and `change` events. Form-associated: its
// `name` goes with the form as "on" while checked.
/**
* @summary On or off, with no words.
* @fires input - It was turned (read `checked`).
* @fires change - It was turned (read `checked`).
*/
export class Switch extends DsFormElement {
static styles = [DsFormElement.styles, styles]
static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }
static properties = { checked: { type: Boolean, reflect: true }, label: {}, disabled: { type: Boolean, reflect: true }, name: {}, required: { type: Boolean } }
checked = false
/** What it turns, for assistive tech (the row beside it says it for the eye). */
label = ''
disabled = false
name?: string
/** It must be on for its form to send. */
required = false
// No native control: a required switch that is off is missing its value, as a required checkbox is.
syncValidity() {
this.setMissing(this.required && !this.checked, 'checkbox', this.renderRoot.querySelector('button'))
}
protected updated(changed: PropertyValues) {
super.updated(changed)
if (changed.has('checked')) this.internals.setFormValue(this.checked ? 'on' : null)
}
render() {
return html`<button data-ds="switch" type="button" role="switch" class="ds-switch" aria-checked=${String(this.checked)} aria-label=${this.label} ?disabled=${this.inactive} @click=${() => this.commit(!this.checked, 'checked')}><span aria-hidden="true"></span></button>`
}
}
define('switch', Switch)/* Switch.tsx's styles, as they are. */
.ds-switch { --w: 36px; --h: 20px; position: relative; display: inline-flex; width: var(--w); height: var(--h); flex-shrink: 0; align-items: center; padding: var(--ds-space-2xs); border: var(--ds-border-hairline) solid var(--ds-color-edge-strong); border-radius: var(--ds-radius-control); background: var(--ds-color-action-secondary); cursor: pointer; touch-action: manipulation; transition: border-color var(--ds-duration-micro) var(--ds-easing-standard), background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-switch > span { width: calc(var(--h) - 6px); height: calc(var(--h) - 6px); border-radius: var(--ds-radius-sm); background: var(--ds-color-fg-muted); transition: transform var(--ds-duration-glide) var(--ds-easing-out), background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-switch[aria-checked="true"] { border-color: var(--ds-color-action-primary); background: var(--ds-color-action-primary); }
.ds-switch[aria-checked="true"] > span { background: var(--ds-color-action-primary-fg); transform: translateX(calc(var(--w) - var(--h))); }
.ds-switch:disabled { border-color: var(--ds-color-edge); background: var(--ds-color-action-disabled); cursor: not-allowed; }
.ds-switch:disabled > span { background: var(--ds-color-action-disabled-fg); }
.ds-switch:focus-visible { outline: var(--ds-border-focus) solid var(--ds-color-edge-focus); outline-offset: var(--ds-border-focus); }
@media (hover: hover) and (pointer: fine) {
.ds-switch:hover:not(:disabled, [aria-checked="true"]) { border-color: var(--ds-color-action-secondary-edge-hover); }
.ds-switch[aria-checked="true"]:hover:not(:disabled) { border-color: var(--ds-color-action-primary-hover); background: var(--ds-color-action-primary-hover); }
}
/* A 44px place to land on phones, the switch keeping its size. */
@media (pointer: coarse), (max-width: 767px) { .ds-switch::after { position: absolute; inset: -12px; content: ""; } }
@media (prefers-reduced-motion: reduce) { .ds-switch > span { transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); } }Reference: Derived from the brand guidelines (no switch in the brand assets): White as the one primary state, the button's secondary fill, the brand corner.