Try 'magnetic button'
View code

Specification

Catalogue
No. 64 of 110
Interaction
Layout Animation
Element
Accordion
Preview
Live component
Imports
react 19.3.1
Added

Notes

The panel animates its height with grid-template-rows, from 0fr to 1fr, which needs no measuring and no JavaScript for the motion. The inner wrapper has min-height: 0 and overflow: hidden so the content clips while the row grows.

The plus icon is two bars; the vertical one rotates flat when the item opens. The answer text fades and rises slightly after the row starts to open, so it never appears before there is room for it.

Closed panels are inert, which keeps their links out of the tab order without removing them from the page.

Code

React and plain CSS
"use client";

import { useEffect, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./accordion-faq.module.css";

const ITEMS = [
  {
    q: "How does billing work?",
    a: "You pay per workspace, monthly or yearly. Changes apply from the next billing date.",
  },
  {
    q: "Can I invite my team?",
    a: "Yes. Invite people by email and choose whether they can edit or only comment.",
  },
  {
    q: "Where is my data stored?",
    a: "In the region you pick when you create the workspace. You can export it at any time.",
  },
];

const DEMO_ORDER = [0, 1, 2, -1];

export default function AccordionFaq({ active, context }: LivePreviewProps) {
  const [open, setOpen] = useState(0);
  const [step, setStep] = useState(0);
  const demo = active && context === "card";

  // Each time the demo starts, it starts from the first step.
  const [demoRunning, setDemoRunning] = useState(demo);
  if (demoRunning !== demo) {
    setDemoRunning(demo);
    setStep(0);
  }

  useEffect(() => {
    if (!demo) return;
    let id = window.setTimeout(function tick() {
      setStep((s) => s + 1);
      id = window.setTimeout(tick, 1200);
    }, 200);
    return () => window.clearTimeout(id);
  }, [demo]);

  const current = context === "card" ? (demo ? (DEMO_ORDER[step % DEMO_ORDER.length] ?? -1) : 0) : open;

  return (
    <div className={styles.root}>
      {ITEMS.map((item, i) => {
        const expanded = i === current;
        return (
          <div key={item.q} className={styles.item} data-open={expanded || undefined}>
            <h3 className={styles.heading}>
              <button
                type="button"
                id={`faq-q-${i}`}
                className={styles.trigger}
                aria-expanded={expanded}
                aria-controls={`faq-a-${i}`}
                tabIndex={context === "card" ? -1 : 0}
                onClick={() => setOpen(expanded ? -1 : i)}
              >
                {item.q}
                <span className={styles.icon} aria-hidden="true" />
              </button>
            </h3>
            <div id={`faq-a-${i}`} role="region" aria-labelledby={`faq-q-${i}`} className={styles.panel} inert={!expanded}>
              <div className={styles.panelInner}>
                <p className={styles.answer}>{item.a}</p>
              </div>
            </div>
          </div>
        );
      })}
    </div>
  );
}

More like this

Sliding tabs

Layout AnimationTabs

Height that glides

Layout AnimationCards and accordions

FAQ that opens smoothly

Layout AnimationCards and accordions

Tick slider with a wake

DragForms and inputs

Huddle you can join

Press, FeedbackCards and accordions

Read more that glides open

Layout AnimationText and code