Components
Toggle Group
A set of toggle buttons where one or several can be on. Built on react-aria-components, with the same look as Button.
Introduction
ToggleGroup is for choices that stay visible: a text alignment, a view mode, a billing period. Each item is a toggle button, and the group keeps track of which ones are on.
- Choose one item (
single) or several (multiple), and optionally never allow an empty selection. - Selection, keyboard navigation and ARIA roles come from react-aria-components.
- Joined into one control like a ButtonGroup, horizontal or vertical, attached or spaced apart, with every Button size.
- Icon-only items with isIcon.
Installation
centerUI has no CLI or registry yet: components are copied into your project. ToggleGroup is built on Toggle and on the ButtonGroup layout, so it needs both.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxAdd Button and ButtonGroup first (see their Manual installation), then follow the steps below.
Manual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add Toggle
Every item is a Toggle. Add it first (see its Manual installation), or copy these two files.
src/types/toggle-styles.tsShow code
import { tv } from "tailwind-variants";
import { buttonStyles } from "./button-styles";
// A toggle is a Button with a "selected" look: the selected one is filled with
// the primary color. Used by Toggle, and so by every ToggleGroup item. Hover and press keep the selected color, so
// the two states never fight over the background.
export const toggleStyles = tv({
extend: buttonStyles,
base: [
"data-selected:border-primary data-selected:bg-primary data-selected:text-primary-fg",
"data-selected:data-hovered:bg-primary/90 data-selected:data-pressed:bg-primary/80",
],
defaultVariants: {
variant: "outline",
},
});src/components/ui/toggle/toggle.tsxShow code
"use client";
import { ToggleButton, type ToggleButtonProps } from "react-aria-components";
import type { VariantProps } from "tailwind-variants";
import { cx } from "@/lib/primitive";
import { useButtonGroup } from "@/types/button-group-context";
import { toggleStyles } from "@/types/toggle-styles";
type Styles = VariantProps<typeof toggleStyles>;
/** Variants that read well when the selected state is filled with the primary color. */
export type ToggleVariant = Extract<
NonNullable<Styles["variant"]>,
"outline" | "ghost" | "secondary"
>;
export interface ToggleProps extends ToggleButtonProps {
/** Look of the toggle. Defaults to the enclosing group's variant, or `outline`. */
variant?: ToggleVariant;
/** Size of the toggle. Defaults to the enclosing group's size, or `md`. */
size?: Styles["size"];
/** Square toggle for an icon on its own. Give it an `aria-label`. */
isIcon?: boolean;
}
/**
* A button that stays on or off. Looks like a Button, and the "on" state is
* filled with the primary color. Built on react-aria's ToggleButton, so press,
* focus and `aria-pressed` are handled for you. It reads the defaults of a
* ButtonGroup or ToggleGroup around it.
*
* `children` can be a function of `{ isSelected }` to change the content with
* the state.
*
* @example
* <Toggle aria-label="Bookmark" onChange={setBookmarked}>
* <BookmarkIcon />
* </Toggle>
*/
export function Toggle({
variant,
size,
isIcon,
className,
...props
}: ToggleProps) {
const group = useButtonGroup();
return (
<ToggleButton
{...props}
// A group or a toolbar that is disabled disables its toggles too.
isDisabled={props.isDisabled ?? group?.isDisabled}
className={cx(
toggleStyles({
variant: variant ?? (group?.variant as ToggleVariant | undefined),
size: size ?? group?.size,
isIcon,
}),
className,
)}
/>
);
}2. Add the ToggleGroup file
src/components/ui/toggle-group/toggle-group.tsxShow code
"use client";
import { type ReactNode, useMemo } from "react";
import {
ToggleButtonGroup,
type ToggleButtonGroupProps,
} from "react-aria-components";
import {
Toggle,
type ToggleProps,
type ToggleVariant,
} from "@/components/ui/toggle/toggle";
import { cn } from "@/lib/cn";
import { ButtonGroupProvider } from "@/types/button-group-context";
import { buttonGroupStyles } from "@/types/button-group-styles";
export interface ToggleGroupProps
extends Omit<ToggleButtonGroupProps, "className" | "children"> {
/** Look of every item. Items can override it. */
variant?: ToggleVariant;
/** Size of every item. Items can override it. */
size?: ToggleProps["size"];
/** Join the items into one control. `false` keeps them apart with a gap. */
isAttached?: boolean;
/** Stretch the group to the full width, with equally wide items. */
isFullWidth?: boolean;
className?: string;
children?: ReactNode;
}
/**
* A set of toggle buttons where one (`selectionMode="single"`, the default) or
* several (`"multiple"`) can be on. Built on react-aria's ToggleButtonGroup, so
* selection, keyboard navigation and ARIA roles come from there. Give it an
* accessible name with `aria-label`.
*
* @example
* <ToggleGroup aria-label="Text alignment" defaultSelectedKeys={["left"]}>
* <ToggleGroupItem id="left">Left</ToggleGroupItem>
* <ToggleGroupItem id="center">Center</ToggleGroupItem>
* </ToggleGroup>
*/
export function ToggleGroup({
variant = "outline",
size,
isAttached = true,
isFullWidth = false,
orientation = "horizontal",
isDisabled,
className,
children,
...props
}: ToggleGroupProps) {
// Same channel ButtonGroup uses, so the items read their defaults the same way.
const defaults = useMemo(
() => ({ variant, size, isDisabled }),
[variant, size, isDisabled],
);
return (
<ButtonGroupProvider value={defaults}>
<ToggleButtonGroup
{...props}
orientation={orientation}
isDisabled={isDisabled}
className={cn(
buttonGroupStyles({ orientation, isAttached, isFullWidth }),
className,
)}
>
{children}
</ToggleButtonGroup>
</ButtonGroupProvider>
);
}
export type ToggleGroupItemProps = ToggleProps;
/**
* One item of a ToggleGroup: a Toggle whose `id` is the key that appears in
* `selectedKeys`.
*/
export function ToggleGroupItem(props: ToggleGroupItemProps) {
return <Toggle {...props} />;
}3. Use it
import {
ToggleGroup,
ToggleGroupItem,
} from "@/components/ui/toggle-group/toggle-group";Anatomy
A toggle group is a container and the items inside it.
<ToggleGroup aria-label="Text alignment" defaultSelectedKeys={["left"]}>
<ToggleGroupItem id="left">Left</ToggleGroupItem>
<ToggleGroupItem id="center">Center</ToggleGroupItem>
<ToggleGroupItem id="right">Right</ToggleGroupItem>
</ToggleGroup><div role="radiogroup" aria-label="Text alignment"> <!-- ToggleGroup -->
<button role="radio" aria-checked="true">Left</button> <!-- ToggleGroupItem -->
<button role="radio" aria-checked="false">Center</button>
<button role="radio" aria-checked="false">Right</button>
</div>| Part | Rendered as | Notes |
|---|---|---|
| Group | div, radiogroup or group | Lays the items out, joins their corners, holds the selection and passes variant and size down. |
| Item | button (radio or toggle) | One choice. Its id is the key stored in the selection. The selected item is filled with the primary color. |
Give the group an accessible name with aria-label. Every item needs an id, and icon-only items need an aria-label too.
Examples
Live previews with code you can copy.
Usage
Selected: center
Multiple selection
Set selectionMode to "multiple" and any number of items can be on.
Selected: bold
Required selection
With disallowEmptySelection the last selected item cannot be turned off.
Selected: monthly (one is always on)
Orientation
Spacing
Set isAttached to false to keep the items apart.
Variants
The selected item is always filled with the primary color.
Sizes
Icons
Disabled
Disable the whole group, or a single item.
Full width
The group fills its container and the items share the width equally.
Component API
ToggleGroup and ToggleGroupItem take the props below, plus everything the react-aria ToggleButtonGroup and ToggleButton accept.
ToggleGroup
| Prop | Type | Default |
|---|---|---|
| selectionMode | "single" | "multiple" | "single" |
| selectedKeys | Iterable<Key> | - |
| defaultSelectedKeys | Iterable<Key> | - |
| onSelectionChange | (keys: Selection) => void | - |
| disallowEmptySelection | boolean | false |
| orientation | "horizontal" | "vertical" | "horizontal" |
| variant | "outline" | "secondary" | "ghost" | "outline" |
| size | "xxs" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "icon" | "md" |
| isAttached | boolean | true |
| isFullWidth | boolean | false |
| isDisabled | boolean | false |
| aria-label | string | - |
ToggleGroupItem
| Prop | Type | Default |
|---|---|---|
| id | Key | - |
| isIcon | boolean | false |
| isDisabled | boolean | false |
| variant | "outline" | "secondary" | "ghost" | the group's variant |
| size | same as the group | the group's size |
| aria-label | string | - |