Menu

Components

Disclosure

A section with a trigger that shows and hides its content, for details most people do not need at first glance.

Introduction

Disclosure hides content behind a trigger: advanced settings, the fine print of a plan, an answer to a question. What matters is always visible, and the rest is one click away. It is the single form of what other libraries call a collapsible.

  • The trigger is a real button that reports aria-expanded, inside a heading, so screen readers list it in the page outline. Enter and Space toggle it.
  • The panel animates to the height of its content, whatever it is, and is skipped for people who prefer reduced motion. While closed, the browser's find-in-page can still find its text, and opens it.
  • Uncontrolled by default. Start open with defaultExpanded, or control it with isExpanded.
  • Three looks: lines only, a box, or (in a group) separate boxes.

For several related sections where opening one closes the others, use Disclosure Group.

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 clsx

Manual installation

Disclosure and Disclosure Group share the same files.

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. Add the icon, or swap it for your own.

3. Use it

import {
  Disclosure,
  DisclosurePanel,
  DisclosureTrigger,
} from "@/components/ui/disclosure/disclosure";

Anatomy

A trigger that is always there, and a panel that opens under it.

<Disclosure>
  <DisclosureTrigger icon={<BookIcon />}>Shipping</DisclosureTrigger>
  <DisclosurePanel>Orders arrive in 2 to 3 working days.</DisclosurePanel>
</Disclosure>
PartRendered asNotes
DisclosuredivHolds the open or closed state. Marks itself with data-expanded while open.
DisclosureTriggerbutton in a headingAlways visible. A real button with aria-expanded, inside a heading so it shows up in the page outline.
IconspanOptional, before the text, set with the icon prop.
ChevronsvgAt the end. It turns upside down while the panel is open.
DisclosurePaneldiv (group)The content. It animates its height, and stays findable by the browser's find-in-page while closed.

Examples

Click a trigger, or focus it and press Enter or Space.

Usage

Open by default

defaultExpanded starts it open, and it stays uncontrolled.

Send anything back within 30 days, unused and in its box, and we refund it.

With icon

An icon before the text helps the trigger be found in a longer list.

Variants

Flush has a line above and below and lines up with the page. Bordered, the default, is a rounded box.

Sizes

Some content under the trigger.

Some content under the trigger.

Some content under the trigger.

Heading level

The trigger sits in a heading, level 3 by default. Set level so it fits the outline of your page: under an h2, use 3.

Disabled

Rich content

The panel holds anything. Its height follows the content.

These change how events are delivered to your server.

  • Retries stop after 5 failed attempts.
  • Each attempt waits twice as long as the last.
  • Events older than 3 days are dropped.

Controlled

Keep it in your own state with isExpanded and onExpandedChange, to open it from elsewhere.

isExpanded: false

Component API

Disclosure is made of the parts below.

Disclosure

PropTypeDefault
childrenReactNode- (required)
isExpandedboolean-
defaultExpandedbooleanfalse
onExpandedChange(isExpanded: boolean) => void-
isDisabledbooleanfalse
variant"flush" | "bordered" | "separated""bordered"
size"sm" | "md" | "lg""md"
idKey (inside a DisclosureGroup)-
classNamestring-

DisclosureTrigger

PropTypeDefault
childrenReactNode- (required)
iconReactNode-
level1 | 2 | 3 | 4 | 5 | 63
classNamestring-

DisclosurePanel

PropTypeDefault
childrenReactNode- (required)
classNamestring-