{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"matchbook-faq-row-where-each-printed-cover-flips-up-off-its-comb-of-matches-to-show-the-answer-inside","type":"registry:block","title":"Matchbook FAQ Row Where Each Printed Cover Flips up off Its Comb of Matches to Show the Answer Inside","description":"FAQ drawn as a row of bar matchbooks: press one and its printed cover swings up in 3D to show the answer inside. Free React and Tailwind code, or HTML.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/matchbook-faq-row-where-each-printed-cover-flips-up-off-its-comb-of-matches-to-show-the-answer-inside.tsx","type":"registry:component","content":"import { useEffect, useId, useRef, useState } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"framer-motion\";\n\ntype Motif = \"sun\" | \"arcs\" | \"check\" | \"bars\" | \"moon\" | \"slant\";\n/** The bar, and one matchbook per question: cover colour, ink, the colour of the match heads and the printed motif. */\nconst bar = { name: \"The Lantern Room\", line: \"Close cover before striking\" };\nconst books: { q: string; a: string; cover: string; ink: string; head: string; motif: Motif }[] = [\n  { q: \"Do we need to book?\", a: \"Not at the bar or the six stools by the window. For a table after eight on a Friday or Saturday, book online or ring us after four.\", cover: \"#d8432b\", ink: \"#fff1d8\", head: \"#f4ead2\", motif: \"sun\" },\n  { q: \"How late are you open?\", a: \"Until one, Tuesday to Saturday. Last orders are called at half past twelve and the kitchen hatch closes at eleven.\", cover: \"#e5a91f\", ink: \"#1b1510\", head: \"#c8321f\", motif: \"arcs\" },\n  { q: \"Anything without alcohol?\", a: \"A whole page of it. Five drinks built from our own cordials and teas, made with the same care and served in the same glasses.\", cover: \"#14573f\", ink: \"#f3e7c6\", head: \"#e8b63c\", motif: \"check\" },\n  { q: \"Can we bring a big group?\", a: \"Up to twelve fit in the back snug. Write to us a week ahead and we will hold it for you and set out the long table.\", cover: \"#efe4cb\", ink: \"#1c2957\", head: \"#1c2957\", motif: \"bars\" },\n  { q: \"Is there a dress code?\", a: \"No. Come as you are, from work or from the allotment. We only ask that hats come off at the bar so Ada can see who is ordering.\", cover: \"#2548ad\", ink: \"#f6ecd4\", head: \"#f4ead2\", motif: \"moon\" },\n  { q: \"Do you serve food?\", a: \"Small plates until eleven: toasties, pickles, a cheese board and whatever the bakery next door had left at closing.\", cover: \"#ee9bb0\", ink: \"#57101f\", head: \"#201418\", motif: \"slant\" },\n];\n\nconst TOUR = [2, 0, 5, 3, 1, 4]; // the order the row opens its own books in while nobody is touching it\nconst REST = 171; // degrees an open cover lies back at: card is springy and never goes quite flat\nconst STICKS = 10;\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\nconst svgUrl = (body: string, w: number, h: number) => `url(\"data:image/svg+xml;utf8,${encodeURIComponent(`<svg xmlns='http://www.w3.org/2000/svg' width='${w}' height='${h}'>${body}</svg>`)}\")`;\nconst CARD = svgUrl(`<filter id='p'><feTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.1  0 0 0 0 0.08  0 0 0 0 0.05  0 0 0 0.75 -0.24'/></filter><rect width='200' height='200' filter='url(#p)'/>`, 200, 200);\nconst GRIT = svgUrl(`<filter id='s'><feTurbulence type='fractalNoise' baseFrequency='1.4' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.85  0 0 0 0 0.7  0 0 0 0 0.55  0 0 0 2.4 -1.25'/></filter><rect width='120' height='120' filter='url(#s)'/>`, 120, 120);\n// how each book was dropped on the bar\nconst LAY = [\n  { r: -1.1, y: 4 },\n  { r: 0.8, y: -3 },\n  { r: -0.5, y: 6 },\n  { r: 1.2, y: 0 },\n  { r: -0.9, y: -4 },\n  { r: 0.6, y: 5 },\n];\n\n/** What is printed across the lower part of the cover, drawn with gradients in the cover's ink. */\nfunction motif(kind: Motif, ink: string): React.CSSProperties {\n  switch (kind) {\n    case \"sun\":\n      return { backgroundImage: `radial-gradient(circle at 50% 100%, ${ink} 0 30%, rgba(0,0,0,0) 30.8%), repeating-conic-gradient(from -90deg at 50% 100%, ${ink} 0 6deg, rgba(0,0,0,0) 6deg 15deg)` };\n    case \"arcs\":\n      return { backgroundImage: `repeating-radial-gradient(circle at 50% 118%, ${ink} 0 5px, rgba(0,0,0,0) 5px 13px)` };\n    case \"check\":\n      return { backgroundImage: `conic-gradient(${ink} 25%, rgba(0,0,0,0) 0 50%, ${ink} 0 75%, rgba(0,0,0,0) 0)`, backgroundSize: \"24px 24px\" };\n    case \"bars\":\n      return { backgroundImage: `repeating-linear-gradient(90deg, ${ink} 0 9px, rgba(0,0,0,0) 9px 18px)` };\n    case \"moon\":\n      return { backgroundImage: `radial-gradient(circle at 68% 62%, ${ink} 0 27px, rgba(0,0,0,0) 27.8px), radial-gradient(circle at 24% 30%, ${ink} 0 2px, rgba(0,0,0,0) 2.6px), radial-gradient(circle at 40% 74%, ${ink} 0 1.6px, rgba(0,0,0,0) 2.2px), radial-gradient(circle at 12% 66%, ${ink} 0 1.4px, rgba(0,0,0,0) 2px)` };\n    default:\n      return { backgroundImage: `repeating-linear-gradient(-52deg, ${ink} 0 7px, rgba(0,0,0,0) 7px 17px)` };\n  }\n}\n\nfunction Matchbook({ i, open, onPick, reduce, uid }: { i: number; open: boolean; onPick: () => void; reduce: boolean; uid: string }) {\n  const b = books[i];\n  const angle = useMotionValue(open ? REST : 0);\n  const started = useRef(false);\n\n  useEffect(() => {\n    if (reduce) {\n      angle.set(open ? REST : 0);\n      return;\n    }\n    if (!started.current) {\n      // first paint: books that start open let their covers bob once\n      started.current = true;\n      if (!open) return;\n      const bob = animate(angle, [REST, REST - 30, REST], { duration: 1, delay: 0.6 + i * 0.12, ease: \"easeInOut\" });\n      return () => bob.stop();\n    }\n    const run = animate(angle, open ? REST : 0, open ? { type: \"spring\", stiffness: 150, damping: 12.5, mass: 1 } : { type: \"spring\", stiffness: 230, damping: 24 });\n    return () => run.stop();\n  }, [angle, i, open, reduce]);\n\n  const flip = useTransform(angle, (v) => `rotateX(${clamp(v, 0, 178).toFixed(2)}deg)`);\n  const frontShade = useTransform(angle, (v) => clamp(v / 90, 0, 1) * 0.5);\n  const backShade = useTransform(angle, (v) => clamp((REST - v) / 80, 0, 1) * 0.42);\n  const inside = useTransform(angle, (v) => (v > 88 ? \"visible\" : \"hidden\"));\n  const thrown = useTransform(angle, (v) => clamp((v - 96) / 70, 0, 1) * 0.62);\n  const lifted = useTransform(angle, (v) => Math.sin((clamp(v, 0, 180) * Math.PI) / 180) * 0.55);\n  const light = b.ink.toLowerCase() > \"#8\";\n\n  return (\n    <div className=\"relative mx-auto w-full max-w-[184px] text-[15px] sm:text-[17px]\" style={{ transform: `translateY(${LAY[i].y}px) rotate(${LAY[i].r}deg)`, perspective: 950, perspectiveOrigin: \"50% 62%\" }}>\n      {/* empty bar top the cover falls back onto */}\n      <div aria-hidden=\"true\" className=\"relative aspect-[184/170]\">\n        <motion.div className=\"absolute inset-x-1 bottom-0 top-3 rounded-[4px] bg-black\" style={{ opacity: thrown, filter: \"blur(9px)\" }} />\n      </div>\n\n      <div className=\"relative\">\n        <div aria-hidden=\"true\" className=\"absolute -inset-x-1 -bottom-2 top-2 rounded-[6px] bg-black\" style={{ opacity: 0.75, filter: \"blur(11px)\", transform: \"translateY(9px)\" }} />\n        <motion.div aria-hidden=\"true\" className=\"absolute inset-x-0 top-0 h-full rounded-[4px] bg-black\" style={{ opacity: lifted, filter: \"blur(14px)\", transform: \"translateY(18px) scale(1.05)\" }} />\n\n        {/* inside the book: the back card and two combs of matches */}\n        <div aria-hidden=\"true\" className=\"relative aspect-[184/170] overflow-hidden rounded-t-[3px]\" style={{ background: \"#cfc2a6\", backgroundImage: `linear-gradient(180deg, rgba(0,0,0,0.55) 0, rgba(0,0,0,0.16) 9%, rgba(0,0,0,0) 26%, rgba(0,0,0,0.25) 100%), ${CARD}`, boxShadow: \"inset 0 0 0 1px rgba(0,0,0,0.25)\" }}>\n          {[1, 0].map((row) => (\n            <div key={row} className=\"absolute bottom-0 flex justify-between\" style={{ left: row ? \"9%\" : \"5.5%\", right: row ? \"5.5%\" : \"9%\", top: row ? \"13%\" : \"24%\", filter: row ? \"brightness(0.74)\" : \"drop-shadow(0 3px 3px rgba(0,0,0,0.55))\" }}>\n              {Array.from({ length: STICKS }, (_, k) => (\n                <span key={k} className=\"relative block h-full\" style={{ width: `${80 / STICKS}%`, background: \"linear-gradient(90deg, #a98a55 0%, #ecd9ab 26%, #f7e9c4 52%, #d2b982 82%, #8f7343 100%)\", transform: `translateY(${[0, 3, -2, 2, 0, -3, 1, 3, -1, 2][(k + row * 3) % 10]}px)` }}>\n                  <span className=\"absolute -inset-x-[22%] -top-[6px] block h-[21px] rounded-[48%_48%_42%_42%]\" style={{ background: `radial-gradient(70% 60% at 36% 28%, rgba(255,255,255,0.75) 0%, rgba(255,255,255,0) 46%), radial-gradient(90% 90% at 50% 40%, ${b.head} 40%, rgba(0,0,0,0.55) 130%)`, backgroundColor: b.head, boxShadow: \"0 1px 2px rgba(0,0,0,0.6), inset 0 -3px 3px rgba(0,0,0,0.3)\" }} />\n                </span>\n              ))}\n            </div>\n          ))}\n        </div>\n\n        {/* the cover: printed outside, answer inside, hinged along its top fold */}\n        <motion.div className=\"absolute inset-x-0 top-0 aspect-[184/170]\" style={{ transformOrigin: \"50% 0\", transformStyle: \"preserve-3d\", transform: flip }}>\n          <div aria-hidden=\"true\" className=\"absolute inset-0 overflow-hidden rounded-t-[3px] rounded-b-[1px]\" style={{ backfaceVisibility: \"hidden\", WebkitBackfaceVisibility: \"hidden\", background: b.cover, color: b.ink }}>\n            <div className=\"absolute inset-x-0 bottom-0 top-[60%]\" style={{ ...motif(b.motif, b.ink), opacity: 0.94 }} />\n            <div className=\"absolute inset-x-[9%] top-[8%]\">\n              <div className=\"flex items-baseline justify-between font-mono text-[0.44em] font-bold uppercase tracking-[0.16em]\">\n                <span>{bar.name}</span>\n                <span>No. {i + 1}</span>\n              </div>\n              <div className=\"mt-[0.55em] h-[2px]\" style={{ background: b.ink }} />\n              <div className=\"mt-[0.5em] text-[1em] font-black uppercase leading-[1.02] tracking-[-0.01em]\">{b.q}</div>\n            </div>\n            <div className=\"absolute inset-[5px] rounded-[1px]\" style={{ border: `1.5px solid ${b.ink}`, opacity: 0.9 }} />\n            <div className=\"absolute inset-0\" style={{ backgroundImage: `linear-gradient(180deg, rgba(255,255,255,0.5) 0, rgba(255,255,255,0.12) 4px, rgba(255,255,255,0) 14px), linear-gradient(118deg, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0) 34%, rgba(0,0,0,0) 64%, rgba(0,0,0,0.2) 100%), ${CARD}`, boxShadow: \"inset 0 0 0 0.5px rgba(0,0,0,0.35)\" }} />\n            <motion.div className=\"absolute inset-0 bg-black\" style={{ opacity: frontShade }} />\n          </div>\n          <motion.div\n            id={`${uid}-a${i}`}\n            role=\"region\"\n            aria-labelledby={`${uid}-q${i}`}\n            onClick={onPick}\n            className=\"absolute inset-0 cursor-pointer overflow-hidden rounded-t-[3px] rounded-b-[1px] text-[#221a14]\"\n            style={{ backfaceVisibility: \"hidden\", WebkitBackfaceVisibility: \"hidden\", transform: \"rotateX(180deg)\", visibility: inside, background: \"#f6eeda\", backgroundImage: `linear-gradient(180deg, rgba(60,40,10,0.12) 0, rgba(60,40,10,0) 12%, rgba(60,40,10,0) 84%, rgba(40,25,5,0.28) 100%), linear-gradient(112deg, rgba(255,255,255,0.5), rgba(255,255,255,0) 45%)`, boxShadow: `inset 0 0 0 1px rgba(0,0,0,0.2), inset 0 3px 0 ${b.cover}` }}\n          >\n            <div className=\"absolute inset-x-[9%] top-[9%]\">\n              <div className=\"flex items-baseline justify-between font-mono text-[0.44em] font-bold uppercase tracking-[0.16em]\" style={{ color: light ? b.cover : b.ink }}>\n                <span>Answer</span>\n                <span aria-hidden=\"true\">No. {i + 1}</span>\n              </div>\n              <p className=\"m-0 mt-[0.6em] text-[0.73em] font-medium leading-[1.36]\">{b.a}</p>\n            </div>\n            <motion.div aria-hidden=\"true\" className=\"absolute inset-0 bg-black\" style={{ opacity: backShade }} />\n          </motion.div>\n        </motion.div>\n\n        {/* the folded front flap: staple, striking strip */}\n        <div aria-hidden=\"true\" className=\"relative -mt-[5px] h-[31px] overflow-hidden rounded-b-[3px]\" style={{ background: b.cover, boxShadow: \"0 -2px 3px rgba(0,0,0,0.45), inset 0 0 0 0.5px rgba(0,0,0,0.35)\" }}>\n          <div className=\"absolute inset-x-0 bottom-[5px] h-[13px]\" style={{ background: \"#2a1b14\", backgroundImage: GRIT, boxShadow: \"inset 0 1px 0 rgba(0,0,0,0.7), inset 0 -1px 0 rgba(255,255,255,0.12)\" }} />\n          <div className=\"absolute inset-0\" style={{ backgroundImage: `linear-gradient(180deg, rgba(255,255,255,0.45) 0, rgba(255,255,255,0) 4px, rgba(0,0,0,0) 80%, rgba(0,0,0,0.35) 100%), ${CARD}` }} />\n          <span className=\"absolute left-1/2 top-[4px] block h-[3.5px] w-[26px] -translate-x-1/2 rounded-full\" style={{ background: \"linear-gradient(180deg, #ffffff, #a9adb3 55%, #5c6068)\", boxShadow: \"0 1px 1px rgba(0,0,0,0.6)\" }} />\n        </div>\n\n        <h3 className=\"absolute inset-0 m-0\">\n          <button type=\"button\" id={`${uid}-q${i}`} aria-expanded={open} aria-controls={`${uid}-a${i}`} onClick={onPick} className=\"block h-full w-full cursor-pointer rounded-[3px] outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#15110f]\">\n            <span className=\"sr-only\">{b.q}</span>\n          </button>\n        </h3>\n      </div>\n    </div>\n  );\n}\n\nexport default function MatchbookFaq() {\n  const reduce = !!useReducedMotion();\n  const uid = useId();\n  const [open, setOpen] = useState<number[]>([1, 4]);\n  const idle = useRef({ touched: false, paused: false, step: 0 });\n\n  // until someone touches it, the row opens the next book and shuts the oldest\n  useEffect(() => {\n    if (reduce) return;\n    const s = idle.current;\n    const t = window.setInterval(() => {\n      if (s.touched || s.paused || document.hidden) return;\n      const n = TOUR[s.step % TOUR.length];\n      s.step += 1;\n      setOpen((v) => [...v.filter((x) => x !== n).slice(-1), n]);\n    }, 4000);\n    return () => window.clearInterval(t);\n  }, [reduce]);\n\n  const pick = (i: number) => {\n    idle.current.touched = true;\n    setOpen((v) => (v.includes(i) ? v.filter((x) => x !== i) : [...v, i]));\n  };\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#15110f]\">\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(60% 70% at 50% 44%, #3a2a20 0%, #211813 46%, #0d0a09 100%)\" }} />\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 opacity-40 mix-blend-overlay\" style={{ backgroundImage: CARD }} />\n      <div onPointerEnter={() => (idle.current.paused = true)} onPointerLeave={() => (idle.current.paused = false)} onFocus={() => (idle.current.paused = true)} onBlur={() => (idle.current.paused = false)} className=\"relative mx-auto w-full max-w-[1280px] px-4 pb-28 pt-10 sm:px-6 sm:pt-16 xl:pb-40 xl:pt-28\">\n        <h2 className=\"sr-only\">{bar.name}: questions and answers</h2>\n        <div className=\"grid grid-cols-2 gap-x-4 gap-y-3 sm:grid-cols-3 sm:gap-x-6 xl:grid-cols-6 xl:gap-x-5\">\n          {books.map((_, i) => (\n            <Matchbook key={i} i={i} open={open.includes(i)} onPick={() => pick(i)} reduce={reduce} uid={uid} />\n          ))}\n        </div>\n        <p className=\"absolute bottom-5 left-5 m-0 font-mono text-[11px] uppercase leading-relaxed tracking-[0.18em] text-[#b9a48c] sm:bottom-7 sm:left-8\">\n          <span className=\"text-[#fff1d8]\">{bar.name}</span>\n          <span className=\"block\">Press a matchbook to lift its cover</span>\n        </p>\n      </div>\n    </section>\n  );\n}\n"}]}
