Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

InputsReact

Switch

A control that indicates whether a setting is on or off.

Rendered by React 19.3.1

Loading React…
import { Switch } from '@hamur/react/switch'

export default function SwitchExample() {
  return (
    <label className="row">
      <Switch.Root className="switch" defaultChecked>
        <Switch.Thumb className="thumb" />
      </Switch.Root>
      Airplane mode
    </label>
  )
}
188Tests passing in React · 0 failing
7.7 KiBSize
Not measured yetMemory
Not measured yetSpeed
productdevbook and 15 upstream authors14 commits · last

Installation

npm i @hamur/react

Not on npm yet. The package goes out under the chosen name.

Anatomy

Import the component and assemble its parts:

<Switch.Root>
  <Switch.Thumb />
</Switch.Root>

Examples

The demo at the top is the one example this component has so far.

API reference

13 props across 2 parts, in React syntax. className, style, render are listed once, under Shared props.

RootSwitch.Root

Represents the switch itself. and a hidden <input> beside. Renders a <span>.

Root props
PropTypeDefault
nameIdentifies the field when a form is submitted.string—
defaultCheckedWhether the switch is initially active.booleanfalse
checkedWhether the switch is currently active.boolean—
onCheckedChangeEvent handler called when the switch is activated or deactivated.((checked: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void)—
valueThe value submitted with the form when the switch is on.string—
formIdentifies the form that owns the hidden input.string—
nativeButtonWhether the component renders a native <button> element when replacing itbooleanfalse
uncheckedValueThe value submitted with the form when the switch is off.string—
disabledWhether the component should ignore user interaction.booleanfalse
readOnlyWhether the user should be unable to activate or deactivate the switch.booleanfalse
requiredWhether the user must activate the switch before submitting a form.booleanfalse
inputRefA ref to access the hidden <input> element.React.Ref<HTMLInputElement>—
idThe id of the hidden input element.string—
Root data attributes
Data attributeDescription
data-checkedPresent when the switch is checked.
data-uncheckedPresent when the switch is not checked.
data-disabledPresent when the switch is disabled.
data-readonlyPresent when the switch is readonly.
data-requiredPresent when the switch is required.
data-validPresent when the switch is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the switch is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the switch has been touched (when wrapped in Field.Root).
data-dirtyPresent when the switch's value has changed (when wrapped in Field.Root).
data-filledPresent when the switch is active (when wrapped in Field.Root).
data-focusedPresent when the switch is focused (when wrapped in Field.Root).

ThumbSwitch.Thumb

The movable part of the switch that indicates whether the switch is on or off. Renders a <span>. Renders a <span>.

Thumb data attributes
Data attributeDescription
data-checkedPresent when the switch is checked.
data-uncheckedPresent when the switch is not checked.
data-disabledPresent when the switch is disabled.
data-readonlyPresent when the switch is readonly.
data-requiredPresent when the switch is required.
data-validPresent when the switch is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the switch is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the switch has been touched (when wrapped in Field.Root).
data-dirtyPresent when the switch's value has changed (when wrapped in Field.Root).
data-filledPresent when the switch is active (when wrapped in Field.Root).
data-focusedPresent when the switch is focused (when wrapped in Field.Root).

Shared props

Every part that renders an element takes these.

Shared props
PropType
classNameCSS class applied to the element, or a function that returns a class based on the component's state.string | ((state: State) => string | undefined)
styleStyle applied to the element, or a function that returns a style object based on the component's state.React.CSSProperties | ((state: State) => React.CSSProperties | undefined)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State>

Keyboard and accessibility

Keyboard

Keyboard
KeysDoesPart
EnterSpaceTurns the switch on or offRoot

Accessibility

What the parts set
PartSetsSo that
Rootrole="switch", aria-checkeda screen reader reads it as a switch and whether it is on
Rootaria-readonly, aria-required from readOnly and requiredthe constraints are announced
Rootaria-labelledby to an associated labela <label> or Field label names the switch
Roota hidden <input type="checkbox"> with aria-hidden="true" and tabindex="-1"the form gets the value without a second control to read or tab to
Thumbrenders a <span>the moving part is decoration; the state is on Root

Focus order

  1. Root is one tab stop. The hidden input is skipped.