Dev Helpers
← Back

Controlled disclosure button

Import and render <ControlledDisclosureButton /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/ControlledDisclosureButton.tsx

import { useId, useState } from "react";

import type { ReactNode } from "react";

export default function ControlledDisclosureButton({
  title = "Delivery",
  children = "Arrives in two days.",
}: { title?: string; children?: ReactNode } = {}) {
  const [open, set] = useState(false);
  const panel = useId();
  return (
    <section>
      <button aria-controls={panel} aria-expanded={open} onClick={() => set(!open)}>
        {title}
      </button>
      <div id={panel} hidden={!open}>
        {children}
      </div>
    </section>
  );
}