Switch
A control that indicates whether a setting is on or off.
Stencil boundary
fs-switch-thumb exposes its internal movable span through an open ShadowRoot and
::part(thumb). Classes remain on the host; use this part or the
--fs-switch-thumb-* theme variables to style the indicator. Checked and disabled
state attributes are available on the host. Inline styles continue to reach the
internal span, while ordinary page selectors cannot style it directly.
fs-switch-root retains Light DOM for its native checkbox, switch control, label
association, and form participation. The Base UI theme is built in; select data-fs-theme="base-ui" on an ancestor to enable the optional theme.
The Root and authored Thumb content remain accessible to ordinary CSS selectors.
Usage guidelines
- Form controls must have an accessible name: It can be created using a
<label>element or theFieldcomponent. See Labeling a switch and the forms guide.
Anatomy
Import the component and assemble its parts:
Examples
Labeling a switch
An enclosing <label> is the simplest labeling pattern:
Rendering as a native button
By default, <Switch.Root> renders a <span> element to support enclosing labels. Prefer rendering the switch as a native button when using sibling labels (htmlFor/id).
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:
Form integration
Use Field to handle label associations and form integration:
API reference
Root
Represents the switch itself.
Renders a <span> element and a hidden <input> beside.
namestring—
- Name
- Description
Identifies the field when a form is submitted.
- Type
defaultCheckedbooleanfalse
- Name
- Description
Whether the switch is initially active.
To render a controlled switch, use the
checkedprop instead.- Type
- Default
false
checkedboolean—
- Name
- Description
Whether the switch is currently active.
To render an uncontrolled switch, use the
defaultCheckedprop instead.- Type
onCheckedChangefunction—
- Name
- Description
Event handler called when the switch is activated or deactivated.
- Type
valuestring—
- Name
- Description
The value submitted with the form when the switch is on. By default, switch submits the “on” value, matching native checkbox behavior.
- Type
formstring—
- Name
- Description
Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form.
- Type
nativeButtonbooleanfalse
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set totrueif the rendered element is a native button.- Type
- Default
false
uncheckedValuestring—
- Name
- Description
The value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior.
- Type
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
- Type
- Default
false
readOnlybooleanfalse
- Name
- Description
Whether the user should be unable to activate or deactivate the switch.
- Type
- Default
false
requiredbooleanfalse
- Name
- Description
Whether the user must activate the switch before submitting a form.
- Type
- Default
false
inputRefReact.Ref<HTMLInputElement>—
- Name
- Description
A ref to access the hidden
<input>element.- Type
idstring—
- Name
- Description
The id of the hidden input element.
When
nativeButtonistrue, the id is applied to the root element.- Type
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
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-dirty
Present when the switch’s value has changed (when wrapped in Field.Root).
data-touched
Present when the switch has been touched (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).
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-dirty | Present when the switch’s value has changed (when wrapped in Field.Root). | |
data-touched | Present when the switch has been touched (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.Root.StateHide
Switch.Root.ChangeEventReasonHide
Switch.Root.ChangeEventDetailsHide
Thumb
The movable part of the switch that indicates whether the switch is on or off.
Renders a <span>.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
styleReact.CSSProperties | function—
- Name
- Description
Style applied to the element, or a function that returns a style object based on the component’s state.
- Type
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
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-dirty
Present when the switch’s value has changed (when wrapped in Field.Root).
data-touched
Present when the switch has been touched (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).
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-dirty | Present when the switch’s value has changed (when wrapped in Field.Root). | |
data-touched | Present when the switch has been touched (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). | |