{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"expandable-panel","type":"registry:component","title":"Expandable Panel","description":"Photo strips that open wide on hover, focus or tap.","author":"DiMaac (HankTheTank) <https://dimaac.com>","dependencies":["gsap","@gsap/react"],"files":[{"path":"registry/dimaac/expandable-panel/expandable-panel.tsx","type":"registry:component","target":"components/dimaac/expandable-panel.tsx","content":"\"use client\";\n\nimport { useRef, useState, type KeyboardEvent } from \"react\";\nimport { gsap } from \"gsap\";\nimport { useGSAP } from \"@gsap/react\";\n\n// An accordion of photos. One strip is wide open, the rest are slim; hover, focus or tap another\n// and the room moves over to it. Each strip is a real button, so the keyboard works too.\n// GSAP tweens flex-grow (not width), so the browser shares the space for us and every strip\n// stays exactly as tall as the box. On narrow boxes the strips stack and open downwards.\n\nexport type ExpandablePanelItem = {\n  /** The photo's address. */\n  image: string;\n  /** Describes the photo for screen readers. */\n  alt: string;\n  /** The label shown on the open strip, and the accessible name of the button. */\n  title: string;\n  /** A small line under the title, optional. */\n  caption?: string;\n};\n\nexport type ExpandablePanelProps = {\n  /** The strips, in order. Four to eight look best. */\n  panels: ExpandablePanelItem[];\n  /** What opens a strip: \"hover\" (mouse), or \"click\". Tap and keyboard focus always open one. */\n  trigger?: \"hover\" | \"click\";\n  /** Which strip starts open. -1 starts with all of them closed. */\n  defaultActive?: number;\n  /** Go back to the first choice when the pointer leaves the whole row. */\n  resetOnLeave?: boolean;\n  /** How much wider the open strip is than a closed one (1 is the same width). */\n  expandedRatio?: number;\n  /** Space between the strips, in pixels. */\n  gap?: number;\n  /** Corner radius of each strip, in pixels. */\n  radius?: number;\n  /** How long the strips take to change size, in seconds. */\n  duration?: number;\n  /** The GSAP ease of the change, e.g. \"power3.inOut\" or \"expo.out\". */\n  ease?: string;\n  /** \"auto\" stacks the strips when the box is narrower than 672px. */\n  orientation?: \"auto\" | \"horizontal\" | \"vertical\";\n  /** Called with the new strip's index whenever the open one changes. */\n  onChange?: (index: number) => void;\n  /** Extra classes for the outer box. Give it a size (it fills it). */\n  className?: string;\n  /** Extra classes for each strip. */\n  panelClassName?: string;\n};\n\nexport function ExpandablePanel({\n  panels,\n  trigger = \"hover\",\n  defaultActive = 0,\n  resetOnLeave = false,\n  expandedRatio = 6,\n  gap = 12,\n  radius = 24,\n  duration = 0.9,\n  ease = \"expo.out\",\n  orientation = \"auto\",\n  onChange,\n  className = \"\",\n  panelClassName = \"\",\n}: ExpandablePanelProps) {\n  const listRef = useRef<HTMLDivElement>(null);\n  const buttonRefs = useRef<(HTMLButtonElement | null)[]>([]);\n  const [active, setActive] = useState(defaultActive);\n  // The strips are drawn at their starting size by React; after that only GSAP changes them\n  const startActive = useRef(defaultActive);\n  const started = useRef(false);\n\n  // When the defaultActive prop changes on a live strip (the playground does this), follow it\n  const [seenDefault, setSeenDefault] = useState(defaultActive);\n  if (seenDefault !== defaultActive) {\n    setSeenDefault(defaultActive);\n    setActive(defaultActive);\n  }\n\n  const open = (index: number) => {\n    if (index === active) return;\n    setActive(index);\n    onChange?.(index);\n  };\n\n  useGSAP(\n    () => {\n      const buttons = buttonRefs.current.slice(0, panels.length).filter((button): button is HTMLButtonElement => button !== null);\n      if (buttons.length === 0) return;\n\n      const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n      // The first run only places things, so the page doesn't open with an animation nobody asked for\n      const time = started.current && !reduced ? duration : 0;\n      started.current = true;\n\n      const isOpen = (i: number) => i === active;\n      gsap.to(buttons, { flexGrow: (i: number) => (isOpen(i) ? expandedRatio : 1), duration: time, ease, overwrite: \"auto\" });\n      // The photo eases from a slight zoom to rest, and closed strips are dimmed\n      gsap.to(buttons.map((b) => b.querySelector(\"[data-photo]\")), { scale: (i: number) => (isOpen(i) ? 1 : 1.12), duration: time * 1.1, ease, overwrite: \"auto\" });\n      gsap.to(buttons.map((b) => b.querySelector(\"[data-shade]\")), { opacity: (i: number) => (isOpen(i) ? 0 : 0.45), duration: time, ease: \"power2.out\", overwrite: \"auto\" });\n      // The title slides up into view only on the open strip; on the others it fades out quickly\n      const titles = buttons.map((b) => b.querySelector(\"[data-title]\"));\n      gsap.to(titles, {\n        opacity: (i: number) => (isOpen(i) ? 1 : 0),\n        y: (i: number) => (isOpen(i) || reduced ? 0 : 18),\n        duration: time * 0.6,\n        delay: (i: number) => (isOpen(i) && time > 0 ? 0.12 : 0),\n        ease: \"power3.out\",\n        overwrite: \"auto\",\n      });\n    },\n    { scope: listRef, dependencies: [active, panels.length, expandedRatio, duration, ease] },\n  );\n\n  // Arrow keys walk along the strips, and landing on one opens it (focus opens)\n  const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n    const keys = [\"ArrowRight\", \"ArrowDown\", \"ArrowLeft\", \"ArrowUp\", \"Home\", \"End\"];\n    if (!keys.includes(event.key)) return;\n    const current = buttonRefs.current.findIndex((button) => button === document.activeElement);\n    if (current === -1) return;\n    event.preventDefault();\n    const last = panels.length - 1;\n    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);\n    buttonRefs.current[next]?.focus();\n  };\n\n  const direction = orientation === \"vertical\" ? \"flex-col\" : orientation === \"horizontal\" ? \"flex-row\" : \"flex-row @max-2xl:flex-col\";\n\n  return (\n    <div className={`@container relative h-full w-full ${className}`}>\n      <div\n        ref={listRef}\n        role=\"group\"\n        aria-label=\"Expandable panels\"\n        onKeyDown={onKeyDown}\n        onPointerLeave={() => resetOnLeave && open(defaultActive)}\n        className={`flex h-full w-full ${direction}`}\n        style={{ gap }}\n      >\n        {panels.map((panel, index) => (\n          <button\n            key={`${index}-${panel.image}`}\n            ref={(button) => {\n              buttonRefs.current[index] = button;\n            }}\n            type=\"button\"\n            aria-expanded={index === active}\n            aria-label={panel.title}\n            onPointerEnter={(event) => {\n              // Touch fires an enter just before the tap, so only a real pointer counts as a hover\n              if (trigger === \"hover\" && event.pointerType !== \"touch\") open(index);\n            }}\n            onClick={() => open(index)}\n            onFocus={() => open(index)}\n            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}`}\n            style={{ flexGrow: index === startActive.current ? expandedRatio : 1, borderRadius: radius, WebkitTapHighlightColor: \"transparent\" }}\n          >\n            <img\n              data-photo\n              src={panel.image}\n              alt={panel.alt}\n              draggable={false}\n              loading=\"lazy\"\n              decoding=\"async\"\n              className=\"absolute inset-0 -z-10 block h-full w-full object-cover\"\n              style={{ transform: index === startActive.current ? \"scale(1)\" : \"scale(1.12)\" }}\n            />\n            {/* Darkens closed strips, and the bottom of every strip so the white text reads */}\n            <span aria-hidden className=\"pointer-events-none absolute inset-0 bg-black\" data-shade style={{ opacity: index === startActive.current ? 0 : 0.45 }} />\n            <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\" />\n            <span aria-hidden className=\"absolute top-4 left-5 font-mono text-[11px] tracking-[0.14em] opacity-80 @max-2xl:top-3\">\n              {String(index + 1).padStart(2, \"0\")}\n            </span>\n            <span data-title className=\"pointer-events-none absolute right-5 bottom-5 left-5 block whitespace-nowrap\" style={{ opacity: index === startActive.current ? 1 : 0 }}>\n              <span className=\"block text-[clamp(22px,2.6vw,40px)] leading-[1.05] font-medium tracking-[-0.03em]\">{panel.title}</span>\n              {panel.caption ? <span className=\"mt-1.5 block text-[13px] opacity-75\">{panel.caption}</span> : null}\n            </span>\n          </button>\n        ))}\n      </div>\n    </div>\n  );\n}\n"}],"categories":["layout","gallery","accordion","hover","images","gsap"],"docs":"import { ExpandablePanel } from \"@/components/dimaac/expandable-panel\". Give its parent a size: it fills it. Docs, playground and props: https://ui.dimaac.com/components/expandable-panel","meta":{"tier":"free","preview":"https://ui.dimaac.com/components/expandable-panel","credits":[{"by":"PZF","url":"https://www.lummi.ai/photo/mossy-forest-scene-o4b8t","what":"Mossy forest","source":"Lummi","licence":"Lummi License"},{"by":"rena","url":"https://www.lummi.ai/photo/modern-architectural-design-aozka","what":"Yellow planes","source":"Lummi","licence":"Lummi License"},{"by":"Cayetano Gros","url":"https://www.lummi.ai/photo/dramatic-coastline-aerial-view-jml-7","what":"Coastline","source":"Lummi","licence":"Lummi License"},{"by":"Dona Mara","url":"https://www.lummi.ai/photo/abstract-sculptural-objects-flp-3","what":"Organic forms","source":"Lummi","licence":"Lummi License"},{"by":"Beth Design Studio","url":"https://www.lummi.ai/photo/desert-wanderer-9puhu","what":"Desert wanderer","source":"Lummi","licence":"Lummi License"},{"by":"Viri Gutiérrez","url":"https://www.lummi.ai/photo/contemplative-silhouette-against-modern-minimalist-architecture-1-haa","what":"Pastel rooftop","source":"Lummi","licence":"Lummi License"},{"by":"SHIHO","url":"https://www.lummi.ai/photo/lush-green-hills-mdvvk","what":"Misty hills","source":"Lummi","licence":"Lummi License"}]}}