{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"riveted-swatch-deck-photo-carousel-that-fans-portraits-round-a-brass-pin","type":"registry:block","title":"Riveted Swatch Deck Photo Carousel That Fans Portraits Round a Brass Pin","description":"Fan carousel styled as a studio swatch deck: portrait leaves pivot on a brass pin and trail each other on springs. Free React and Tailwind code, or HTML.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/riveted-swatch-deck-photo-carousel-that-fans-portraits-round-a-brass-pin.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** One leaf per backdrop paper: the colour chip, its name and roll code, and a portrait shot on it. Top of the deck first. */\nconst leaves = [\n  { name: \"Marigold\", code: \"BP 114\", chip: \"#f5a00c\", src: \"https://images.pexels.com/photos/11493115/pexels-photo-11493115.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a white top and black trousers sitting on a step ladder against an orange backdrop\", focus: \"38% 50%\" },\n  { name: \"Fern\", code: \"BP 207\", chip: \"#3d8a3c\", src: \"https://images.pexels.com/photos/12104261/pexels-photo-12104261.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Man in black with a silver chain standing against a green backdrop\", focus: \"55% 50%\" },\n  { name: \"Poppy\", code: \"BP 031\", chip: \"#e0362c\", src: \"https://images.pexels.com/photos/32174989/pexels-photo-32174989.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman holding out a pleated metallic skirt against a red backdrop\", focus: \"52% 50%\" },\n  { name: \"Lagoon\", code: \"BP 162\", chip: \"#12aebf\", src: \"https://images.pexels.com/photos/33231184/pexels-photo-33231184.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in yellow heart shaped sunglasses with her arms raised against a turquoise backdrop\", focus: \"55% 50%\" },\n  { name: \"Cobalt\", code: \"BP 188\", chip: \"#1d50b6\", src: \"https://images.pexels.com/photos/30755172/pexels-photo-30755172.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Person in pale blue crouching against a deep blue backdrop\", focus: \"45% 50%\" },\n  { name: \"Canary\", code: \"BP 102\", chip: \"#fdd31d\", src: \"https://images.pexels.com/photos/35511773/pexels-photo-35511773.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a grey check blazer leaning on a bright yellow wall\", focus: \"62% 50%\" },\n  { name: \"Apricot\", code: \"BP 046\", chip: \"#f08a55\", src: \"https://images.pexels.com/photos/6923372/pexels-photo-6923372.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a ruffled orange outfit and red sunglasses against an apricot backdrop\", focus: \"55% 50%\" },\n  { name: \"Emerald\", code: \"BP 213\", chip: \"#0d8748\", src: \"https://images.pexels.com/photos/32174358/pexels-photo-32174358.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a teal suit and pink heels standing beside green curtains\", focus: \"42% 50%\" },\n  { name: \"Pine\", code: \"BP 229\", chip: \"#1e4a47\", src: \"https://images.pexels.com/photos/4823731/pexels-photo-4823731.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a brown blazer standing in front of a dark green painted backdrop\", focus: \"50% 50%\" },\n];\n\nconst N = leaves.length;\n// Design pixels; the scene is scaled as one piece to fit narrow screens.\nconst SCENE_W = 880;\nconst SCENE_H = 566;\nconst PX = SCENE_W / 2; // the pin\nconst PY = 490;\nconst LW = 124; // leaf width\nconst LH = 470; // leaf length\nconst PIN = 440; // distance from the top of a leaf to its hole\nconst GAP = 36; // degrees the leaf above the open one is pushed aside\nconst HOLD_MS = 2300;\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst ease = (t: number) => t * t * (3 - 2 * t);\n\n/** Resting angle of a leaf that is `d` places from the open one (negative: above it, swung left). */\nfunction angleFor(d: number) {\n  if (d < 0) {\n    const m = -d;\n    if (m <= 1) return -GAP * ease(m);\n    return -(GAP + 38 * (1 - Math.exp(-(m - 1) * 0.38)));\n  }\n  return 60 * (1 - Math.exp(-d * 0.34));\n}\n\nexport default function RivetedSwatchDeck() {\n  const reduce = useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const sceneRef = useRef<HTMLDivElement>(null);\n  const deckRef = useRef<HTMLDivElement>(null);\n  const leafRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const sheenRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const [active, setActive] = useState(4);\n  const sim = useRef({\n    c: 4,\n    cv: 0,\n    target: 4,\n    a: new Array<number>(N).fill(0),\n    v: new Array<number>(N).fill(0),\n    tx: 0,\n    ty: 0,\n    px: 0,\n    py: 0,\n    dragging: false,\n    moved: 0,\n    lastX: 0,\n    lastT: 0,\n    scale: 1,\n    paused: false,\n    nextAuto: 0,\n    active: 4,\n  });\n\n  const go = useCallback((to: number) => {\n    const s = sim.current;\n    s.target = clamp(to, 0, N - 1);\n    s.nextAuto = performance.now() + HOLD_MS * 2;\n  }, []);\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const scene = sceneRef.current;\n    const deck = deckRef.current;\n    if (!wrap || !scene || !deck) return;\n    const s = sim.current;\n    let raf = 0;\n    let visible = true;\n    let last = performance.now();\n    s.nextAuto = last + 2400;\n\n    const measure = () => {\n      const sc = clamp(wrap.clientWidth / 800, 0.56, 1);\n      s.scale = sc;\n      wrap.style.height = `${Math.round(SCENE_H * sc)}px`;\n      scene.style.transform = `translateX(-50%) scale(${sc.toFixed(4)})`;\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(wrap);\n\n    const tick = (now: number) => {\n      const dt = Math.min(0.033, (now - last) / 1000);\n      last = now;\n      if (reduce) {\n        s.c = s.target;\n        s.cv = 0;\n      } else if (!s.dragging) {\n        const acc = 34 * (s.target - s.c) - 10.5 * s.cv;\n        s.cv += acc * dt;\n        s.c += s.cv * dt;\n        if (!s.paused && now > s.nextAuto) {\n          // thumb through to the bottom leaf, then let the whole deck sweep back\n          s.target = s.target >= N - 1 ? 0 : s.target + 1;\n          s.nextAuto = now + (s.target === 0 ? HOLD_MS * 1.5 : HOLD_MS);\n        }\n      }\n      // keep the fan centred over the pin whichever leaf is open\n      const bias = (0.5 - s.c / (N - 1)) * -14 + 3;\n      const sway = reduce ? 0 : Math.sin(now / 1400) * 0.7;\n      for (let i = 0; i < N; i++) {\n        const want = angleFor(i - s.c) + bias + sway * (1 + Math.abs(i - s.c) * 0.25);\n        if (reduce) {\n          s.a[i] = want;\n          s.v[i] = 0;\n        } else {\n          // leaves further from the open one are looser, so they trail behind it\n          const k = 150 - Math.min(5, Math.abs(i - s.c)) * 13;\n          const acc = k * (want - s.a[i]) - 15 * s.v[i];\n          s.v[i] += acc * dt;\n          s.a[i] += s.v[i] * dt;\n        }\n        const el = leafRefs.current[i];\n        if (!el) continue;\n        const a = s.a[i];\n        const r = (a * Math.PI) / 180;\n        const lift = clamp(Math.abs(s.v[i]) * 0.06, 0, 10);\n        el.style.transform = `translateZ(${((N - i) * 1.6 + lift).toFixed(2)}px) rotate(${a.toFixed(3)}deg)`;\n        // the light stays put while the leaf turns, so the shadow is counter-rotated\n        const sx = 5 + lift * 0.6;\n        const sy = 9 + lift;\n        const lx = sx * Math.cos(r) + sy * Math.sin(r);\n        const ly = -sx * Math.sin(r) + sy * Math.cos(r);\n        el.style.boxShadow = `0 0 0 0.5px rgba(40,34,24,0.22), ${lx.toFixed(1)}px ${ly.toFixed(1)}px ${(16 + lift * 2).toFixed(0)}px rgba(30,28,40,0.2), ${(lx * 0.2).toFixed(1)}px ${(ly * 0.2).toFixed(1)}px 2px rgba(30,28,40,0.22)`;\n        const sh = sheenRefs.current[i];\n        if (sh) sh.style.backgroundPosition = `${(50 + a * 1.7 + s.px * 30).toFixed(1)}% 0`;\n      }\n      s.px += (s.tx - s.px) * Math.min(1, dt * 6);\n      s.py += (s.ty - s.py) * Math.min(1, dt * 6);\n      deck.style.transform = `rotateX(${(15 - s.py * 5).toFixed(2)}deg) rotateY(${(s.px * 7).toFixed(2)}deg)`;\n      const a = clamp(Math.round(s.c), 0, N - 1);\n      if (a !== s.active) {\n        s.active = a;\n        setActive(a);\n      }\n      if (visible) raf = requestAnimationFrame(tick);\n    };\n\n    const io = new IntersectionObserver(([e]) => {\n      const was = visible;\n      visible = e.isIntersecting;\n      if (visible && !was) {\n        last = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    });\n    io.observe(wrap);\n    raf = requestAnimationFrame(tick);\n\n    const down = (e: PointerEvent) => {\n      if (e.button !== 0) return;\n      s.dragging = true;\n      s.moved = 0;\n      s.lastX = e.clientX;\n      s.lastT = performance.now();\n      s.cv = 0;\n      wrap.setPointerCapture(e.pointerId);\n    };\n    const move = (e: PointerEvent) => {\n      const rect = wrap.getBoundingClientRect();\n      if (e.pointerType === \"mouse\" && !reduce) {\n        s.tx = clamp((e.clientX - rect.left) / rect.width - 0.5, -0.5, 0.5) * 2;\n        s.ty = clamp((e.clientY - rect.top) / rect.height - 0.5, -0.5, 0.5) * 2;\n      }\n      if (!s.dragging) return;\n      const now = performance.now();\n      const dx = e.clientX - s.lastX;\n      const dc = dx / (120 * s.scale);\n      s.c = clamp(s.c + dc, -0.35, N - 0.65);\n      s.moved += Math.abs(dx);\n      const dt = Math.max(1, now - s.lastT) / 1000;\n      s.cv = s.cv * 0.6 + (dc / dt) * 0.4;\n      s.lastX = e.clientX;\n      s.lastT = now;\n    };\n    const up = (e: PointerEvent) => {\n      if (!s.dragging) return;\n      s.dragging = false;\n      if (s.moved < 6) {\n        const hit = (document.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null)?.closest<HTMLElement>(\"[data-leaf]\");\n        if (hit?.dataset.leaf) s.target = Number(hit.dataset.leaf);\n      } else {\n        s.target = clamp(Math.round(s.c + clamp(s.cv, -8, 8) * 0.2), 0, N - 1);\n      }\n      s.nextAuto = performance.now() + HOLD_MS * 2;\n    };\n    const leave = () => {\n      s.tx = 0;\n      s.ty = 0;\n    };\n    wrap.addEventListener(\"pointerdown\", down);\n    wrap.addEventListener(\"pointermove\", move);\n    wrap.addEventListener(\"pointerup\", up);\n    wrap.addEventListener(\"pointercancel\", up);\n    wrap.addEventListener(\"pointerleave\", leave);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      wrap.removeEventListener(\"pointerdown\", down);\n      wrap.removeEventListener(\"pointermove\", move);\n      wrap.removeEventListener(\"pointerup\", up);\n      wrap.removeEventListener(\"pointercancel\", up);\n      wrap.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [reduce]);\n\n  const onKey = (e: React.KeyboardEvent) => {\n    const t = sim.current.target;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") {\n      e.preventDefault();\n      go(t + 1);\n    } else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") {\n      e.preventDefault();\n      go(t - 1);\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      go(0);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      go(N - 1);\n    }\n  };\n  const pause = (v: boolean) => () => {\n    sim.current.paused = v;\n    if (!v) sim.current.nextAuto = performance.now() + HOLD_MS;\n  };\n\n  const btn = \"grid h-10 w-10 place-items-center rounded-full text-slate-700 transition hover:bg-slate-900/10 disabled:opacity-30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-900\";\n  const cur = leaves[active];\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#dfe3ec] pb-8 pt-6\" onMouseEnter={pause(true)} onMouseLeave={pause(false)} onFocus={pause(true)} onBlur={pause(false)}>\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(70% 80% at 50% 45%, #f1f3f8 0%, #dfe3ec 55%, #c9cedb 100%)\" }} />\n      <div\n        ref={wrapRef}\n        role=\"group\"\n        aria-roledescription=\"carousel\"\n        aria-label=\"Swatch deck of portraits. Use the left and right arrow keys to thumb through the leaves.\"\n        tabIndex={0}\n        onKeyDown={onKey}\n        className=\"relative mx-auto w-full cursor-grab touch-pan-y select-none outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-slate-900/70 active:cursor-grabbing\"\n        style={{ height: SCENE_H }}\n      >\n        <div ref={sceneRef} className=\"absolute left-1/2 top-0\" style={{ width: SCENE_W, height: SCENE_H, transformOrigin: \"50% 0\", transform: \"translateX(-50%)\", perspective: \"1500px\", perspectiveOrigin: \"50% 40%\" }}>\n          <div ref={deckRef} className=\"absolute inset-0\" style={{ transformStyle: \"preserve-3d\", transformOrigin: `${PX}px ${PY}px`, transform: \"rotateX(15deg)\" }}>\n            {/* soft pool of shadow on the table under the pin */}\n            <div aria-hidden=\"true\" className=\"absolute rounded-full\" style={{ left: PX - 300, top: PY - 250, width: 600, height: 340, background: \"radial-gradient(closest-side, rgba(40,44,70,0.22), rgba(40,44,70,0))\", transform: \"translateZ(-2px)\" }} />\n            {leaves.map((l, i) => (\n              <div\n                key={l.name}\n                ref={(el) => {\n                  leafRefs.current[i] = el;\n                }}\n                data-leaf={i}\n                className=\"absolute rounded-[13px] bg-[#fbf9f4]\"\n                style={{ left: PX - LW / 2, top: PY - PIN, width: LW, height: LH, transformOrigin: `${LW / 2}px ${PIN}px`, zIndex: N - i, willChange: \"transform\" }}\n              >\n                <div className=\"absolute left-[6px] right-[6px] top-[6px] h-[292px] overflow-hidden rounded-[8px] bg-slate-300\">\n                  <img src={l.src} alt={l.alt} loading=\"lazy\" draggable={false} className=\"h-full w-full object-cover\" style={{ objectPosition: l.focus }} />\n                  {/* a band of gloss that slides across the laminate as the leaf turns */}\n                  <div\n                    ref={(el) => {\n                      sheenRefs.current[i] = el;\n                    }}\n                    aria-hidden=\"true\"\n                    className=\"absolute inset-0\"\n                    style={{ backgroundImage: \"linear-gradient(105deg, rgba(255,255,255,0) 35%, rgba(255,255,255,0.28) 50%, rgba(255,255,255,0) 65%)\", backgroundSize: \"300% 100%\", backgroundPosition: \"50% 0\" }}\n                  />\n                  <div aria-hidden=\"true\" className=\"absolute inset-0 rounded-[8px]\" style={{ boxShadow: \"inset 0 0 0 1px rgba(0,0,0,0.08)\" }} />\n                </div>\n                <div className=\"absolute left-[6px] right-[6px] top-[304px] h-[38px] rounded-[4px]\" style={{ background: l.chip, boxShadow: \"inset 0 0 0 1px rgba(0,0,0,0.08)\" }} />\n                <div className=\"absolute left-[9px] right-[9px] top-[350px]\">\n                  <div className=\"text-[13px] font-semibold leading-tight tracking-tight text-slate-900\">{l.name}</div>\n                  <div className=\"mt-0.5 font-mono text-[8.5px] uppercase tracking-[0.14em] text-slate-600\">{l.code}</div>\n                </div>\n                <div className=\"absolute left-[9px] top-[392px] font-mono text-[7px] uppercase leading-[1.5] tracking-[0.14em] text-slate-500\">\n                  2.72 m roll\n                  <br />\n                  Matte\n                </div>\n                {/* edge highlight so each leaf reads as a slip of card with thickness */}\n                <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 rounded-[13px]\" style={{ boxShadow: \"inset 0 1px 0 rgba(255,255,255,0.9), inset 0 -1.5px 0 rgba(120,110,95,0.35)\" }} />\n              </div>\n            ))}\n            {/* the brass pin that holds the deck */}\n            <div\n              aria-hidden=\"true\"\n              className=\"absolute rounded-full\"\n              style={{\n                left: PX - 13,\n                top: PY - 13,\n                width: 26,\n                height: 26,\n                zIndex: N + 2,\n                transform: \"translateZ(22px)\",\n                background: \"radial-gradient(circle at 34% 28%, #fff6cf 0%, #f0cf74 22%, #c8962f 52%, #7c5512 86%, #5b3d0b 100%)\",\n                boxShadow: \"0 3px 5px rgba(30,24,10,0.45), 0 0 0 1px rgba(80,54,10,0.5), inset 0 -2px 3px rgba(60,40,6,0.55)\",\n              }}\n            >\n              <div className=\"absolute left-1/2 top-1/2 h-[11px] w-[11px] -translate-x-1/2 -translate-y-1/2 rounded-full\" style={{ background: \"radial-gradient(circle at 60% 66%, #f7dc8c, #a8791f 70%)\", boxShadow: \"inset 0 1px 2px rgba(60,40,6,0.7)\" }} />\n            </div>\n          </div>\n        </div>\n      </div>\n\n      <div className=\"relative mx-auto flex max-w-sm items-center justify-between px-4\">\n        <button type=\"button\" onClick={() => go(sim.current.target - 1)} disabled={active === 0} aria-label=\"Previous leaf\" className={btn}>\n          <svg aria-hidden=\"true\" width=\"18\" height=\"18\" viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n            <path d=\"M12 4l-6 6 6 6\" />\n          </svg>\n        </button>\n        <p aria-live=\"polite\" className=\"flex items-center gap-2.5 text-[13px] font-medium text-slate-800\">\n          <span aria-hidden=\"true\" className=\"h-3 w-3 rounded-full\" style={{ background: cur.chip, boxShadow: \"inset 0 0 0 1px rgba(0,0,0,0.12)\" }} />\n          {cur.name}\n          <span className=\"font-mono text-[11px] uppercase tracking-[0.14em] text-slate-600\">{cur.code}</span>\n        </p>\n        <button type=\"button\" onClick={() => go(sim.current.target + 1)} disabled={active === N - 1} aria-label=\"Next leaf\" className={btn}>\n          <svg aria-hidden=\"true\" width=\"18\" height=\"18\" viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n            <path d=\"M8 4l6 6-6 6\" />\n          </svg>\n        </button>\n      </div>\n    </section>\n  );\n}\n"}]}
