Switch
An on/off switch for settings that apply right away.
Use a switch when the change takes effect immediately, and a checkbox when it’s applied with the rest of a form.
Dependencies
What it's built from and what's built from it in Mocha, read from the imports in its source.
| Uses | Nothing else in the system. |
|---|---|
| Used by | Nothing yet. |
| Packages | @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20 |
Installation
Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.
With the CLI
Manually
Install its packages:
Then copy the source:
API reference
Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.
Switch
An on/off switch with its thumb built in. Wrap it in a Label with its text, or name it with aria-label.
- checked
Whether it's checked. Pass it to control the checkbox.
Type boolean
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: SwitchState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: SwitchState) => string)
- controller
Toggles it from anywhere. Create one with
createSwitchController().Type { toggle: (details?: CheckboxChangeOptions) => boolean; getChecked: () => boolean; setChecked: (checked: boolean, details?: CheckboxChangeOptions) => boolean; focus: () => void; }
- debug
Logs every state change with its reason to the console, in development builds.
Type boolean
- defaultChecked
Whether it's checked at first, when uncontrolled. A form reset returns to it.
Type boolean · Default false
- disabled
Disables it like a native control: it can't be focused or changed, and isn't submitted.
Type boolean
- form
The id of a form to belong to, when it isn't inside that form.
Type string
- name
The name it submits under. Without one, it submits nothing.
Type string
- onCheckedChange
Runs when a user or a form reset wants to change it.
details.reasonsays why, anddetails.cancel()stops it.Type (checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) => void
- readOnly
Keeps it focusable and submitted, but stops changes.
Type boolean
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<SwitchState>
- required
Blocks form submission until it's checked, with the browser's own message.
Type boolean
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: SwitchState) => CSSProperties)
- value
What it submits while checked.
Type string · Default "on"
SwitchThumb
The sliding thumb.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: NoInfer<SwitchState>) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: NoInfer<SwitchState>) => string)
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<NoInfer<SwitchState>>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: NoInfer<SwitchState>) => CSSProperties)