No results found

Switch

A toggleable control that changes between two states when activated.

Features

  • WAI ARIA Switch design pattern
  • Hidden native input for form integration
  • Controlled and uncontrolled state management
  • Error state handling with aria-errormessage
  • Support for required/disabled states
  • Custom labels and descriptions with ARIA support
  • Keyboard navigation (Space/Enter)
  • Form binding support
  • Toggle animation support via thumb component
  • Customizable styling through data attributes

Anatomy

PartDescription
<Switch.Root>Root component that manages the switch state and context
<Switch.Trigger>Interactive element that toggles the switch state
<Switch.Thumb>Visual indicator that moves to reflect the checked state
<Switch.Label>Text label associated with the switch
<Switch.Description>Additional descriptive text for the switch
<Switch.ErrorMessage>Error message displayed when the switch is in an error state
<Switch.HiddenInput>Hidden native input for form integration

Examples

Basic Usage

The most basic switch implementation includes a trigger, thumb, and label. The switch can be controlled through mouse clicks or keyboard interactions.

Status: Notifications are enabled

In this example, we demonstrate:

Visual Features

Description Text

Add supplementary information to the switch using the Description component.

(Receive notifications about important updates)

This example shows:

Advanced Usage

Disabled State

Switches can be disabled to prevent user interaction.

This example demonstrates:

Form Integration

Switches can be integrated into forms with validation and error handling.

This example showcases:

Component State

Using Component State

The Switch component accepts several state-related props:

State Interactions

Handling State Changes

The Switch component provides the onChange$ prop to handle state changes:

<Switch.Root
  onChange$={(checked) => {
    console.log(`Switch is now ${checked ? 'on' : 'off'}`);
  }}
>
  {/* ... */}
</Switch.Root>

Form Integration

As shown in the Form Integration example above, the Switch component integrates seamlessly with forms through:

And can be configured for form integration with the following props:

Error State Management

The Switch supports error state handling through:

As demonstrated in the Form Integration example, error states can be managed dynamically based on form validation or other conditions.

State Persistence

The Switch maintains its state across:

Default Values

The Switch component initializes with these default configurations:

{
  checked: false,
  disabled: false,
  required: false,
  name: "",
  value: ""
}

Advanced Configuration

Component Structure

The Switch uses a compound component pattern with these required elements:

Optional components include:

Technical Constraints

API Reference

Switch Root

Inherits from: <div />

Props

PropTypeDefaultDescription
checkedbooleanfalse
disabledbooleanfalse
requiredbooleanfalse
namestring""
valuestring""

Data Attributes

AttributeDescription
data-qds-switch-rootPresent on the root element
data-checkedIndicates whether the switch is currently checked
data-disabledIndicates whether the switch is currently disabled
data-errorIndicates whether the switch is in an error state

Switch Trigger

Inherits from: <button />

Data Attributes

AttributeDescription
data-qds-switch-triggerPresent on the trigger element
data-checkedIndicates whether the switch is currently checked
data-disabledIndicates whether the switch is currently disabled

Switch Thumb

Inherits from: <div />

Data Attributes

AttributeDescription
data-qds-switch-thumbPresent on the thumb element
data-checkedIndicates whether the switch is currently checked
data-disabledIndicates whether the switch is currently disabled

Switch Label

Inherits from: <label />

Data Attributes

AttributeDescription
data-qds-switch-labelPresent on the label element

Switch Description

Inherits from: <div />

Data Attributes

AttributeDescription
data-qds-switch-descriptionPresent on the description element

Switch Error Message

Inherits from: <div />

Data Attributes

AttributeDescription
data-qds-switch-error-messagePresent on the error message element

Switch Hidden Input

Inherits from: <input />

Props

PropTypeDefaultDescription
type"checkbox""checkbox"