{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"concertina-fold-photo-book-carousel-that-opens-flat-at-the-centre","type":"registry:block","title":"Concertina Fold Photo Book Carousel That Opens Flat at the Centre","description":"Carousel shaped like a concertina photo book: zigzag panels stand up and the one pulled to the middle opens flat. Free React and Tailwind code, or HTML.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/concertina-fold-photo-book-carousel-that-opens-flat-at-the-centre.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** The prints, in the order they are bound into the book. */\nconst prints = [\n  { title: \"Cobalt on ochre\", src: \"https://images.pexels.com/photos/14792999/pexels-photo-14792999.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a blue blazer lit with blue light against a deep yellow backdrop\", focus: \"50% 30%\" },\n  { title: \"White shirt, red room\", src: \"https://images.pexels.com/photos/2734952/pexels-photo-2734952.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Person with cropped orange hair and hoop earrings in a white shirt against a red backdrop\", focus: \"55% 25%\" },\n  { title: \"Tangerine halter\", src: \"https://images.pexels.com/photos/39325164/pexels-photo-39325164.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in an orange halter top and lime skirt seated against a vivid orange backdrop\", focus: \"50% 30%\" },\n  { title: \"Green sleeve\", src: \"https://images.pexels.com/photos/21352926/pexels-photo-21352926.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a green top with a dark jacket slipping off her shoulders against a crimson backdrop\", focus: \"50% 30%\" },\n  { title: \"Lace gloves\", src: \"https://images.pexels.com/photos/18763472/pexels-photo-18763472.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Model in an orange jacket covering her eyes with sheer dotted gloves\", focus: \"50% 35%\" },\n  { title: \"Check coat\", src: \"https://images.pexels.com/photos/36440745/pexels-photo-36440745.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Man in a grey check coat resting a hand on his temple against a teal backdrop\", focus: \"50% 25%\" },\n  { title: \"Velvet on magenta\", src: \"https://images.pexels.com/photos/14938882/pexels-photo-14938882.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a black velvet dress reclining with one arm raised against a magenta backdrop\", focus: \"55% 30%\" },\n  { title: \"Pink crop, red wash\", src: \"https://images.pexels.com/photos/12089098/pexels-photo-12089098.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman in a pink crop top and dark trousers lit red against a coral backdrop\", focus: \"50% 30%\" },\n  { title: \"Blue hour curls\", src: \"https://images.pexels.com/photos/15817799/pexels-photo-15817799.jpeg?auto=compress&cs=tinysrgb&w=700\", alt: \"Woman with curly hair in a loose sweater under deep blue studio light\", focus: \"50% 25%\" },\n];\n\nconst N = prints.length;\nconst SLOTS = N * 2; // panels kept in the page; must be even so the zigzag never breaks\nconst FOLD = 64; // degrees each panel turns away once it is clear of the centre\nconst WRAP = 9; // degrees of gentle curve that carries both ends back into the distance\nconst HOLD_MS = 2600; // pause on each print while it plays by itself\n\nconst mod = (a: number, n: number) => ((a % n) + n) % n;\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst smooth = (a: number, b: number, v: number) => {\n  const t = clamp((v - a) / (b - a), 0, 1);\n  return t * t * (3 - 2 * t);\n};\n\nexport default function ConcertinaFoldPhotoBook() {\n  const reduce = useReducedMotion();\n  const stageRef = useRef<HTMLDivElement>(null);\n  const panelRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const faceRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const shadeRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const floorRef = useRef<HTMLDivElement>(null);\n  const [active, setActive] = useState(0);\n  const sim = useRef({\n    pos: -1.4,\n    vel: 0,\n    target: 0,\n    open: 0,\n    openVel: 0,\n    w: 280,\n    dragging: false,\n    moved: 0,\n    lastX: 0,\n    lastT: 0,\n    paused: false,\n    nextAuto: 0,\n    blur: new Array<number>(SLOTS).fill(-1),\n    active: 0,\n  });\n\n  const step = useCallback((dir: number) => {\n    const s = sim.current;\n    s.target = Math.round(s.target) + dir;\n    s.nextAuto = performance.now() + HOLD_MS * 2;\n  }, []);\n\n  useEffect(() => {\n    const stage = stageRef.current;\n    if (!stage) return;\n    const s = sim.current;\n    let raf = 0;\n    let visible = true;\n    let last = performance.now();\n    s.nextAuto = last + 1900;\n    if (reduce) {\n      s.pos = 0;\n      s.open = 1;\n    }\n\n    const measure = () => {\n      const sw = stage.clientWidth;\n      s.w = sw < 640 ? Math.round(sw * 0.54) : Math.min(320, Math.round(sw * 0.27));\n      stage.style.setProperty(\"--w\", `${s.w}px`);\n    };\n    measure();\n    const ro = new ResizeObserver(measure);\n    ro.observe(stage);\n\n    const layout = (now: number) => {\n      const w = s.w;\n      const idle = reduce ? 0 : Math.sin(now / 1100) * 1.6;\n      const stretch = clamp(Math.abs(s.vel) * 0.07, 0, 0.3);\n      const fold = (88 + (FOLD - 88) * s.open) * (1 - stretch) + idle;\n      const base = Math.round(s.pos);\n      const k0 = base - SLOTS / 2;\n      const hx: number[] = [0];\n      const hz: number[] = [0];\n      const th: number[] = [];\n      for (let j = 0; j < SLOTS; j++) {\n        const k = k0 + j;\n        const d = k - s.pos;\n        const f = smooth(0.28, 1.2, Math.abs(d));\n        // The zigzag, plus a slight wrap so both ends curve away from the viewer.\n        const a = (mod(k, 2) === 0 ? 1 : -1) * fold * f + WRAP * Math.tanh(d * 0.9);\n        th.push(a);\n        const r = (a * Math.PI) / 180;\n        hx.push(hx[j] + w * Math.cos(r));\n        hz.push(hz[j] - w * Math.sin(r));\n      }\n      // The point of the strip that should sit dead centre.\n      const cj = base - k0;\n      const fr = s.pos + 0.5 - base;\n      const cr = (th[cj] * Math.PI) / 180;\n      const cx = hx[cj] + fr * w * Math.cos(cr);\n      // Sit the book back a little so the ridge that folds toward you never outgrows the open panel.\n      const cz = hz[cj] - fr * w * Math.sin(cr) + w * 0.4 * Math.sin((fold * Math.PI) / 180);\n      for (let j = 0; j < SLOTS; j++) {\n        const k = k0 + j;\n        const slot = mod(k, SLOTS);\n        const el = panelRefs.current[slot];\n        if (!el) continue;\n        const d = Math.abs(k - s.pos);\n        el.style.transform = `translate3d(${(hx[j] - cx).toFixed(2)}px,0,${(hz[j] - cz).toFixed(2)}px) rotateY(${th[j].toFixed(2)}deg)`;\n        el.style.opacity = clamp(SLOTS / 2 - 0.4 - d, 0, 1).toFixed(2);\n        el.dataset.k = String(k);\n        const shade = shadeRefs.current[slot];\n        if (shade) shade.style.opacity = Math.abs(Math.sin((th[j] * Math.PI) / 180)).toFixed(3);\n        const depth = Math.abs((hz[j] + hz[j + 1]) / 2 - cz + w * 0.4);\n        const b = Math.round(clamp((depth - w * 0.55) / (w * 0.4) + (d - 3) * 0.6, 0, 3.5) * 2) / 2;\n        if (b !== s.blur[slot]) {\n          s.blur[slot] = b;\n          const face = faceRefs.current[slot];\n          if (face) face.style.filter = b ? `blur(${b}px)` : \"none\";\n        }\n      }\n      if (floorRef.current) {\n        const span = hx[SLOTS] - hx[0];\n        floorRef.current.style.width = `${Math.min(span, stage.clientWidth * 1.1).toFixed(0)}px`;\n      }\n      const a = mod(Math.round(s.pos), N);\n      if (a !== s.active) {\n        s.active = a;\n        setActive(a);\n      }\n    };\n\n    const tick = (now: number) => {\n      const dt = Math.min(0.033, (now - last) / 1000);\n      last = now;\n      if (reduce) {\n        s.pos = s.target;\n        s.vel = 0;\n        s.open = 1;\n      } else {\n        if (!s.dragging) {\n          const acc = 62 * (s.target - s.pos) - 11.5 * s.vel;\n          s.vel += acc * dt;\n          s.pos += s.vel * dt;\n          if (!s.paused && now > s.nextAuto) {\n            s.target = Math.round(s.target) + 1;\n            s.nextAuto = now + HOLD_MS;\n          }\n        }\n        const oa = 38 * (1 - s.open) - 9 * s.openVel;\n        s.openVel += oa * dt;\n        s.open += s.openVel * dt;\n      }\n      layout(now);\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(stage);\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.vel = 0;\n      stage.setPointerCapture(e.pointerId);\n    };\n    const move = (e: PointerEvent) => {\n      if (!s.dragging) return;\n      const now = performance.now();\n      const dx = e.clientX - s.lastX;\n      const dp = -dx / (s.w * 0.78);\n      s.pos += dp;\n      s.moved += Math.abs(dx);\n      const dt = Math.max(1, now - s.lastT) / 1000;\n      s.vel = s.vel * 0.6 + (dp / 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        // A plain tap: bring the tapped panel to the centre.\n        const hit = (document.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null)?.closest<HTMLElement>(\"[data-k]\");\n        if (hit?.dataset.k) s.target = Number(hit.dataset.k);\n      } else {\n        s.target = Math.round(s.pos + clamp(s.vel, -9, 9) * 0.22);\n      }\n      s.nextAuto = performance.now() + HOLD_MS * 2;\n    };\n    stage.addEventListener(\"pointerdown\", down);\n    stage.addEventListener(\"pointermove\", move);\n    stage.addEventListener(\"pointerup\", up);\n    stage.addEventListener(\"pointercancel\", up);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      stage.removeEventListener(\"pointerdown\", down);\n      stage.removeEventListener(\"pointermove\", move);\n      stage.removeEventListener(\"pointerup\", up);\n      stage.removeEventListener(\"pointercancel\", up);\n    };\n  }, [reduce]);\n\n  const onKey = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      step(1);\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      step(-1);\n    }\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  return (\n    <section className=\"relative overflow-hidden bg-[#e7e5df] py-10 sm:py-14\" 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(120% 70% at 50% 38%, #f6f5f1 0%, #e9e7e1 48%, #d3d0c8 100%)\" }} />\n      <div\n        ref={stageRef}\n        role=\"group\"\n        aria-roledescription=\"carousel\"\n        aria-label=\"Concertina photo book. Use the left and right arrow keys to pull it through.\"\n        tabIndex={0}\n        onKeyDown={onKey}\n        className=\"relative mx-auto w-full max-w-6xl cursor-grab touch-pan-y select-none outline-none focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-4 focus-visible:ring-offset-[#e7e5df] active:cursor-grabbing\"\n        style={{ height: \"calc(var(--w, 280px) * 1.44 + 72px)\", perspective: \"1900px\", perspectiveOrigin: \"50% 4%\" }}\n      >\n        <div className=\"absolute left-1/2 top-6\" style={{ transformStyle: \"preserve-3d\", width: 0, height: \"calc(var(--w, 280px) * 1.44)\" }}>\n          {/* One soft pool of shadow under the whole book */}\n          <div\n            ref={floorRef}\n            aria-hidden=\"true\"\n            className=\"absolute bottom-0 left-0 h-40\"\n            style={{\n              transform: \"translateX(-50%) translateY(50%) rotateX(90deg)\",\n              background: \"radial-gradient(closest-side, rgba(40,32,20,0.2), rgba(40,32,20,0.08) 55%, rgba(40,32,20,0) 100%)\",\n              filter: \"blur(10px)\",\n            }}\n          />\n          {Array.from({ length: SLOTS }, (_, slot) => {\n            const p = prints[slot % N];\n            const away = slot % 2 === 0;\n            return (\n              <div\n                key={slot}\n                ref={(el) => {\n                  panelRefs.current[slot] = el;\n                }}\n                className=\"absolute left-0 top-0 h-full\"\n                style={{ width: \"var(--w, 280px)\", transformOrigin: \"0 50%\", transformStyle: \"preserve-3d\", willChange: \"transform\" }}\n              >\n                <div\n                  ref={(el) => {\n                    faceRefs.current[slot] = el;\n                  }}\n                  className=\"absolute inset-0 overflow-hidden rounded-[3px] bg-[#f4f1ea]\"\n                  style={{ boxShadow: \"inset 0 0 0 1px rgba(60,48,30,0.10)\", backfaceVisibility: \"hidden\" }}\n                >\n                  <div className=\"absolute inset-[5%] bottom-[13%] overflow-hidden bg-stone-300\">\n                    <img src={p.src} alt={slot < N ? p.alt : \"\"} loading=\"lazy\" draggable={false} className=\"h-full w-full object-cover\" style={{ objectPosition: p.focus }} />\n                  </div>\n                  <div className=\"absolute inset-x-[6%] bottom-[4.2%] flex items-baseline justify-between font-mono text-[9px] uppercase tracking-[0.14em] text-stone-600 sm:text-[10px]\">\n                    <span>{p.title}</span>\n                    <span>{String((slot % N) + 1).padStart(2, \"0\")}</span>\n                  </div>\n                  {/* Light falling across the fold: bright on the ridge, dark in the valley */}\n                  <div\n                    ref={(el) => {\n                      shadeRefs.current[slot] = el;\n                    }}\n                    aria-hidden=\"true\"\n                    className=\"absolute inset-0\"\n                    style={{\n                      background: away\n                        ? \"linear-gradient(90deg, rgba(255,255,255,0.10) 0%, rgba(28,20,10,0.30) 30%, rgba(28,20,10,0.72) 100%)\"\n                        : \"linear-gradient(90deg, rgba(28,20,10,0.50) 0%, rgba(28,20,10,0.08) 34%, rgba(255,255,255,0.14) 100%)\",\n                      opacity: 0,\n                    }}\n                  />\n                </div>\n                {/* Contact shadow thrown forward onto the table */}\n                <div\n                  aria-hidden=\"true\"\n                  className=\"absolute inset-x-0 top-full h-[20%]\"\n                  style={{ transformOrigin: \"50% 0\", transform: \"rotateX(90deg)\", background: \"linear-gradient(180deg, rgba(40,32,20,0.42), rgba(40,32,20,0) 85%)\", filter: \"blur(4px)\" }}\n                />\n              </div>\n            );\n          })}\n        </div>\n      </div>\n\n      <div className=\"relative mx-auto mt-2 flex max-w-md items-center justify-between px-4\">\n        <button\n          type=\"button\"\n          onClick={() => step(-1)}\n          aria-label=\"Previous print\"\n          className=\"grid h-10 w-10 place-items-center rounded-full text-stone-700 transition hover:bg-stone-900/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900\"\n        >\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=\"text-center font-mono text-[11px] uppercase tracking-[0.18em] text-stone-700\">\n          <span className=\"tabular-nums\">{String(active + 1).padStart(2, \"0\")}</span>\n          <span className=\"mx-2 text-stone-500\">/</span>\n          {prints[active].title}\n        </p>\n        <button\n          type=\"button\"\n          onClick={() => step(1)}\n          aria-label=\"Next print\"\n          className=\"grid h-10 w-10 place-items-center rounded-full text-stone-700 transition hover:bg-stone-900/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-900\"\n        >\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"}]}
