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:

Anatomytsx
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:

Render callbacktsx
<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.

PropTypeDefault
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.

defaultCheckedbooleanfalse

Whether the switch is initially active.

To render a controlled switch, use the checked prop instead.

disabledbooleanfalse

Whether 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.

readOnlybooleanfalse

Whether the user should be unable to activate or deactivate the switch.

requiredbooleanfalse

Whether 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.

nativeButtonbooleanfalse

Whether 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 attributeDescription
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).

<Switch.Thumb>

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

PropTypeDefault
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 attributeDescription
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).