A toggle is used to switch between two possible states, commonly enabled or disabled states.
import { Toggle } from "@opengovsg/oui"<Toggle>Enable notifications</Toggle>The Toggle component allows users to switch between two states (on/off). It is built on React Aria's Switch.
If the component does not have a visible label (by passing children), an aria-label prop must be passed instead to identify it to assistive technology.
Pass children to add a visible text label next to the toggle.
Use the labelPlacement prop to position the label before or after the toggle. Defaults to "end".
Use the size prop to change the size of the toggle. Defaults to "md".
Use the thumbIcon prop to display an icon inside the toggle thumb. The prop accepts a React element or a render function that receives SwitchRenderProps (including isSelected and className).
Use the isDisabled prop to disable the toggle.
Use the isSelected and onChange props to control the toggle state programmatically.
Dark mode is off.
Toggle supports the following event handlers, inherited from React Aria's Switch:
onChange — Called when the selection state changes, receiving a boolean.onFocus / onBlur — Called when the toggle gains or loses focus.onFocusChange — Called when the focus state changes, receiving a boolean.onKeyDown / onKeyUp — Called on keyboard events.onHoverStart / onHoverEnd / onHoverChange — Called on hover events.children, an aria-label prop must be set.You can customize the Toggle component by passing custom Tailwind CSS classes to the component slots via the classNames prop.
| Prop | Type | Default | Description |
|---|---|---|---|
children | React.ReactNode | - | The label content. Required if aria-label is not set. |
aria-label | string | - | Accessible label. Required if children is not set. |
isSelected | boolean | - | The current selection state (controlled) |
defaultSelected | boolean | - | The default selection state (uncontrolled) |
onChange | (isSelected: boolean) => void | - | Callback fired when the selection state changes |
thumbIcon | React.ReactElement | ((props: ToggleThumbIconProps) => React.ReactNode) | - | Icon to display inside the thumb. Render function receives isSelected and className. |
labelPlacement | "start" | "end" | "end" | Where to place the label relative to the toggle |
size | "xs" | "sm" | "md" | "md" | The size of the toggle |
colorScheme | "success" | "success" | The color scheme of the toggle |
isDisabled | boolean | false | Whether the toggle is disabled |
isReadOnly | boolean | false | Whether the toggle is read-only |
name | string | - | The name of the input, used when submitting a form |
value | string | - | The value of the input, used when submitting a form |
autoFocus | boolean | false | Whether the toggle should receive focus on render |
onFocus | (e: FocusEvent) => void | - | Called when the toggle receives focus |
onBlur | (e: FocusEvent) => void | - | Called when the toggle loses focus |
onFocusChange | (isFocused: boolean) => void | - | Called when the focus state changes |
onKeyDown | (e: KeyboardEvent) => void | - | Called when a key is pressed |
onKeyUp | (e: KeyboardEvent) => void | - | Called when a key is released |
classNames | SlotsToClasses<ToggleSlots> | - | Custom CSS classes for component slots |