GitHub

Switch

Toggle switch for boolean options

Basic Usage

Loading...

Installation

pnpm add @corew500/ui

Usage

import { Switch } from "@corew500/ui/switch"

<Switch>Content</Switch>

Variants

switchVariants

AxisValuesDefault
size
smdefaultlg
default

thumbVariants

AxisValuesDefault
size
smdefaultlg
default

CMS Integration

CMSPayload CMS Integration
This component supports CMS-driven content via @corew500/cms-payload.
Loading...

API Reference

Props

PropTypeDefaultDescription
classNameenum—CSS class applied to the element, or a function that returns a class based on the component’s state.
defaultCheckedenumfalseWhether the switch is initially active. To render a controlled switch, use the `checked` prop instead.
styleenum—Style applied to the element, or a function that returns a style object based on the component’s state.
idstring—The id of the switch element.
renderenum—Allows you to replace the component’s HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.
checkedenum—Whether the switch is currently active. To render an uncontrolled switch, use the `defaultChecked` prop instead.
disabledenumfalseWhether the component should ignore user interaction.
inputRefenum—A ref to access the hidden `<input>` element.
namestring—Identifies the field when a form is submitted.
onCheckedChange(checked: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element; }) => void—Event handler called when the switch is activated or deactivated.
readOnlyenumfalseWhether the user should be unable to activate or deactivate the switch.
requiredenumfalseWhether the user must activate the switch before submitting a form.
valuestring—The value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior.
uncheckedValuestring—The value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.
nativeButtonenumfalseWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `true` if the rendered element is a native button.
sizeenum——

Accessibility

Keyboard Navigation

  • Keyboard accessible: Space or Enter to toggle
  • Focus visible ring for keyboard navigation

Screen Readers

  • - Uses `role="switch"` for proper semantics (handled by Base UI)
  • `aria-checked` state managed automatically

Additional

  • Disabled state indicated visually and to assistive technology
  • Extended touch target via pseudo-element for mobile accessibility

Localization

Translatable Content

  • - Labels should be provided externally via `FieldLabel`
  • For accessible name, use `aria-labelledby` pointing to the label's `id`, or `aria-label` when no visible label exists

Additional

  • Note: `htmlFor`/`id` does not work with Base UI Switch (id goes to hidden input, not the visible element)

Related

Related Components