Focus mode
The top bar has a Focus mode switch. Turning it on mutes notifications at once; the switch is the only place that says so.
Components / InputsStable
Turns a setting on or off, and the change applies at once with no Save button.
'use client'
import { useId, useState, type ButtonHTMLAttributes, type Ref } from 'react'
import { Note } from './Note'
// Nacre Switch: a setting that applies at once. React plus Tailwind classes only; needs nacre-theme.css.
// The thumb is a ring: a surface disc with a small centre dot in the track colour. Motion: the thumb slides base 240 on
// the spring curve and settles with a small overshoot, the track and the dot change colour quick 160. A pointer press
// stretches the thumb 25 % toward the side it will travel to (scale plus a matching translate, so its back edge stays
// put); letting go carries it over and back to round in one spring, retargeted from wherever it is. A key press (Space,
// Enter) moves the thumb at once, because keyboard actions never animate movement. Reduced motion or
// [data-nacre-motion="off"]: no movement.
// invalid marks it (aria-invalid only; the track keeps its on and off colours); error is the message under the label.
export type SwitchSize = 'sm' | 'md'
export type SwitchProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onChange' | 'value' | 'children'> & {
label: string
hideLabel?: boolean
description?: string
invalid?: boolean
error?: string
checked?: boolean
defaultChecked?: boolean
onCheckedChange?: (checked: boolean) => void
size?: SwitchSize
ref?: Ref<HTMLButtonElement>
}
// Thumb inset 2 px. sm: 28x16 track, 12 px thumb, on at 28 - 12 - 2 = 14 px. md: 36x20, 16 px thumb, on at 18 px.
// Pressed, the thumb is 1.25 wide from its centre, so it shifts by an eighth of its width (1.5 or 2 px) toward the
// travel side to keep its back edge where it was.
const track: Record<SwitchSize, string> = { sm: 'mt-0.5 h-4 w-7', md: 'h-5 w-9' }
const thumb: Record<SwitchSize, string> = {
sm:
'size-3 translate-x-0.5 data-[state=on]:translate-x-3.5 after:inset-1 ' +
'motion-safe:group-[:active:not(:focus-visible)]/track:translate-x-[3.5px] motion-safe:group-[:active:not(:focus-visible)]/track:data-[state=on]:translate-x-[12.5px]',
md:
'size-4 translate-x-0.5 data-[state=on]:translate-x-4.5 after:inset-[5px] ' +
'motion-safe:group-[:active:not(:focus-visible)]/track:translate-x-1 motion-safe:group-[:active:not(:focus-visible)]/track:data-[state=on]:translate-x-4',
}
export function Switch({
label,
hideLabel = false,
description,
invalid = false,
error,
checked,
defaultChecked = false,
onCheckedChange,
size = 'md',
disabled,
id,
className = '',
onClick,
ref,
...rest
}: SwitchProps) {
const auto = useId()
const switchId = id ?? auto
const descId = `${switchId}-desc`
const errorId = `${switchId}-error`
const bad = invalid || Boolean(error)
const describedBy = [rest['aria-describedby'], description ? descId : undefined, error ? errorId : undefined].filter(Boolean).join(' ') || undefined
const [inner, setInner] = useState(defaultChecked)
const on = checked ?? inner
const state = on ? 'on' : 'off'
return (
<div
data-part="root"
className={`group/switch flex items-start gap-3 ${className}`}
onKeyDown={(e) => e.currentTarget.setAttribute('data-kbd', '')}
onPointerDown={(e) => e.currentTarget.removeAttribute('data-kbd')}
>
<button
ref={ref}
id={switchId}
type="button"
role="switch"
aria-checked={on}
disabled={disabled}
data-state={state}
data-part="track"
onClick={(e) => {
onClick?.(e)
if (e.defaultPrevented) return
if (checked === undefined) setInner(!on)
onCheckedChange?.(!on)
}}
className={`group/track inline-flex shrink-0 cursor-pointer items-center rounded-full transition-colors duration-(--nx-dur-quick) ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus disabled:cursor-not-allowed disabled:opacity-50 ${track[size]} ${on ? 'bg-switch-on' : 'bg-switch-off'}`}
{...rest}
aria-invalid={bad || undefined}
aria-describedby={describedBy}
>
<span
data-part="thumb"
data-state={state}
data-press-scale=""
className={`relative rounded-full bg-surface shadow-1 after:absolute after:rounded-full after:transition-colors after:duration-(--nx-dur-quick) after:ease-out ${on ? 'after:bg-switch-on' : 'after:bg-switch-off'} transition-[translate,scale] duration-(--nx-dur-base) ease-spring motion-safe:group-[:active:not(:focus-visible)]/track:scale-x-125 group-active/track:duration-(--nx-dur-quick) group-active/track:ease-out group-disabled/track:after:bg-transparent group-data-[kbd]/switch:transition-none! ${thumb[size]}`}
/>
</button>
<span className="flex min-w-0 flex-col gap-0.5">
<label data-part="label" htmlFor={switchId} className={hideLabel ? 'sr-only' : `text-body ${disabled ? 'cursor-not-allowed text-fg-disabled' : 'cursor-pointer text-fg-strong'}`}>
{label}
</label>
{description && <span data-part="description" id={descId} className="text-caption text-fg-muted">{description}</span>}
<Note as="span" part="error" id={errorId} className="[--nx-note-gap:2px]">{error}</Note>
</span>
</div>
)
}
'use client'
import { useEffect, useRef, useState, type ReactNode } from 'react'
// Nacre Note: the hint, error or success line under a field. It rolls out: the row grows from 0 to its height (base 240
// out) while the words fade and rise 4 px into place, so the fields below glide down instead of jumping. On the way out
// it keeps its last words on screen while the row rolls back up (quick 160), then unmounts the words. reserve keeps one
// line of space even when there is nothing to say, for a field whose message is likely to appear (no layout shift at all).
// With motion off or reduced, it changes at once. Parts: data-part="hint" (muted), "error" and "success" (their tone).
// React plus Tailwind classes only; needs nacre-theme.css. --nx-note-gap (default 6 px) is the gap the parent puts above
// the note, so the collapsed row cancels it.
export type NoteProps = {
id?: string
part: 'hint' | 'error' | 'success'
children?: ReactNode
className?: string
as?: 'p' | 'span'
/** Keep one line of space while empty. */
reserve?: boolean
}
const tone = { hint: 'text-fg-muted', error: 'text-error-strong', success: 'text-success-strong' } as const
export function Note({ id, part, children, className = '', as: Tag = 'p', reserve = false }: NoteProps) {
const has = children !== undefined && children !== null && children !== false && children !== ''
const [last, setLast] = useState<ReactNode>(children)
const el = useRef<HTMLElement>(null)
const [leaving, setLeaving] = useState(false)
const [seen, setSeen] = useState(has)
if (has !== seen) {
setSeen(has)
setLeaving(!has)
}
if (has && children !== last) setLast(children)
useEffect(() => {
if (!leaving) return
const node = el.current
const raw = node ? getComputedStyle(node).transitionDuration.split(',')[0] : '0s'
const ms = parseFloat(raw) * (raw.endsWith('ms') ? 1 : 1000)
if (!ms) {
setLeaving(false)
return
}
const t = setTimeout(() => setLeaving(false), ms)
return () => clearTimeout(t)
}, [leaving])
const Element = 'span' as unknown as 'p'
const open = has
const hasWords = has || leaving
// The row: a one-cell grid whose track animates 0fr to 1fr. The negative margin cancels the parent's gap while closed.
const wrap = open || reserve ? 'grid-rows-[1fr]' : 'grid-rows-[0fr] mt-[calc(var(--nx-note-gap,6px)*-1)]'
const Wrap = Tag as 'p'
return (
<Wrap
data-part="note"
aria-hidden={hasWords ? undefined : true}
className={`grid transition-[grid-template-rows,margin-top] ease-out ${open ? 'duration-(--nx-dur-base)' : 'duration-(--nx-dur-quick)'} ${wrap} ${className}`}
>
<span className="block min-h-0 overflow-hidden">
{hasWords && (
<Element
ref={el as React.Ref<HTMLParagraphElement>}
data-part={part}
id={id}
aria-hidden={has ? undefined : true}
className={`block text-caption ${tone[part]} transition-[opacity,translate] duration-(--nx-dur-quick) ease-out starting:-translate-y-1 starting:opacity-0 ${has ? '' : '-translate-y-1 opacity-0'}`}
>
{has ? children : last}
</Element>
)}
{reserve && !hasWords && <span className="block text-caption" aria-hidden="true"> </span>}
</span>
</Wrap>
)
}
<div data-part="root" class="group/switch flex items-start gap-3 ">
<button id="_R_0_" type="button" role="switch" aria-checked="false" data-state="off" data-part="track" class="group/track inline-flex shrink-0 cursor-pointer items-center rounded-full transition-colors duration-(--nx-dur-quick) ease-out focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus disabled:cursor-not-allowed disabled:opacity-50 h-5 w-9 bg-switch-off" aria-describedby="_R_0_-desc">
<span data-part="thumb" data-state="off" data-press-scale="" class="relative rounded-full bg-surface shadow-1 after:absolute after:rounded-full after:transition-colors after:duration-(--nx-dur-quick) after:ease-out after:bg-switch-off transition-[translate,scale] duration-(--nx-dur-base) ease-spring motion-safe:group-[:active:not(:focus-visible)]/track:scale-x-125 group-active/track:duration-(--nx-dur-quick) group-active/track:ease-out group-disabled/track:after:bg-transparent group-data-[kbd]/switch:transition-none! size-4 translate-x-0.5 data-[state=on]:translate-x-4.5 after:inset-[5px] motion-safe:group-[:active:not(:focus-visible)]/track:translate-x-1 motion-safe:group-[:active:not(:focus-visible)]/track:data-[state=on]:translate-x-4"></span>
</button>
<span class="flex min-w-0 flex-col gap-0.5">
<label data-part="label" for="_R_0_" class="text-body cursor-pointer text-fg-strong">Focus mode</label>
<span data-part="description" id="_R_0_-desc" class="text-caption text-fg-muted">Mute notifications until you turn it off.</span>
<span data-part="note" aria-hidden="true" class="grid transition-[grid-template-rows,margin-top] ease-out duration-(--nx-dur-quick) grid-rows-[0fr] mt-[calc(var(--nx-note-gap,6px)*-1)] [--nx-note-gap:2px]">
<span class="block min-h-0 overflow-hidden"></span>
</span>
</span>
</div>/* The Tailwind theme lines Switch uses. They come from nacre-theme.css (Resources, Tokens), which also defines
the --nx-* values these lines point at. Import that file once; this list is what this component needs from it. */
@theme inline {
--color-surface: var(--nx-surface);
--color-fg-strong: var(--nx-fg-strong);
--color-fg-muted: var(--nx-fg-muted);
--color-fg-disabled: var(--nx-fg-disabled);
--color-focus: var(--nx-focus);
--color-switch-off: var(--nx-switch-off);
--color-switch-on: var(--nx-switch-on);
--color-success-strong: var(--nx-success-strong);
--color-error-strong: var(--nx-error-strong);
--color-transparent: transparent;
--text-body: 0.875rem;
--text-body--line-height: 1.25rem;
--text-body--letter-spacing: 0em;
--text-body--font-weight: 400;
--text-caption: 0.75rem;
--text-caption--line-height: 1rem;
--text-caption--letter-spacing: 0.01em;
--text-caption--font-weight: 400;
--spacing: 0.25rem;
--shadow-1: var(--nx-shadow-1);
--ease-out: cubic-bezier(.23, 1, .32, 1);
--ease-spring: linear(0, 0.036 3.1%, 0.128 6.3%, 0.251 9.4%, 0.389 12.5%, 0.526 15.6%, 0.655 18.8%, 0.769 21.9%, 0.864 25%, 0.939 28.1%, 0.996 31.3%, 1.036 34.4%, 1.062 37.5%, 1.075 40.6%, 1.08 43.8%, 1.077 46.9%, 1.07 50%, 1.061 53.1%, 1.05 56.3%, 1.039 59.4%, 1.028 62.5%, 1.019 65.6%, 1.011 68.8%, 1.005 71.9%, 1.001 75%, 0.997 78.1%, 0.995 81.3%, 0.994 84.4%, 0.994 87.5%, 0.994 90.6%, 0.994 93.8%, 0.995 96.9%, 1);
--breakpoint-sm: 40rem;
--breakpoint-md: 48rem;
}
The top bar has a Focus mode switch. Turning it on mutes notifications at once; the switch is the only place that says so.
Show weekends and Start the week on Monday are switches in a list. Each row has a short description of what changes.
DoLabel the setting, not the action: Focus mode.
Don'tWrite Turn on focus mode, which reads wrong once it is on.
DoApply the change the moment the switch moves.
Don'tAsk for a Save after a switch; use a checkbox in that form instead.