← Components

Expandable Panel

Photo strips that open wide on hover, focus or tap.

Layout · FreeInstall ↓
Open preview ↗
Loading the demoNew tab ↗

Customize

0
6.0 x
12 px
24 px
0.90 s
Your code
<ExpandablePanel panels={panels} />

Changes show live and stay in the address, so a link keeps them.

Code

"use client";

import { useRef, useState, type KeyboardEvent } from "react";
import { gsap } from "gsap";
import { useGSAP } from "@gsap/react";

// An accordion of photos. One strip is wide open, the rest are slim; hover, focus or tap another
// and the room moves over to it. Each strip is a real button, so the keyboard works too.
// GSAP tweens flex-grow (not width), so the browser shares the space for us and every strip
// stays exactly as tall as the box. On narrow boxes the strips stack and open downwards.

export type ExpandablePanelItem = {
  /** The photo's address. */
  image: string;
  /** Describes the photo for screen readers. */
  alt: string;
  /** The label shown on the open strip, and the accessible name of the button. */
  title: string;
  /** A small line under the title, optional. */
  caption?: string;
};

export type ExpandablePanelProps = {
  /** The strips, in order. Four to eight look best. */
  panels: ExpandablePanelItem[];
  /** What opens a strip: "hover" (mouse), or "click". Tap and keyboard focus always open one. */
  trigger?: "hover" | "click";
  /** Which strip starts open. -1 starts with all of them closed. */
  defaultActive?: number;
  /** Go back to the first choice when the pointer leaves the whole row. */
  resetOnLeave?: boolean;
  /** How much wider the open strip is than a closed one (1 is the same width). */
  expandedRatio?: number;
  /** Space between the strips, in pixels. */
  gap?: number;
  /** Corner radius of each strip, in pixels. */
  radius?: number;
  /** How long the strips take to change size, in seconds. */
  duration?: number;
  /** The GSAP ease of the change, e.g. "power3.inOut" or "expo.out". */
  ease?: string;
  /** "auto" stacks the strips when the box is narrower than 672px. */
  orientation?: "auto" | "horizontal" | "vertical";
  /** Called with the new strip's index whenever the open one changes. */
  onChange?: (index: number) => void;
  /** Extra classes for the outer box. Give it a size (it fills it). */
  className?: string;
  /** Extra classes for each strip. */
  panelClassName?: string;
};

export function ExpandablePanel({
  panels,
  trigger = "hover",
  defaultActive = 0,
  resetOnLeave = false,
  expandedRatio = 6,
  gap = 12,
  radius = 24,
  duration = 0.9,
  ease = "expo.out",
  orientation = "auto",
  onChange,
  className = "",
  panelClassName = "",
}: ExpandablePanelProps) {
  const listRef = useRef<HTMLDivElement>(null);
  const buttonRefs = useRef<(HTMLButtonElement | null)[]>([]);
  const [active, setActive] = useState(defaultActive);
  // The strips are drawn at their starting size by React; after that only GSAP changes them
  const startActive = useRef(defaultActive);
  const started = useRef(false);

  // When the defaultActive prop changes on a live strip (the playground does this), follow it
  const [seenDefault, setSeenDefault] = useState(defaultActive);
  if (seenDefault !== defaultActive) {
    setSeenDefault(defaultActive);
    setActive(defaultActive);
  }

  const open = (index: number) => {
    if (index === active) return;
    setActive(index);
    onChange?.(index);
  };

  useGSAP(
    () => {
      const buttons = buttonRefs.current.slice(0, panels.length).filter((button): button is HTMLButtonElement => button !== null);
      if (buttons.length === 0) return;

      const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
      // The first run only places things, so the page doesn't open with an animation nobody asked for
      const time = started.current && !reduced ? duration : 0;
      started.current = true;

      const isOpen = (i: number) => i === active;
      gsap.to(buttons, { flexGrow: (i: number) => (isOpen(i) ? expandedRatio : 1), duration: time, ease, overwrite: "auto" });
      // The photo eases from a slight zoom to rest, and closed strips are dimmed
      gsap.to(buttons.map((b) => b.querySelector("[data-photo]")), { scale: (i: number) => (isOpen(i) ? 1 : 1.12), duration: time * 1.1, ease, overwrite: "auto" });
      gsap.to(buttons.map((b) => b.querySelector("[data-shade]")), { opacity: (i: number) => (isOpen(i) ? 0 : 0.45), duration: time, ease: "power2.out", overwrite: "auto" });
      // The title slides up into view only on the open strip; on the others it fades out quickly
      const titles = buttons.map((b) => b.querySelector("[data-title]"));
      gsap.to(titles, {
        opacity: (i: number) => (isOpen(i) ? 1 : 0),
        y: (i: number) => (isOpen(i) || reduced ? 0 : 18),
        duration: time * 0.6,
        delay: (i: number) => (isOpen(i) && time > 0 ? 0.12 : 0),
        ease: "power3.out",
        overwrite: "auto",
      });
    },
    { scope: listRef, dependencies: [active, panels.length, expandedRatio, duration, ease] },
  );

  // Arrow keys walk along the strips, and landing on one opens it (focus opens)
  const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
    const keys = ["ArrowRight", "ArrowDown", "ArrowLeft", "ArrowUp", "Home", "End"];
    if (!keys.includes(event.key)) return;
    const current = buttonRefs.current.findIndex((button) => button === document.activeElement);
    if (current === -1) return;
    event.preventDefault();
    const last = panels.length - 1;
    const next = event.key === "Home" ? 0 : event.key === "End" ? last : event.key === "ArrowRight" || event.key === "ArrowDown" ? Math.min(last, current + 1) : Math.max(0, current - 1);
    buttonRefs.current[next]?.focus();
  };

  const direction = orientation === "vertical" ? "flex-col" : orientation === "horizontal" ? "flex-row" : "flex-row @max-2xl:flex-col";

  return (
    <div className={`@container relative h-full w-full ${className}`}>
      <div
        ref={listRef}
        role="group"
        aria-label="Expandable panels"
        onKeyDown={onKeyDown}
        onPointerLeave={() => resetOnLeave && open(defaultActive)}
        className={`flex h-full w-full ${direction}`}
        style={{ gap }}
      >
        {panels.map((panel, index) => (
          <button
            key={`${index}-${panel.image}`}
            ref={(button) => {
              buttonRefs.current[index] = button;
            }}
            type="button"
            aria-expanded={index === active}
            aria-label={panel.title}
            onPointerEnter={(event) => {
              // Touch fires an enter just before the tap, so only a real pointer counts as a hover
              if (trigger === "hover" && event.pointerType !== "touch") open(index);
            }}
            onClick={() => open(index)}
            onFocus={() => open(index)}
            className={`group relative isolate min-h-0 min-w-0 basis-0 cursor-pointer overflow-hidden border-0 bg-transparent p-0 text-left text-white outline-none focus-visible:ring-2 focus-visible:ring-white/90 focus-visible:ring-offset-2 focus-visible:ring-offset-black/60 ${panelClassName}`}
            style={{ flexGrow: index === startActive.current ? expandedRatio : 1, borderRadius: radius, WebkitTapHighlightColor: "transparent" }}
          >
            <img
              data-photo
              src={panel.image}
              alt={panel.alt}
              draggable={false}
              loading="lazy"
              decoding="async"
              className="absolute inset-0 -z-10 block h-full w-full object-cover"
              style={{ transform: index === startActive.current ? "scale(1)" : "scale(1.12)" }}
            />
            {/* Darkens closed strips, and the bottom of every strip so the white text reads */}
            <span aria-hidden className="pointer-events-none absolute inset-0 bg-black" data-shade style={{ opacity: index === startActive.current ? 0 : 0.45 }} />
            <span aria-hidden className="pointer-events-none absolute inset-x-0 bottom-0 h-2/5 bg-gradient-to-t from-black/70 to-transparent" />
            <span aria-hidden className="absolute top-4 left-5 font-mono text-[11px] tracking-[0.14em] opacity-80 @max-2xl:top-3">
              {String(index + 1).padStart(2, "0")}
            </span>
            <span data-title className="pointer-events-none absolute right-5 bottom-5 left-5 block whitespace-nowrap" style={{ opacity: index === startActive.current ? 1 : 0 }}>
              <span className="block text-[clamp(22px,2.6vw,40px)] leading-[1.05] font-medium tracking-[-0.03em]">{panel.title}</span>
              {panel.caption ? <span className="mt-1.5 block text-[13px] opacity-75">{panel.caption}</span> : null}
            </span>
          </button>
        ))}
      </div>
    </div>
  );
}

The component. Installs to components/dimaac/expandable-panel.tsx.

Install

With the shadcn CLI

npx shadcn@latest add https://ui.dimaac.com/r/expandable-panel.json

It adds components/dimaac/expandable-panel.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/expandable-panel.

By hand

npm install gsap @gsap/react
  1. Install the packages it uses: gsap, @gsap/react.
  2. Copy the component file below into components/dimaac/expandable-panel.tsx.
  3. It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
  4. Import it with import { ExpandablePanel } from "@/components/dimaac/expandable-panel". Give its parent a size: it fills it.

Props

PropTypeDefaultDescription
panels (required)ExpandablePanelItem[]-The strips, in order. Four to eight look best.
trigger"hover" | "click""hover"What opens a strip: "hover" (mouse), or "click". Tap and keyboard focus always open one.
defaultActivenumber0Which strip starts open. -1 starts with all of them closed.
resetOnLeavebooleanfalseGo back to the first choice when the pointer leaves the whole row.
expandedRationumber6How much wider the open strip is than a closed one (1 is the same width).
gapnumber12Space between the strips, in pixels.
radiusnumber24Corner radius of each strip, in pixels.
durationnumber0.9How long the strips take to change size, in seconds.
easestring"expo.out"The GSAP ease of the change, e.g. "power3.inOut" or "expo.out".
orientation"auto" | "horizontal" | "vertical""auto""auto" stacks the strips when the box is narrower than 672px.
onChange(index: number) => void-Called with the new strip's index whenever the open one changes.
classNamestring""Extra classes for the outer box. Give it a size (it fills it).
panelClassNamestring""Extra classes for each strip.

Use with AI

Agent prompt

Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.

Prompt
Add the Expandable Panel component (<ExpandablePanel />) from DiMaac UI to this project.

Docs: https://ui.dimaac.com/components/expandable-panel
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/expandable-panel.json

Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/expandable-panel.json

Then use it like this:
import { ExpandablePanel } from "@/components/dimaac/expandable-panel";
<ExpandablePanel panels={panels} />

If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/expandable-panel.tsx and install gsap, @gsap/react.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size: it fills it. Keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txt

Files agents read

llms.txt
Every component and how to install it
expandable-panel.md
This page as Markdown: install, props and the full source
expandable-panel.json
The shadcn registry item

About

An accordion gallery. One photo strip is wide open and the others are slim; hover, focus or tap another and the room moves over to it, with a gentle zoom, a fading shade and a title that slides up. Every strip is a real button with aria-expanded, arrow keys walk along them, and on narrow boxes the strips stack and open downwards instead of overflowing. GSAP tweens flex-grow rather than width, so the layout never jumps, and the movement is skipped for visitors who prefer reduced motion. Ported from the first DiMaac UI, which used a plain CSS width transition on clickable divs.

Installs to
components/dimaac/expandable-panel.tsx
Uses
gsap, @gsap/react
Tags
gallery, accordion, hover, images, gsap

Mossy forest by PZF on Lummi (Lummi License). Yellow planes by rena on Lummi (Lummi License). Coastline by Cayetano Gros on Lummi (Lummi License). Organic forms by Dona Mara on Lummi (Lummi License). Desert wanderer by Beth Design Studio on Lummi (Lummi License). Pastel rooftop by Viri Gutiérrez on Lummi (Lummi License). Misty hills by SHIHO on Lummi (Lummi License).