Skip to content
Inspireca Design

Interactive Example · Cards

Card that expands in place

A summary card that opens to show its details without leaving the page, built on a real button and a height animation that needs no measuring.

Written by Inspireca Design (drafted with AI assistance)Updated Not yet reviewed by an editor

Try it

Use it with a mouse, by touch or with the keyboard. The controls below show its other states.

Items
3
Carrier
Northline
Total
€84.00

Track this order

Preview controls

How it works

Expanding in place lets someone check a detail and carry on, without a trip to another screen and back.

What moves

  • The details area grows from nothing to its full height in 200 milliseconds. Whatever is below the card moves down with it.
  • The arrow turns half a circle to point up.

How the height animates

Heights are awkward to animate because the browser cannot move smoothly to "whatever height the content needs". The demo wraps the details in a grid with a single row and animates that row from 0fr to 1fr. The content sets the final height, and no script has to measure anything.

This does animate layout: every frame, the content below is moved. For a short panel, as here, that is fine. For a long list of cards, or a very tall panel, test it on a slow phone and consider opening without animation.

When to use it

For secondary details of an item in a list: an order, a booking, a setting with options. If the details are the main thing people came for, show them from the start. The "Start open" control shows the opened state.

Accessibility notes

  • The header is a button inside a heading, with aria-expanded and aria-controls, as in the Disclosure pattern of the WAI-ARIA Authoring Practices. It works with Enter and Space.
  • Hidden means hidden. While closed, the details are marked inert, so the link inside cannot be reached with Tab or read by a screen reader.
  • The whole header is the target, 56 px tall, not just the small arrow.
  • Reduced motion: it opens and closes at once and the arrow flips without turning.
  • Focus stays on the button after opening, so pressing it again closes the card.

The code

This is the source of the preview above: React with Tailwind CSS classes (expandable-card.tsx). Colour names such as bg-primary and the ex- animation classes come from this site’s own stylesheet; replace them with your own.

"use client";

import { useId, useState } from "react";
import { ChevronDown } from "lucide-react";
import type { DemoProps } from "@/lib/examples/keys";

/**
 * A card with details that open in place. The whole header is one real button, so it works
 * with a keyboard and says "expanded" or "collapsed". The height animates with a grid row
 * going from 0fr to 1fr, which needs no measuring in JavaScript.
 */
export default function ExpandableCard({ values }: DemoProps) {
  const id = useId();
  const [open, setOpen] = useState(values.open === true);

  return (
    <div className="w-full max-w-sm rounded-2xl border border-border bg-background text-left">
      <h3>
        <button type="button" aria-expanded={open} aria-controls={`${id}-details`} onClick={() => setOpen(!open)}
          className="flex min-h-14 w-full items-center justify-between gap-4 rounded-2xl px-5 py-3 text-left outline-none focus-visible:ring-2 focus-visible:ring-foreground">
          <span>
            <span className="block text-[15px] font-semibold">Order 1042</span>
            <span className="block text-sm text-muted-foreground">Arrives Thursday</span>
          </span>
          <ChevronDown aria-hidden className={`size-5 shrink-0 transition-transform duration-200 ease-out motion-reduce:transition-none ${open ? "rotate-180" : ""}`} />
        </button>
      </h3>
      <div id={`${id}-details`} className={`grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none ${open ? "grid-rows-[1fr]" : "grid-rows-[0fr]"}`}>
        {/* inert keeps hidden links and buttons out of the Tab order while closed. */}
        <div className="overflow-hidden" inert={!open}>
          <dl className="grid grid-cols-2 gap-x-4 gap-y-2 border-t border-border px-5 py-4 text-sm">
            <dt className="text-muted-foreground">Items</dt><dd className="text-right">3</dd>
            <dt className="text-muted-foreground">Carrier</dt><dd className="text-right">Northline</dd>
            <dt className="text-muted-foreground">Total</dt><dd className="text-right font-medium">€84.00</dd>
          </dl>
          <p className="px-5 pb-2 text-sm"><a href="#" onClick={(e) => e.preventDefault()} className="inline-flex min-h-11 items-center font-medium underline underline-offset-4">Track this order</a></p>
        </div>
      </div>
    </div>
  );
}

See it in real apps

Still screenshots from the Inspireca Design library of the kinds of screen where this interaction is used. A screenshot cannot show the motion itself.

  • Interactive Examples

    Button press, loading and success feedback

    A button that dips when pressed, shows a spinner while it works and confirms when it is done, with words that carry the meaning at every step.

  • Interactive Examples

    Skeleton loading placeholder

    Grey shapes hold the place of content while it loads, in the same layout, so nothing jumps when the real content arrives.

  • Interactive Examples

    Tabs with a sliding indicator

    A marker that slides to the selected tab shows where you came from and where you are, with full arrow-key support.

  • Interactive Examples

    Dropdown menu with keyboard support

    A menu that opens from its button with a short fade and scale, and can be used completely from the keyboard.