InputsReact
A control that indicates whether a setting is on or off.
Rendered by React 19.3.1
Loading React…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/reactNot 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>.
| Prop | Type | Default |
|---|---|---|
nameIdentifies the field when a form is submitted. | string | — |
defaultCheckedWhether the switch is initially active. | boolean | false |
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 it | boolean | false |
uncheckedValueThe value submitted with the form when the switch is off. | string | — |
disabledWhether the component should ignore user interaction. | boolean | false |
readOnlyWhether the user should be unable to activate or deactivate the switch. | boolean | false |
requiredWhether the user must activate the switch before submitting a form. | boolean | false |
inputRefA ref to access the hidden <input> element. | React.Ref<HTMLInputElement> | — |
idThe id of the hidden input element. | string | — |
| Data attribute | Description |
|---|---|
data-checked | Present when the switch is checked. |
data-unchecked | Present when the switch is not checked. |
data-disabled | Present when the switch is disabled. |
data-readonly | Present when the switch is readonly. |
data-required | Present when the switch is required. |
data-valid | Present when the switch is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the switch is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the switch has been touched (when wrapped in Field.Root). |
data-dirty | Present when the switch's value has changed (when wrapped in Field.Root). |
data-filled | Present when the switch is active (when wrapped in Field.Root). |
data-focused | Present 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>.
| Data attribute | Description |
|---|---|
data-checked | Present when the switch is checked. |
data-unchecked | Present when the switch is not checked. |
data-disabled | Present when the switch is disabled. |
data-readonly | Present when the switch is readonly. |
data-required | Present when the switch is required. |
data-valid | Present when the switch is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the switch is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the switch has been touched (when wrapped in Field.Root). |
data-dirty | Present when the switch's value has changed (when wrapped in Field.Root). |
data-filled | Present when the switch is active (when wrapped in Field.Root). |
data-focused | Present when the switch is focused (when wrapped in Field.Root). |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
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
| Keys | Does | Part |
|---|---|---|
| EnterSpace | Turns the switch on or off | Root |
Accessibility
| Part | Sets | So that |
|---|---|---|
| Root | role="switch", aria-checked | a screen reader reads it as a switch and whether it is on |
| Root | aria-readonly, aria-required from readOnly and required | the constraints are announced |
| Root | aria-labelledby to an associated label | a <label> or Field label names the switch |
| Root | a 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 |
| Thumb | renders a <span> | the moving part is decoration; the state is on Root |
Focus order
- Root is one tab stop. The hidden input is skipped.