Try 'magnetic button'
View code

Specification

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

Notes

Questions that open their answer under them with no jump, by click or keyboard.

Every business site has this section. The whole question is the button, the answer glides open and closes a little faster, and the arrow keys move between questions.

Opens 280ms cubic-bezier(.22, 1, .36, 1), closes 200ms ease-in (grid rows 0fr to 1fr). Closed answers are inert, so they are skipped by Tab and screen readers. Up and Down arrows, Home and End move between questions. Unlike Image panels that widen: text answers, opened by a click, several open at once.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useId, useRef, useState, type KeyboardEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./faq-opens-smoothly.css";

const FAQ = [
  { q: "Are you taking new patients?", a: "Yes, for private check-ups. Book online or call the practice, and we will ask a few questions about your last visit." },
  { q: "How much is a check-up?", a: "A check-up with the dentist is £55, including small X-rays when you need them. Hygiene visits are booked separately." },
  { q: "Can I bring my children?", a: "Yes. Children under 18 are seen with a parent or guardian, and we can book family visits back to back." },
  { q: "What if I need to cancel?", a: "Tell us 24 hours ahead and there is no charge. Later than that, we may charge half the appointment fee." },
  { q: "Do you see emergencies?", a: "We keep time for urgent appointments each weekday morning. Call when we open at 8:15 and we will offer the first slot available." },
];

function AccordionDemo() {
  const [open, setOpen] = useState<Set<number>>(() => new Set([0]));
  const uid = useId();
  const heads = useRef<(HTMLButtonElement | null)[]>([]);
  const toggle = (i: number) =>
    setOpen((o) => {
      const n = new Set(o);
      if (n.has(i)) n.delete(i);
      else n.add(i);
      return n;
    });
  const onKey = (e: KeyboardEvent, i: number) => {
    const map: Record<string, number> = { ArrowDown: (i + 1) % FAQ.length, ArrowUp: (i - 1 + FAQ.length) % FAQ.length, Home: 0, End: FAQ.length - 1 };
    const n = map[e.key];
    if (n === undefined) return;
    e.preventDefault();
    heads.current[n]?.focus();
  };
  return (
    <div className="dm">
      <div className="faq">
        {FAQ.map((f, i) => {
          const isOpen = open.has(i);
          return (
            <div key={f.q} className="faq-item" data-open={isOpen || undefined}>
              <h3>
                <button
                  ref={(el) => {
                    heads.current[i] = el;
                  }}
                  type="button"
                  id={`${uid}-h${i}`}
                  aria-expanded={isOpen}
                  aria-controls={`${uid}-a${i}`}
                  onClick={() => toggle(i)}
                  onKeyDown={(e) => onKey(e, i)}
                >
                  {f.q}
                  <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
                    <path d="M5 12h14" />
                    <path d="M12 5v14" className="faq-v" />
                  </svg>
                </button>
              </h3>
              <div className="faq-a" id={`${uid}-a${i}`} role="region" aria-labelledby={`${uid}-h${i}`} inert={!isOpen}>
                <div>
                  <p>{f.a}</p>
                </div>
              </div>
            </div>
          );
        })}
      </div>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
// One answer open at a time, so the card never needs to scroll.
const TOUR: Tour = [
  { click: "button", nth: 0, wait: 600 },
  { click: "button", nth: 2, wait: 1600 },
  { click: "button", nth: 2, wait: 600 },
  { click: "button", nth: 0, wait: 1200 },
];

export default function FaqOpensSmoothlyEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="FAQ that opens smoothly" sample tour={TOUR}>
      <AccordionDemo />
    </PieceStage>
  );
}

More like this

Read more that glides open

Layout AnimationText and code

Form in named steps

Type, FeedbackForms and inputs

Menu that opens into link cards

Press, Layout AnimationNavigation

Cookie choice that folds away

FeedbackOverlays and toasts

Height that glides

Layout AnimationCards and accordions

Select with a gliding highlight

HoverForms and inputs