navius

Switch

A control that toggles between an on and off state.

Features

  • Full keyboard navigation.
  • Can be controlled (@bind-Checked) or uncontrolled (DefaultChecked).
  • Renders a hidden input so it participates in native form submission.
  • role="switch" with the correct aria-checked state.

Installation

Install the brain, or copy just this primitive in with the CLI.

Anatomy

Import the part and assemble it.

API Reference

Contains all the parts of a switch. Renders a button.

Prop Type Default
Checked bool false
CheckedChanged EventCallback<bool> -
DefaultChecked bool false
Disabled bool false
Required bool false
Name string? -
Value string "on"
Form string? -
Data attribute Values
[data-checked] Present when checked
[data-unchecked] Present when unchecked
[data-disabled] Present when disabled

The thumb that is toggled to indicate state. Renders a span.

Prop Type Default
ChildContent RenderFragment? -
Data attribute Values
[data-checked] Present when checked
[data-unchecked] Present when unchecked
[data-disabled] Present when disabled

Accessibility

Adheres to the switch WAI-ARIA design pattern.

Key Description
Space Toggles the component's state.
Enter Toggles the component's state.