Switch
A toggle component that allows users to enable or disable a setting with a smooth transition effect.
Basic
A switch is a toggle that flips between on and off, similar to a light switch.
Installation
Composed components
When you install this component via the CLI, it automatically loads all composed components, so you don’t need to add them individually.
This component comes packed with several components to enhance functionality and provide a seamless experience.
Manual installation
Anatomy
Description
You can add a description to the switch by passing the description
prop or using the Description
component.
Controlled
You can manage the switch's state by setting the isSelected
prop.
Uncontrolled
You can also use the uncontrolled version of the switch.
Disabled
A disabled switch cannot be toggled and is non-interactive.
Installation
To use the Switch component, you need to install the @intentui/switch
package. You can do this using npm or yarn:
or
Additionally, the Switch component requires the react-aria-components
package. If you haven't installed it yet, you can do so with the following command:
Composed components
The Switch component is composed of the following sub-components:
Switch
: The main toggle switch component.Label
: A component to display the label of the switch.Description
: A component to display a description or helper text for the switch.
Manual installation
If you prefer to install the components manually, you can do so by installing the react-aria-components
package. This package includes all the necessary components and hooks to use the Switch component.
To install the package, run the following command:
Once installed, you can import the components directly from the package:
Anatomy
The Switch component consists of the following parts:
- A container element that wraps the entire switch.
- A hidden checkbox input that represents the on/off state of the switch.
- A visual indicator (the switch knob) that shows the current state of the switch.
- A label element that describes the purpose of the switch.
- An optional description element that provides additional information about the switch.
The following diagram illustrates the anatomy of the Switch component:
Description
The Switch component supports an optional description that can be used to provide additional information about the switch's purpose or functionality.
To add a description to the switch, you can use the description
prop or include a Description
component as a child of the Switch
component.
Here's an example of using the description
prop:
And here's an example of using the Description
component:
Controlled
The Switch component can be used in a controlled manner, where the on/off state of the switch is controlled by a parent component.
To use the switch in a controlled manner, you need to manage the isSelected
state in the parent component and pass it as a prop to the Switch
component.
Here's an example:
Uncontrolled
The Switch component can also be used in an uncontrolled manner, where the on/off state is managed internally by the component.
To use the switch in an uncontrolled manner, simply render the Switch
component without managing the isSelected
state in the parent component.
Here's an example:
Disabled
The Switch component supports a disabled state, where the switch is visually indicated as disabled and cannot be toggled.
To disable the switch, you can set the isDisabled
prop to true
.
Here's an example:
Design Intent UI