Components
Disclosure Group
A stack of related disclosures, often called an accordion, where opening one can close the others.
Introduction
Disclosure Group puts several disclosures in one list and coordinates them. It is the accordion: a FAQ, a settings page split in sections, the steps of a form. Give the group one look, and it dresses every item the same way.
- By default opening one item closes the one that was open. Set
allowsMultipleExpandedto let several stay open. - Each trigger is a button in the tab order: Enter or Space opens the focused one, and screen readers hear whether it is expanded.
- Which items are open is a set of their
ids. Start withdefaultExpandedKeys, or control it withexpandedKeys. - Lines between the items (
flush), one box (bordered), or a box each (separated). One item, or the whole group, can be disabled.
For a single section on its own, use Disclosure.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# 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 clsxManual installation
Disclosure Group shares its files with Disclosure. If you already added Disclosure, there is nothing more to copy.
1. Add the styles and the context
src/types/disclosure-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Disclosure. react-aria puts `data-expanded` on the root, and the
// chevron reads it through the `group/disclosure` name. The panel animates its
// height from the `--disclosure-panel-height` variable react-aria keeps up to
// date, so it opens and closes smoothly to whatever size the content has.
export const disclosureStyles = tv({
slots: {
root: "group/disclosure",
heading: "flex",
trigger: [
"flex w-full cursor-pointer items-center gap-3 text-left font-medium outline-none transition-colors",
"data-focus-visible:ring-2 data-focus-visible:ring-ring data-focus-visible:ring-inset",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
"[&_svg]:shrink-0",
],
icon: "text-muted-fg [&_svg]:size-4",
chevron:
"ml-auto size-4 text-muted-fg transition-transform duration-200 group-data-expanded/disclosure:rotate-180 motion-reduce:transition-none",
panel:
"h-(--disclosure-panel-height) overflow-clip transition-[height] duration-200 motion-reduce:transition-none",
content: "text-muted-fg",
},
variants: {
variant: {
// Lines between the items, no box: for pages and long lists.
flush: {},
// One rounded box around all the items.
bordered: {
root: "overflow-hidden",
trigger: "data-hovered:bg-muted/60",
},
// Each item is its own box, with a gap between them.
separated: {
root: "overflow-hidden rounded-xl border",
trigger: "data-hovered:bg-muted/60",
},
},
size: {
sm: { trigger: "px-3 py-2 text-sm", content: "px-3 pb-3 text-sm" },
md: { trigger: "px-4 py-3 text-sm", content: "px-4 pb-4 text-sm" },
lg: { trigger: "px-5 py-4 text-base", content: "px-5 pb-5 text-base" },
},
// Inside a DisclosureGroup, which draws the box or the lines itself.
grouped: {
true: {},
false: {},
},
},
compoundVariants: [
{ variant: "flush", grouped: false, class: { root: "border-y" } },
{
variant: "bordered",
grouped: false,
class: { root: "rounded-xl border" },
},
// Flush items sit on the page, so their text lines up with it.
{
variant: "flush",
size: "sm",
class: { trigger: "px-0", content: "px-0" },
},
{
variant: "flush",
size: "md",
class: { trigger: "px-0", content: "px-0" },
},
{
variant: "flush",
size: "lg",
class: { trigger: "px-0", content: "px-0" },
},
],
defaultVariants: { variant: "bordered", size: "md", grouped: false },
});
export const disclosureGroupStyles = tv({
base: "w-full",
variants: {
variant: {
flush: "divide-y border-y",
bordered: "divide-y overflow-hidden rounded-xl border",
separated: "flex flex-col gap-2",
},
},
defaultVariants: { variant: "bordered" },
});src/types/disclosure-context.tsShow code
import { createContext, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { disclosureStyles } from "./disclosure-styles";
/** What a Disclosure tells its trigger and panel, and a group tells its disclosures. */
export type DisclosureContextValue = Required<
Pick<VariantProps<typeof disclosureStyles>, "variant" | "size">
> & { grouped: boolean };
const DisclosureContext = createContext<DisclosureContextValue | null>(null);
export const DisclosureProvider = DisclosureContext.Provider;
/** The nearest Disclosure or DisclosureGroup's look, or null outside of both. */
export function useDisclosureLook() {
return useContext(DisclosureContext);
}2. Add the Disclosure file
src/components/ui/disclosure/disclosure.tsxShow code
"use client";
import type { ReactNode } from "react";
import {
Button as AriaButton,
Disclosure as AriaDisclosure,
DisclosureGroup as AriaDisclosureGroup,
type DisclosureGroupProps as AriaDisclosureGroupProps,
DisclosurePanel as AriaDisclosurePanel,
type DisclosureProps as AriaDisclosureProps,
Heading,
} from "react-aria-components";
import { ChevronDownIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import {
DisclosureProvider,
useDisclosureLook,
} from "@/types/disclosure-context";
import {
disclosureGroupStyles,
disclosureStyles,
} from "@/types/disclosure-styles";
type Variant = "flush" | "bordered" | "separated";
type Size = "sm" | "md" | "lg";
export interface DisclosureGroupProps
extends Omit<AriaDisclosureGroupProps, "children" | "className" | "style"> {
/** The `Disclosure`s. Give each an `id`. */
children: ReactNode;
/**
* `flush`: lines between the items. `bordered`: one rounded box around all
* of them. `separated`: each item is its own box.
*/
variant?: Variant;
/** Passed down to every disclosure in the group. */
size?: Size;
className?: string;
}
/**
* A stack of related `Disclosure`s, usually called an accordion. By default
* opening one closes the others; set `allowsMultipleExpanded` to let several
* stay open.
*
* Which ones are open is a set of the `id`s of the disclosures: start with
* `defaultExpandedKeys`, or control it with `expandedKeys` and
* `onExpandedChange`.
*
* @example
* <DisclosureGroup defaultExpandedKeys={["a"]}>
* <Disclosure id="a">
* <DisclosureTrigger>What is centerUI?</DisclosureTrigger>
* <DisclosurePanel>A component library.</DisclosurePanel>
* </Disclosure>
* </DisclosureGroup>
*/
export function DisclosureGroup({
variant = "bordered",
size = "md",
className,
children,
...props
}: DisclosureGroupProps) {
return (
<DisclosureProvider value={{ variant, size, grouped: true }}>
<AriaDisclosureGroup
{...props}
className={cn(disclosureGroupStyles({ variant }), className)}
>
{children}
</AriaDisclosureGroup>
</DisclosureProvider>
);
}
export interface DisclosureProps
extends Omit<AriaDisclosureProps, "children" | "className" | "style"> {
/** A `DisclosureTrigger` and a `DisclosurePanel`. */
children: ReactNode;
/** Ignored inside a `DisclosureGroup`, which sets it for all of them. */
variant?: Variant;
/** Ignored inside a `DisclosureGroup`, which sets it for all of them. */
size?: Size;
className?: string;
}
/**
* A section whose content can be shown and hidden: a trigger that is always
* visible, and a panel that opens under it. Built on react-aria's Disclosure,
* so the trigger is a real button that reports `aria-expanded`, and the panel
* can be found by the browser's find-in-page while it is closed.
*
* Uncontrolled by default. Start open with `defaultExpanded`, or control it
* with `isExpanded` and `onExpandedChange`.
*
* @example
* <Disclosure>
* <DisclosureTrigger>Shipping</DisclosureTrigger>
* <DisclosurePanel>Arrives in 2 to 3 days.</DisclosurePanel>
* </Disclosure>
*/
export function Disclosure({
variant,
size,
className,
children,
...props
}: DisclosureProps) {
const group = useDisclosureLook();
// In a group, the group decides the look.
const look = {
variant: group?.grouped ? group.variant : (variant ?? "bordered"),
size: group?.grouped ? group.size : (size ?? "md"),
grouped: Boolean(group?.grouped),
};
const styles = disclosureStyles(look);
return (
<DisclosureProvider value={look}>
<AriaDisclosure {...props} className={cn(styles.root(), className)}>
{children}
</AriaDisclosure>
</DisclosureProvider>
);
}
export interface DisclosureTriggerProps {
children: ReactNode;
/** An icon before the text. */
icon?: ReactNode;
/** The level of the heading that holds the button. Defaults to 3. */
level?: 1 | 2 | 3 | 4 | 5 | 6;
className?: string;
}
/**
* The always-visible part of a disclosure: a button, inside a heading, that
* opens and closes the panel. The chevron turns when it is open.
*/
export function DisclosureTrigger({
children,
icon,
level = 3,
className,
}: DisclosureTriggerProps) {
const look = useDisclosureLook();
const styles = disclosureStyles(look ?? {});
return (
<Heading level={level} className={styles.heading()}>
<AriaButton slot="trigger" className={cn(styles.trigger(), className)}>
{icon ? <span className={styles.icon()}>{icon}</span> : null}
<span className="min-w-0 flex-1">{children}</span>
<ChevronDownIcon className={styles.chevron()} />
</AriaButton>
</Heading>
);
}
export interface DisclosurePanelProps {
children: ReactNode;
className?: string;
}
/** The content that opens under the trigger. */
export function DisclosurePanel({ children, className }: DisclosurePanelProps) {
const look = useDisclosureLook();
const styles = disclosureStyles(look ?? {});
return (
<AriaDisclosurePanel className={styles.panel()}>
<div className={cn(styles.content(), className)}>{children}</div>
</AriaDisclosurePanel>
);
}It imports ChevronDownIcon from @/components/icons.
3. Use it
import {
Disclosure,
DisclosureGroup,
DisclosurePanel,
DisclosureTrigger,
} from "@/components/ui/disclosure/disclosure";Anatomy
A group around several disclosures, each with an id.
<DisclosureGroup defaultExpandedKeys={["what"]}>
<Disclosure id="what">
<DisclosureTrigger>What is centerUI?</DisclosureTrigger>
<DisclosurePanel>A component library.</DisclosurePanel>
</Disclosure>
<Disclosure id="free">
<DisclosureTrigger>Is it free?</DisclosureTrigger>
<DisclosurePanel>Yes.</DisclosurePanel>
</Disclosure>
</DisclosureGroup>| Part | Rendered as | Notes |
|---|---|---|
| DisclosureGroup | div | Draws the box or the lines, and decides which disclosures are open. |
| Disclosure | div | One item. Needs an id, so the group can tell which ones are open. |
| DisclosureTrigger / DisclosurePanel | button in a heading / group | The parts of every item, the same as on their own. |
Examples
Open an item, then open another.
Usage
Multiple open
Opening an item no longer closes the others.
Open by default
defaultExpandedKeys lists the ids that start open. Without allowsMultipleExpanded, give it one.
Variants
Flush draws lines between the items and lines up with the page. Bordered, the default, is one rounded box. Separated gives each item its own box.
Sizes
With icons
Disabled
Disable one item, or the whole group.
Controlled
Keep the open items in your own state, as a Set of ids, to open or close them from outside.
expandedKeys: [shipping]
Component API
The parts of every item are the ones documented under Disclosure. The props below are the ones a group adds.
DisclosureGroup
| Prop | Type | Default |
|---|---|---|
| children | Disclosure[] | - (required) |
| allowsMultipleExpanded | boolean | false |
| expandedKeys | Iterable<Key> | - |
| defaultExpandedKeys | Iterable<Key> | - |
| onExpandedChange | (keys: Set<Key>) => void | - |
| isDisabled | boolean | false |
| variant | "flush" | "bordered" | "separated" | "bordered" |
| size | "sm" | "md" | "lg" | "md" |
| className | string | - |
Disclosure, inside a group
| Prop | Type | Default |
|---|---|---|
| id | Key | - (required in a group) |
| isDisabled | boolean | false |
| className | string | - |
Inside a group, a disclosure ignores its own variant and size: the group sets them for all.