--- title: Toggle description: "`Toggle` is a component that has two states: on or off." links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/toggle/toggle.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/toggle - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-toggle--basic --- # Toggle `Toggle` is a component that has two states: on or off. ```tsx } aria-label="Toggle bold" /> ``` ## Usage ```ts import { Toggle, ToggleGroup } from "@yamada-ui/react" ``` ```ts import { Toggle, ToggleGroup } from "@/components/ui" ``` ```ts import { Toggle, ToggleGroup } from "@workspaces/ui" ``` ```tsx ``` ```tsx ``` :::tip If you want to perform an action rather than toggle a state, use [IconButton](https://yamada-ui.com/docs/components/icon-button.md) or [Button](https://yamada-ui.com/docs/components/button.md). ::: :::tip If there are multiple `Toggle` components that can be grouped together, use [ToggleGroup](#grouping). ::: ### With Text ```tsx Italic ``` ### Change Variant ```tsx {(variant) => ( } aria-label="Toggle bold" variant={variant} /> )} ``` ### Change Size ```tsx {(size) => ( } aria-label="Toggle bold" size={size} /> )} ``` ### Change Color Scheme ```tsx {(colorScheme) => ( } /> )} ``` ### Change Shape To change the shape, set the `rounded` prop. ```tsx {(variant) => ( } aria-label="Toggle bold" variant={variant} fullRounded /> )} ``` ### Disable To disable, set `disabled` to `true`. ```tsx {(variant) => ( } aria-label="Toggle bold" variant={variant} disabled /> )} ``` ### Read-Only To read-only, set `readOnly` to `true`. ```tsx {(variant) => ( } aria-label="Toggle bold" variant={variant} readOnly /> )} ``` ### Invalid To set invalid state, set `invalid` to `true`. ```tsx {(variant) => ( } aria-label="Toggle bold" variant={variant} invalid /> )} ``` ### Change Invalid Border Color To change the border color, set `errorBorderColor` to the color. ```tsx } defaultChecked invalid /> ``` ### Control ```tsx const [checked, setChecked] = useState(false) return ( } aria-label="Toggle bold" checked={checked} onChange={setChecked} /> ) ``` ### Grouping ```tsx } aria-label="Toggle bold" /> } aria-label="Toggle italic" /> } aria-label="Toggle underline" /> ``` For multiple selections, set `value` or `defaultValue` to an array. ```tsx const [value, setValue] = useState([]) return ( } aria-label="Toggle bold" /> } aria-label="Toggle italic" /> } aria-label="Toggle underline" /> ) ``` ## Props ### Toggle | Prop | Default | Type | Description | | ------------------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"2xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"` | The size of the component. | | `variant` | `"ghost"` | `"ghost" \| "outline" \| "solid" \| "subtle" \| "surface" ...` | The variant of the component. | | `active` | `false` | `boolean` | If `true`, the button is represented as active. | | `checked` | - | `boolean` | If `true`, the toggle button will be checked. | | `disabled` | `false` | `boolean` | If `true`, the button is disabled. | | `disableRipple` | `false` | `boolean` | If `true`, disable ripple effects when pressing a element. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `icon` | - | `string \| number \| bigint \| boolean \| ReactElement> \| Iterable \| ReactPortal \| Promise<...>` | The icon to be used in the button. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `loading` | `false` | `boolean` | If `true`, the loading state of the button is represented. | | `onChange` | - | `(checked: boolean) => void` | The callback invoked when checked state changes. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `type` | `"button"` | `"button" \| "reset" \| "submit"` | The type of button. Accepts `button`, `reset`, or `submit`. | | `value` | - | `Y` | The value of the toggle button. | ### ToggleGroup.Root | Prop | Default | Type | Description | | -------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"2xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"` | The size of the component. | | `variant` | `"ghost"` | `"ghost" \| "outline" \| "solid" \| "subtle" \| "surface" ...` | The variant of the component. | | `align` | - | `"-moz-initial" \| "anchor-center" \| "baseline" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "normal" ...` | The CSS `align-items` property. | | `basis` | - | `"-moz-fit-content" \| "-moz-initial" \| "-moz-max-content" \| "-moz-min-content" \| "-webkit-auto" \| "0.5" \| "1.5" \| "1" \| "1/12" \| "1/2" ...` | The CSS `flex-basis` property. | | `defaultValue` | - | `Y extends any[] ? Y : undefined \| Y` | The initial value of the toggle button group. | | `direction` | - | `"-moz-initial" \| "column-reverse" \| "column" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "row-reverse" \| "row" \| "unset" ...` | The CSS `flex-direction` property. | | `disabled` | `false` | `boolean` | If `true`, all wrapped toggle button will be disabled. | | `justify` | - | `"-moz-initial" \| "center" \| "end" \| "flex-end" \| "flex-start" \| "inherit" \| "initial" \| "left" \| "normal" \| "revert-layer" ...` | The CSS `justify-content` property. | | `onChange` | - | `(value: Y extends any[] ? Y : undefined \| Y) => void` | The callback fired when any children toggle button is selected or unselected. | | `readOnly` | `false` | `boolean` | If `true`, all wrapped toggle button will be readonly. | | `shrink` | - | `"-moz-initial" \| "inherit" \| "initial" \| "revert-layer" \| "revert" \| "unset" \| AnyString \| number & {} ...` | The CSS `flex-shrink` property. | | `value` | - | `Y extends any[] ? Y : undefined \| Y` | The value of the toggle button group. | | `wrap` | - | `"-moz-initial" \| "inherit" \| "initial" \| "nowrap" \| "revert-layer" \| "revert" \| "unset" \| "wrap-reverse" \| "wrap" \| AnyString ...` | The CSS `flex-wrap` property. | ### ToggleGroup.Item | Prop | Default | Type | Description | | ------------------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"2xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"` | The size of the component. | | `variant` | `"ghost"` | `"ghost" \| "outline" \| "solid" \| "subtle" \| "surface" ...` | The variant of the component. | | `active` | `false` | `boolean` | If `true`, the button is represented as active. | | `checked` | - | `boolean` | If `true`, the toggle button will be checked. | | `disabled` | `false` | `boolean` | If `true`, the button is disabled. | | `disableRipple` | `false` | `boolean` | If `true`, disable ripple effects when pressing a element. | | `errorBorderColor` | - | `"-moz-initial" \| "AccentColor" \| "AccentColorText" \| "ActiveBorder" \| "ActiveCaption" \| "ActiveText" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" ...` | The border color when the input is invalid. | | `icon` | - | `string \| number \| bigint \| boolean \| ReactElement> \| Iterable \| ReactPortal \| Promise<...>` | The icon to be used in the button. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `loading` | `false` | `boolean` | If `true`, the loading state of the button is represented. | | `onChange` | - | `(checked: boolean) => void` | The callback invoked when checked state changes. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `type` | `"button"` | `"button" \| "reset" \| "submit"` | The type of button. Accepts `button`, `reset`, or `submit`. | | `value` | - | `Y` | The value of the toggle button. | ## Accessibility Setting an `aria-label` on `Toggle` or `ToggleGroup.Root` allows screen readers to announce it, helping users understand the purpose and functionality of the control or group. ```tsx } aria-label="Toggle bold" /> ``` ```tsx } aria-label="Toggle bold" /> } aria-label="Toggle italic" /> } aria-label="Toggle underline" /> ``` When combining `Toggle` with text, set an `aria-label` that includes the visible text. If omitted, the default `aria-label` is used, which may cause a mismatch between the text and the accessible name. ```tsx Italic ``` ### Keyboard Navigation | Key | Description | State | | ---------------- | ---------------------------- | ----- | | `Tab` | Focuses the element. | - | | `Space`, `Enter` | Toggles the focused element. | - | ### ARIA Roles and Attributes | Element | Roles and Attributes | Usage | | ------------------------ | -------------------- | ------------------------------------------------------------------------- | | `div.ui-toggle__group` | `role="group"` | Indicates that it's a group. | | `button.ui-toggle__root` | `aria-pressed` | Set to `true` if the element is pressed, or `false` if it is not pressed. | | | `aria-disabled` | Set to `"true"` if `disabled` or `readOnly` is set. | | | `aria-label` | Set `value` if provided, otherwise default to `"Toggle button"`. | ## Similar Components - [Checkbox](https://yamada-ui.com/docs/components/checkbox.md): `Checkbox` is a component used for allowing users to select multiple values from multiple options. - [NativeSelect](https://yamada-ui.com/docs/components/native-select.md): `NativeSelect` is a component used for allowing users to select one value from a list of options. It displays a native dropdown list provided by the browser (user agent). - [Radio](https://yamada-ui.com/docs/components/radio.md): `Radio` is a component used for allowing users to select one option from multiple choices. - [Rating](https://yamada-ui.com/docs/components/rating.md): `Rating` is a component used to allow users to provide ratings. - [SegmentedControl](https://yamada-ui.com/docs/components/segmented-control.md): `SegmentedControl` is a component used for allowing users to select one option from multiple choices. - [Switch](https://yamada-ui.com/docs/components/switch.md): `Switch` is a component used to toggle between on and off states. - [CheckboxCard](https://yamada-ui.com/docs/components/checkbox-card.md): `CheckboxCard` is a component used for allowing users to select multiple values from multiple options. - [FileButton](https://yamada-ui.com/docs/components/file-button.md): `FileButton` is a button component used for users to select files. ## Uses Components & Hooks - [Group](https://yamada-ui.com/docs/components/group.md): `Group` is a component that groups and attaches multiple elements together. - [Button](https://yamada-ui.com/docs/components/button.md): `Button` is an interactive component that allows users to perform actions such as submitting forms and toggling modals. - [Input](https://yamada-ui.com/docs/components/input.md): `Input` is a component used to obtain text input from the user. - [Portal](https://yamada-ui.com/docs/components/portal.md): `Portal` is a component that renders elements outside of the current `DOM` hierarchy. - [Field](https://yamada-ui.com/docs/components/field.md): `Field` is a component used to group form elements with label, helper message, error message, etc.