GlassKit UI
Components

Toggle

A binary switch, D-pad-focusable. Controlled via checked + onChange. The knob slides on a logical margin, so it mirrors correctly under RTL.

Quick controls

Enter flips the focused switch

600 × 600 · live

Installation

npx @glasskit-ui/cli add toggle

Install the SDK (it provides GlassViewport, useDpad and the stylesheet), then copy these files into your project:

npm install @glasskit-ui/react
// components/lib/utils.tsexport type ClassValue = string | number | null | undefined | false;/** * Join truthy class names. Dependency-free on purpose: the lens components * style via bespoke semantic classes (no conflicting Tailwind utilities to * de-dupe), so this needs no clsx/tailwind-merge and resolves from anywhere * the registry is vendored. */export function cn(...inputs: ClassValue[]): string {  return inputs.filter(Boolean).join(" ");}/** * Accessible name from a free-form `label` prop: the label itself when it's a * plain string, otherwise undefined (a ReactNode can't become an aria-label). */export function stringLabel(label: unknown): string | undefined {  return typeof label === "string" ? label : undefined;}
// components/glasskit/toggle.tsximport type { ReactNode } from "react";import { cn } from "../lib/utils";/** * <Toggle> — a binary switch, D-pad-focusable (renders a real button with the * focusable class; useDpad activates it on Enter/Space). Controlled: pass * `checked` + `onChange`. The knob slides via a logical margin, so it mirrors * correctly under RTL (a switch is UI chrome, not world-anchored). */export function Toggle({  checked,  onChange,  label,  disabled,  className,}: {  checked: boolean;  onChange?: (next: boolean) => void;  label?: ReactNode;  disabled?: boolean;  className?: string;}) {  return (    <button      type="button"      role="switch"      aria-checked={checked}      disabled={disabled}      onClick={onChange ? () => onChange(!checked) : undefined}      className={cn(        "focusable gk-toggle t-body",        checked && "gk-toggle--on",        className,      )}    >      {label != null ? <span className="gk-toggle__label">{label}</span> : null}      <span className="gk-toggle__track">        <span className="gk-toggle__knob" />      </span>    </button>  );}

Usage

<Toggle  label="Notifications"  checked={on}  onChange={setOn}/>

Props

Prop

Type