kipluo
React7 min read4 steps

How to Animate an Accordion Open and Closed in React

CSS cannot transition height to auto in most browsers, which is why accordions so often snap open. This guide explains the three workarounds, builds an accordion with the grid-rows technique, and covers the ARIA pattern so it works with a screen reader and keyboard.

You needReact 18+CSS grid basicsuseId (React 18)
Loading demo
Open and close panels by click or with Enter and Space. Each panel settles on a spring instead of a fixed curve.Component page

1. Why height: auto doesn't animate

A transition needs two numbers to interpolate between. height: auto is a keyword, so the browser has nothing to tween from 0 to and the panel jumps. There are three common workarounds.

  • Measure scrollHeight in JavaScript and animate to that pixel value. Works everywhere, but you must re-measure when the content changes size.
  • Animate grid-template-rows from 0fr to 1fr. Supported in all current major browsers and needs no measuring.
  • Use interpolate-size: allow-keywords. Cleanest syntax, but at the time of writing only Chromium-based browsers support it.

2. Build the panel with grid-template-rows

The outer element is a one-row grid. The inner element has min-height: 0 and overflow: hidden, so when the row is 0fr the content collapses to nothing, and at 1fr it takes its natural height. Transitioning grid-template-rows animates between them.

The trigger is a real <button> inside a heading, with aria-expanded and aria-controls pointing at the panel. When closed, the panel gets visibility: hidden after the transition finishes, so links inside it are removed from the tab order and from the accessibility tree.

Accordion.tsx
import { useId, useState } from "react";

type Item = { title: string; body: string };

export function Accordion({ items }: { items: Item[] }) {
  const [open, setOpen] = useState<number | null>(null);
  const baseId = useId();

  return (
    <div className="divide-y rounded-xl border">
      {items.map((item, i) => {
        const isOpen = open === i;
        const buttonId = `${baseId}-button-${i}`;
        const panelId = `${baseId}-panel-${i}`;
        return (
          <div key={item.title}>
            <h3>
              <button
                id={buttonId}
                type="button"
                aria-expanded={isOpen}
                aria-controls={panelId}
                onClick={() => setOpen(isOpen ? null : i)}
                className="flex w-full items-center justify-between px-4 py-3 text-left font-medium"
              >
                {item.title}
                <span aria-hidden style={{ transition: "transform 200ms", transform: isOpen ? "rotate(180deg)" : "none" }}>
                  ▾
                </span>
              </button>
            </h3>
            <div
              id={panelId}
              role="region"
              aria-labelledby={buttonId}
              className="accordion-panel"
              data-open={isOpen}
            >
              <div className="accordion-inner">
                <p className="px-4 pb-4 text-sm">{item.body}</p>
              </div>
            </div>
          </div>
        );
      })}
    </div>
  );
}

3. The CSS that does the animation

visibility is a discrete property: when transitioning to hidden it waits for the full duration before switching, and when transitioning to visible it switches immediately. That gives you a panel that animates closed and only then leaves the tab order.

accordion.css
.accordion-panel {
  display: grid;
  grid-template-rows: 0fr;
  visibility: hidden;
  transition: grid-template-rows 250ms ease, visibility 250ms;
}

.accordion-panel[data-open="true"] {
  grid-template-rows: 1fr;
  visibility: visible;
}

.accordion-inner {
  min-height: 0;
  overflow: hidden;
}

@media (prefers-reduced-motion: reduce) {
  .accordion-panel {
    transition: none;
  }
}

4. A spring-driven version with Kipluo

The CSS version runs on a fixed duration. If a user clicks the same header twice quickly, the reversal restarts the curve. Kipluo's free Elastic Accordion measures each panel and drives its height with a spring instead, so a mid-animation reversal keeps its velocity and the panel settles with a slight stretch.

It takes an items array of { title, body } and a multiple prop to allow several panels open at once.

Faq.tsx
// npx kipluo add elastic-accordion
import { ElasticAccordion } from "@/components/kipluo/elastic-accordion";

const faq = [
  { title: "Is it a subscription?", body: "No. One payment." },
  { title: "Does it work with Next.js?", body: "Yes, as client components." },
];

export function Faq() {
  return <ElasticAccordion items={faq} />;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 4 checked

Build it yourself or install it

Build it yourself when

A static FAQ where a fixed 250 ms transition is fine. The grid-rows approach is about 40 lines.

Install Kipluo when

You want interruptible spring motion, open and close sounds, theme styles and a matching Flutter widget.

Keep building