Switch
StableToggles a binary on/off state with immediate effect. The thumb translates with spring easing for a premium feel. Use when the change applies instantly — use Checkbox for form submissions.
Form elementInteractiveVhyxSeal
Interactive example
Notifications enabled
Toggle notifications
Import
tsx
import { Switch } from '@vhyxui/react'States
OffOnDisabled offDisabled on
Off, On, Disabled off, Disabled on
Sizes
sm, md, lg
Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Controlled on/off state. |
defaultChecked | boolean | false | Default state for uncontrolled usage. |
onCheckedChange | (checked: boolean) => void | — | Called when the switch is toggled. |
size | 'sm' | 'md' | 'lg' | 'md' | Size of the switch. |
disabled | boolean | false | Disables interaction (from HTMLButtonElement). |
asChild | boolean | false | Renders as the child element via Slot. |
contract | Partial<ComponentContract> | — | VhyxSeal contract override. |
className | string | — | Additional CSS classes appended to the switch. |
Also accepts all standard HTMLButtonElement attributes.
Accessibility
- Renders as
<button role="switch">— correct semantics for a toggle. -
aria-checkedreflects the current on/off state automatically. - Always provide
aria-labelor pair with a visible<label>usinghtmlFor. - Focus ring via
:focus-visible— visible on keyboard, hidden on mouse click. - Use Switch for immediate-effect settings. Use Checkbox for form values submitted on save.
Keyboard navigation
| Key | Action |
|---|---|
| SpaceorEnter | Toggle the switch on or off. |
| Tab | Move focus to the next focusable element. |
| Shift + Tab | Move focus to the previous focusable element. |
Agent contract
Default VhyxSeal contract shipped with every Switch.
Default contract
{
"type": "input",
"intent": "toggle-state",
"description": "Toggles a binary on/off state immediately upon interaction",
"requires": [],
"requiredPermissions": [],
"consequence": "Changes the boolean state of the controlled feature",
"affects": [],
"reversible": true,
"safetyLevel": "low",
"requiresConfirmation": false,
"destructive": false,
"contractVersion": "0.0.1",
"fingerprint": "vhyxs_5ced5d92"
}Theming
Override these CSS tokens to theme Switch.
--vhyx-color-accentTrack color when on
--vhyx-color-accent-hoverTrack hover color when on
--vhyx-color-bg-mutedTrack color when off
--vhyx-color-surfaceThumb color
--vhyx-radius-fullTrack and thumb border radius
--vhyx-duration-normalThumb slide duration
--vhyx-easing-springThumb slide easing
--vhyx-shadow-focusFocus ring
Examples
Settings panel
Push notifications
Dark mode
Marketing emails
Multiple toggles in a settings list
Inside a Field
tsx
<Field name="notifications" label="Push notifications" hint="Receive alerts for important activity">
<Switch
checked={form.watch('notifications')}
onCheckedChange={v => form.setValue('notifications', v)}
aria-label="Enable push notifications"
/>
</Field>