Components
Switch
A control that indicates whether a setting is on or off.
Usage guidelines#
- Form controls must have an accessible name. Wrap the switch in a
<label>, or use the Field component.
Anatomy#
Import the component and assemble its parts:
import { Switch } from '@egoist/solid-base/switch';
<Switch.Root>
<Switch.Thumb />
</Switch.Root>;Examples#
Labeling a switch#
An enclosing <label> is the simplest labeling pattern:
<label>
<Switch.Root />
Notifications
</label>Rendering as a native button#
By default, <Switch.Root> renders a <span> so it can sit inside a label. When the label is a
sibling (for/id), render a native button instead:
<label for="notifications-switch">Notifications</label>
<Switch.Root id="notifications-switch" nativeButton render="button">
<Switch.Thumb />
</Switch.Root>Native buttons with wrapping labels are supported by using the render callback to avoid invalid
HTML, so the hidden input is placed outside the label:
<Switch.Root
nativeButton
render={(buttonProps) => (
<label>
<button {...buttonProps} />
Notifications
</label>
)}
/>Controlled#
Pass checked and update it from onCheckedChange. The second argument carries the event
details, including cancel():
const [enabled, setEnabled] = createSignal(false);
<Switch.Root checked={enabled()} onCheckedChange={(checked) => setEnabled(checked)}>
<Switch.Thumb />
</Switch.Root>;API reference#
<Switch.Root>
Represents the switch itself. Renders a <span> element and a hidden <input> beside.
idstring—The id of the hidden input element.
When nativeButton is true, the id is applied to the root element.
checkedboolean—Whether the switch is currently active.
To render an uncontrolled switch, use the defaultChecked prop instead.
defaultCheckedbooleanfalseWhether the switch is initially active.
To render a controlled switch, use the checked prop instead.
disabledbooleanfalseWhether the component should ignore user interaction.
inputRefRefProp<HTMLInputElement>—A ref to access the hidden <input> element.
namestring—Identifies the field when a form is submitted.
formstring—Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form.
onCheckedChange| ((checked: boolean, eventDetails: SwitchRoot.ChangeEventDetails) => void)—Event handler called when the switch is activated or deactivated.
readOnlybooleanfalseWhether the user should be unable to activate or deactivate the switch.
requiredbooleanfalseWhether 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.
nativeButtonbooleanfalseWhether 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.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
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).
<Switch.Thumb>
The movable part of the switch that indicates whether the switch is on or off. Renders a <span>.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
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).