suiss/ui · Component
Toggle
A two-state button that stays pressed until it's pressed again.
Toggle is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Toggle wraps the toggle primitive of the chosen base. When pressed, it shows a tinted background and primary-colored content, and it scales down slightly while being pressed.
Use a toggle for a single on-off option such as bold text or a bookmark. To choose among related options, use ToggleGroup; for a setting that takes effect immediately, use Switch.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Pressed
Set defaultPressed to start the toggle in the pressed state.
Set defaultSelected to start the toggle in the pressed state.
Variants
Use the variant prop to switch between the borderless default style and the outline style with a border.
Sizes
Use the size prop to set the height and padding: sm, default or lg.
With text
Pairs an icon with a text label. Mark the icon with data-icon="inline-start" or data-icon="inline-end" to tighten the padding on that side.
Disabled
Set disabled so the toggle ignores clicks and keyboard presses.
Set isDisabled so the toggle ignores clicks and keyboard presses.
Accessibility
- Renders a
buttonwitharia-pressedreflecting its state. EnterandSpacetoggle the focused button.- An icon-only toggle needs an
aria-label.
API Reference
Toggle
The pressable two-state button.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
pressed | boolean | — |
defaultPressed | boolean | false |
onPressedChange | (pressed: boolean) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
isSelected | boolean | — |
defaultSelected | boolean | false |
onChange | (isSelected: boolean) => void | — |
isDisabled | boolean | false |
Also accepts the props of the primitive part it wraps.
Use defaultPressed for an uncontrolled toggle, or pressed with onPressedChange to control it. The toggle sets data-slot="toggle", and its pressed styles key off aria-pressed.
Use defaultSelected for an uncontrolled toggle, or isSelected with onChange to control it. The toggle sets data-slot="toggle", and its pressed styles key off data-selected.
toggleVariants is exported so other components, such as ToggleGroupItem, can share the styles. The press animation is removed when reduced motion is preferred.
Web preview
Toggle is a Pressable with an on and off state. When on, it shows a selected background and its text turns to the primary color; it scales down slightly while pressed. It has two variants, default and outline, and three sizes.
ToggleIcon is an Icon sized for use inside a toggle.
Installation
- Install the following dependencies:
- Copy and paste the following code into
components/ui/toggle.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy and paste the following code into
components/ui/toggle.tsx:
- Update the import paths to match your project setup.
- Copy and paste the following code into
components/ui/toggle.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Pressed
An icon toggle that starts in the on state.
Web preview
Variants
The default and outline variants.
Web preview
Sizes
Outline toggles in the sm, default and lg sizes.
Web preview
With text
An outline toggle with an icon and a text label.
Web preview
Disabled
Disabled toggles in the off and on states.
Web preview
Accessibility
- Uses the
togglebuttonrole and reportscheckedanddisabledinaccessibilityState. - Inside a single-select Toggle Group, it uses the
radiorole instead. - Icon-only toggles need an
accessibilityLabel.
API Reference
Toggle
The pressable two-state button.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
Also accepts the props of React.ComponentProps<typeof TogglePrimitive>.
Control the state with pressed and onPressedChange(pressed, details), or leave it uncontrolled with defaultPressed. Put text in a Text and icons in ToggleIcon. To group toggles with single or multiple selection, use Toggle Group. Changing the state triggers a selection haptic when a haptics adapter is provided through HapticsContext. toggleVariants, toggleStateClassName and toggleTextClassName are exported for reuse in custom components.
ToggleIcon
An icon sized for a toggle.
Also accepts the props of React.ComponentProps<typeof Icon>.
ToggleVariant
Values: .default, .outline.
ToggleSize
Values: .default, .sm, .lg.
.toggleStyle(.variant)
Style that creates VariantToggleStyle.
| Parameter | Type | Default |
|---|---|---|
_ variant | ToggleVariant: .default | .outline | .default |
size | ToggleSize: .default | .sm | .lg | .default |
ToggleVariant
Values: Default, Outline.
ToggleSize
Values: Default, Sm, Lg.
Toggle
The pressable two-state button.
| Parameter | Type | Default |
|---|---|---|
pressed | Boolean | - |
onPressedChange | (Boolean) -> Unit | - |
modifier | Modifier | Modifier |
variant | ToggleVariant: Default | Outline | ToggleVariant.Default |
size | ToggleSize: Default | Sm | Lg | ToggleSize.Default |
enabled | Boolean | true |
interactionSource | MutableInteractionSource? | null |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
Overloads: Toggle(pressed, onPressedChange, interactionSource, content), Toggle(text, pressed, onPressedChange).