{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"sliding-block-puzzle-tray-where-page-sections-are-thick-glazed-tiles-you-slide-to-rearrange-a-homepage-on-a-phone","type":"registry:component","title":"Sliding Block Puzzle Tray Where Page Sections Are Thick Glazed Tiles You Slide to Rearrange a Homepage on a Phone","description":"A tray of thick glazed tiles, each a section of a shop's homepage: slide one and the page on the phone beside it rearranges. Free React and HTML to copy.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/sliding-block-puzzle-tray-where-page-sections-are-thick-glazed-tiles-you-slide-to-rearrange-a-homepage-on-a-phone.tsx","type":"registry:component","content":"import { useEffect, useLayoutEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent, ReactNode } from \"react\";\nimport { motion, useMotionValue, useReducedMotion, useSpring } from \"framer-motion\";\n\n// The tiles: where each starts (column, row), how many cells it covers and what it is glazed with.\ntype Glaze = \"photo\" | \"white\" | \"blue\" | \"black\" | \"stone\";\nconst tiles: { id: string; name: string; c: number; r: number; w: number; h: number; glaze: Glaze }[] = [\n  { id: \"hero\", name: \"Hero\", c: 0, r: 0, w: 2, h: 2, glaze: \"photo\" },\n  { id: \"cone\", name: \"Photo\", c: 2, r: 0, w: 1, h: 2, glaze: \"photo\" },\n  { id: \"flavours\", name: \"Flavours\", c: 0, r: 2, w: 2, h: 1, glaze: \"white\" },\n  { id: \"order\", name: \"Order\", c: 2, r: 2, w: 1, h: 1, glaze: \"blue\" },\n  { id: \"hours\", name: \"Hours\", c: 0, r: 3, w: 1, h: 1, glaze: \"black\" },\n  { id: \"tubs\", name: \"Tubs\", c: 1, r: 3, w: 2, h: 1, glaze: \"stone\" },\n  { id: \"map\", name: \"Address\", c: 1, r: 4, w: 1, h: 1, glaze: \"white\" },\n];\nconst shop = \"Pico Gelato\";\nconst headline = \"Gelato, churned at dawn.\";\nconst flavours = [\"Pistachio\", \"Fior di latte\", \"Blood orange\"];\nconst photos = {\n  hero: { bg: \"#d8456a\", src: \"https://images.pexels.com/photos/5535554/pexels-photo-5535554.jpeg?auto=compress&cs=tinysrgb&w=500\", at: \"50% 40%\" },\n  cone: { bg: \"#e9e4de\", src: \"https://images.pexels.com/photos/7761650/pexels-photo-7761650.jpeg?auto=compress&cs=tinysrgb&w=400\", at: \"42% 46%\" },\n};\n\nconst COLS = 3, ROWS = 5, P = 78, G = 4; // grid, cell pitch and the gap between tiles\nconst BW = G + COLS * P, BH = G + ROWS * P; // the board inside the tray\nconst RIM = 17, TW = BW + RIM * 2, TH = BH + RIM * 2;\nconst K = 0.72; // the phone shows the same board at this scale\nconst HEAD = 26, BEZ = 7, PHW = BW * K + BEZ * 2, PHH = BH * K + HEAD + BEZ * 2;\nconst W = 16 + TW + 34 + PHW + 16, H = TH + 40;\nconst TX = 16, TY = 10, PHX = TX + TW + 34, PHY = TY + (TH - PHH) / 2 + 6;\nconst BLUE = \"#4F98FB\";\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, sans-serif';\nconst RX = 31, RZ = -5;\nconst DIRS = { up: [0, -1], left: [-1, 0], right: [1, 0], down: [0, 1] } as const;\ntype Dir = keyof typeof DIRS;\ntype Pos = Record<string, { c: number; r: number }>;\nconst useIsoLayoutEffect = typeof window !== \"undefined\" ? useLayoutEffect : useEffect;\n\nconst start: Pos = Object.fromEntries(tiles.map((t) => [t.id, { c: t.c, r: t.r }]));\nconst extrude = (edge: string, n: number, cast: string) => `inset 0 1px 0 rgba(255,255,255,0.75), inset 0 -1px 0 rgba(0,0,0,0.18), ${Array.from({ length: n }, (_, i) => `0 ${i + 1}px 0 ${edge}`).join(\", \")}, ${cast}`;\nconst GLAZE: Record<Glaze, { face: string; edge: string }> = {\n  photo: { face: \"#F6F4EF\", edge: \"#b9b4a6\" },\n  white: { face: \"linear-gradient(165deg,#ffffff 0%,#F6F4EF 55%,#e9e5db 100%)\", edge: \"#b9b4a6\" },\n  blue: { face: \"linear-gradient(165deg,#86b9ff 0%,#4F98FB 50%,#3f86ee 100%)\", edge: \"#2459b3\" },\n  black: { face: \"linear-gradient(165deg,#3a3f4b 0%,#14171e 40%,#0B0D12 100%)\", edge: \"#000000\" },\n  stone: { face: \"linear-gradient(165deg,#ebe6da 0%,#d9d3c4 60%,#cbc4b3 100%)\", edge: \"#9d9684\" },\n};\n\n// Can this tile move one cell that way without leaving the board or meeting another tile?\nfunction free(pos: Pos, id: string, dir: Dir) {\n  const t = tiles.find((x) => x.id === id);\n  if (!t) return false;\n  const [dc, dr] = DIRS[dir], c = pos[id].c + dc, r = pos[id].r + dr;\n  if (c < 0 || r < 0 || c + t.w > COLS || r + t.h > ROWS) return false;\n  return tiles.every((o) => o.id === id || c + t.w <= pos[o.id].c || pos[o.id].c + o.w <= c || r + t.h <= pos[o.id].r || pos[o.id].r + o.h <= r);\n}\nconst back: Record<Dir, Dir> = { up: \"down\", down: \"up\", left: \"right\", right: \"left\" };\n\n// What is printed on each tile. Drawn at tray size; the phone shows the same drawing scaled down.\nfunction Face({ id }: { id: string }): ReactNode {\n  if (id === \"hero\" || id === \"cone\") {\n    const p = photos[id];\n    return (\n      <span className=\"absolute inset-0 block\" style={{ background: p.bg }}>\n        <img src={p.src} alt=\"\" width={id === \"hero\" ? 152 : 74} height={152} className=\"absolute inset-0 h-full w-full object-cover\" style={{ objectPosition: p.at }} />\n        {id === \"hero\" && (\n          <>\n            <span className=\"absolute inset-0 bg-[linear-gradient(200deg,rgba(11,13,18,0)_38%,rgba(11,13,18,0.82)_100%)]\" />\n            <span className=\"absolute bottom-[11px] left-[11px] w-[118px] text-[19px] font-extrabold leading-[0.98] tracking-[-0.05em] text-white\">{headline}</span>\n          </>\n        )}\n      </span>\n    );\n  }\n  if (id === \"flavours\")\n    return (\n      <span className=\"absolute inset-0 flex flex-col justify-between p-[10px]\">\n        <span className=\"text-[6.5px] font-bold uppercase tracking-[0.2em] text-[#0B0D12]/70\">In the cabinet today</span>\n        <span className=\"flex flex-wrap gap-[4px]\">\n          {flavours.map((f, i) => <span key={f} className={`rounded-full px-[7px] py-[3px] text-[8px] font-extrabold tracking-[-0.02em] ${i === 0 ? \"bg-[#0B0D12] text-[#F6F4EF]\" : \"text-[#0B0D12] shadow-[inset_0_0_0_1.2px_rgba(11,13,18,0.8)]\"}`}>{f}</span>)}\n        </span>\n      </span>\n    );\n  if (id === \"order\")\n    return (\n      <span className=\"absolute inset-0 block p-[9px]\">\n        <span className=\"block text-[13px] font-extrabold leading-[1] tracking-[-0.045em] text-[#0B0D12]\">Order ahead</span>\n        <span className=\"absolute bottom-[8px] right-[8px] flex h-[22px] w-[22px] items-center justify-center rounded-full bg-[#0B0D12]\">\n          <svg viewBox=\"0 0 20 20\" className=\"h-[11px] w-[11px]\" fill=\"none\" stroke={BLUE} strokeWidth=\"2.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M5 15L15 5M8 5h7v7\" /></svg>\n        </span>\n      </span>\n    );\n  if (id === \"hours\")\n    return (\n      <span className=\"absolute inset-0 flex flex-col justify-between p-[9px] text-[#F6F4EF]\">\n        <span className=\"flex items-center gap-[4px] text-[6.5px] font-bold uppercase tracking-[0.2em]\"><i className=\"h-[5px] w-[5px] rounded-full bg-[#4F98FB]\" />Open</span>\n        <span>\n          <span className=\"block text-[15px] font-extrabold leading-[1] tracking-[-0.05em]\">11 to 10</span>\n          <span className=\"block text-[7px] font-semibold opacity-75\">every day</span>\n        </span>\n      </span>\n    );\n  if (id === \"tubs\")\n    return (\n      <span className=\"absolute inset-0 flex items-center justify-between p-[10px]\">\n        <span>\n          <span className=\"block w-[86px] text-[14px] font-extrabold leading-[1] tracking-[-0.045em] text-[#0B0D12]\">Take a tub home</span>\n          <span className=\"mt-[5px] block text-[7px] font-semibold text-[#0B0D12]/75\">Half litre or a full one</span>\n        </span>\n        <svg viewBox=\"0 0 40 40\" className=\"h-[40px] w-[40px]\" aria-hidden=\"true\">\n          <path d=\"M8 15h24l-3 19H11z\" fill=\"#0B0D12\" />\n          <rect x=\"5.5\" y=\"9\" width=\"29\" height=\"6\" rx=\"2\" fill=\"#0B0D12\" />\n          <rect x=\"13\" y=\"20\" width=\"14\" height=\"8\" rx=\"4\" fill={BLUE} />\n        </svg>\n      </span>\n    );\n  return (\n    <span className=\"absolute inset-0 flex flex-col justify-between p-[9px]\">\n      <svg viewBox=\"0 0 20 20\" className=\"h-[18px] w-[18px]\" aria-hidden=\"true\"><path d=\"M10 18s6-5.2 6-9.6A6 6 0 0 0 4 8.4C4 12.800 10 18 10 18z\" fill={BLUE} /><circle cx=\"10\" cy=\"8.3\" r=\"2.2\" fill=\"#0B0D12\" /></svg>\n      <span className=\"text-[10.5px] font-extrabold leading-[1.02] tracking-[-0.04em] text-[#0B0D12]\">14 Quay Street</span>\n    </span>\n  );\n}\n\nexport default function SlidingSectionTray() {\n  const reduce = useReducedMotion();\n  const root = useRef<HTMLDivElement>(null);\n  const [scale, setScale] = useState(1);\n  const [pos, setPos] = useState<Pos>(start);\n  const posRef = useRef(pos);\n  posRef.current = pos;\n  const [auto, setAuto] = useState(true);\n  const [stuck, setStuck] = useState<string | null>(null);\n  const [said, setSaid] = useState(\"Seven tiles, two free cells. Slide a tile and the homepage follows.\");\n  const drag = useRef<{ id: string; x: number; y: number } | null>(null);\n  const swiped = useRef(false);\n\n  useIsoLayoutEffect(() => {\n    const el = root.current;\n    if (!el) return;\n    const fit = () => setScale(Math.min(1.34, (el.clientWidth - 12) / W));\n    fit();\n    const ro = new ResizeObserver(fit);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  const slide = (id: string, dir: Dir, quiet = false) => {\n    const p = posRef.current;\n    if (!free(p, id, dir)) return false;\n    const [dc, dr] = DIRS[dir];\n    const next = { ...p, [id]: { c: p[id].c + dc, r: p[id].r + dr } };\n    posRef.current = next;\n    setPos(next);\n    if (!quiet) setSaid(`${tiles.find((t) => t.id === id)?.name} tile slid ${dir}. Now in column ${next[id].c + 1}, row ${next[id].r + 1}.`);\n    return true;\n  };\n  const refuse = (id: string) => {\n    setSaid(`${tiles.find((t) => t.id === id)?.name} tile is boxed in. Clear a space beside it first.`);\n    if (reduce) return;\n    setStuck(id);\n    window.setTimeout(() => setStuck((s) => (s === id ? null : s)), 320);\n  };\n  const attempt = (id: string, dir?: Dir) => {\n    setAuto(false);\n    const d = dir ?? (Object.keys(DIRS) as Dir[]).find((k) => free(posRef.current, id, k));\n    if (!d || !slide(id, d)) refuse(id);\n  };\n\n  // The self-running demo: hold the finished page, make a few legal moves, then take them back in reverse.\n  useEffect(() => {\n    if (reduce || !auto) return;\n    const done: { id: string; dir: Dir }[] = [];\n    let forward = true, rest = 4, seed = 7;\n    const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;\n    const tick = () => {\n      if (rest > 0) { rest--; return; }\n      if (forward) {\n        const last = done[done.length - 1];\n        const options = tiles.flatMap((t) => (Object.keys(DIRS) as Dir[]).filter((d) => free(posRef.current, t.id, d) && !(last && last.id === t.id && back[last.dir] === d)).map((d) => ({ id: t.id, dir: d })));\n        const pick = options[Math.floor(rand() * options.length)];\n        if (pick && slide(pick.id, pick.dir, true)) done.push(pick);\n        if (done.length >= 6) { forward = false; rest = 1; }\n      } else {\n        const m = done.pop();\n        if (m) slide(m.id, back[m.dir], true);\n        if (!done.length) { forward = true; rest = 3; }\n      }\n    };\n    const id = window.setInterval(tick, 1150);\n    return () => window.clearInterval(id);\n  }, [reduce, auto]);\n\n  const onKey = (e: ReactKeyboardEvent<HTMLButtonElement>, id: string) => {\n    const d = ({ ArrowUp: \"up\", ArrowDown: \"down\", ArrowLeft: \"left\", ArrowRight: \"right\" } as Record<string, Dir | undefined>)[e.key];\n    if (!d) return;\n    e.preventDefault();\n    attempt(id, d);\n  };\n  const onUp = (e: ReactPointerEvent<HTMLButtonElement>) => {\n    const d = drag.current;\n    drag.current = null;\n    if (!d || e.pointerType === \"touch\") return;\n    const dx = e.clientX - d.x, dy = e.clientY - d.y;\n    if (Math.max(Math.abs(dx), Math.abs(dy)) < 14) return;\n    swiped.current = true;\n    attempt(d.id, Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? \"right\" : \"left\") : dy > 0 ? \"down\" : \"up\");\n  };\n\n  // The table top leans a little towards the pointer.\n  const px = useMotionValue(RZ), py = useMotionValue(RX);\n  const rotateZ = useSpring(px, { stiffness: 80, damping: 16 }), rotateX = useSpring(py, { stiffness: 80, damping: 16 });\n  const onMove = (e: ReactPointerEvent<HTMLDivElement>) => {\n    if (reduce || e.pointerType === \"touch\") return;\n    const b = e.currentTarget.getBoundingClientRect();\n    px.set(RZ + ((e.clientX - b.left) / b.width - 0.5) * 4);\n    py.set(RX - ((e.clientY - b.top) / b.height - 0.5) * 6);\n  };\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 520, damping: 34, mass: 0.9 };\n\n  return (\n    <div ref={root} onPointerMove={onMove} onPointerLeave={() => { px.set(RZ); py.set(RX); }} style={{ fontFamily: FONT }} className=\"relative flex min-h-[400px] w-full flex-col items-center justify-center gap-1 overflow-hidden bg-[#E4DFD4] bg-[radial-gradient(72%_62%_at_46%_40%,#faf8f2_0%,#E4DFD4_55%,#cbc5b6_100%)] py-6 sm:min-h-[700px]\">\n      <style>{`@keyframes sliding-block-puzzle-tray-shake { 0%,100% { transform: translateX(0) } 25% { transform: translateX(-3px) } 60% { transform: translateX(3px) } 85% { transform: translateX(-1px) } } @keyframes sliding-block-puzzle-tray-drift { 0% { transform: rotate(-1.6deg) } 100% { transform: rotate(1.6deg) } } .sliding-block-puzzle-tray-drift { animation: sliding-block-puzzle-tray-drift 6s ease-in-out infinite alternate } @media (prefers-reduced-motion: reduce) { .sliding-block-puzzle-tray-drift { animation: none } }`}</style>\n      <div style={{ width: W * scale, height: H * scale }} className=\"relative\">\n        <div style={{ width: W, height: H, transform: `scale(${scale})`, transformOrigin: \"0 0\", perspective: 1500 }} className=\"absolute left-0 top-0\">\n          <motion.div style={{ rotateX: reduce ? RX : rotateX, rotateZ: reduce ? RZ : rotateZ, transformOrigin: \"50% 50%\" }} className=\"absolute inset-0\">\n            <div className=\"sliding-block-puzzle-tray-drift absolute inset-0\">\n            {/* the tray */}\n            <div style={{ left: TX, top: TY, width: TW, height: TH, boxShadow: `inset 0 1.5px 0 rgba(255,255,255,0.5), inset 1.5px 0 0 rgba(255,255,255,0.16), ${Array.from({ length: 13 }, (_, i) => `0 ${i + 1}px 0 ${i < 2 ? \"#23262f\" : \"#050608\"}`).join(\", \")}, 0 34px 34px rgba(60,48,26,0.45), 22px 60px 70px rgba(60,48,26,0.4)` }} className=\"absolute rounded-[22px] bg-[#14171e] bg-[linear-gradient(160deg,#4a505d_0%,#1d2029_24%,#0B0D12_100%)]\">\n              <i aria-hidden=\"true\" className=\"pointer-events-none absolute -left-[10px] top-[30px] h-[60px] w-[340px] -rotate-[24deg] bg-[linear-gradient(180deg,rgba(255,255,255,0),rgba(255,255,255,0.16),rgba(255,255,255,0))] blur-[5px]\" />\n              {/* the floor of the tray */}\n              <div style={{ left: RIM, top: RIM, width: BW, height: BH }} className=\"absolute rounded-[9px] bg-[#1b2030] bg-[radial-gradient(120%_90%_at_50%_100%,#27304a_0%,#1b2030_50%,#131725_100%)] shadow-[inset_0_10px_10px_rgba(0,0,0,0.85),inset_6px_0_8px_rgba(0,0,0,0.5),inset_-6px_0_8px_rgba(0,0,0,0.4),0_1.5px_0_rgba(255,255,255,0.22)]\">\n                <i aria-hidden=\"true\" className=\"absolute inset-0 rounded-[9px] opacity-40 [background:radial-gradient(circle,rgba(255,255,255,0.2)_0.5px,rgba(255,255,255,0)_0.9px)_0_0/3px_3px]\" />\n                {tiles.map((t) => {\n                  const g = GLAZE[t.glaze], w = t.w * P - G, h = t.h * P - G;\n                  return (\n                    <motion.button\n                      key={t.id}\n                      type=\"button\"\n                      initial={false}\n                      animate={{ x: G + pos[t.id].c * P, y: G + pos[t.id].r * P - 5 }}\n                      transition={spring}\n                      onClick={() => { if (swiped.current) { swiped.current = false; return; } attempt(t.id); }}\n                      onKeyDown={(e) => onKey(e, t.id)}\n                      onPointerDown={(e) => { drag.current = { id: t.id, x: e.clientX, y: e.clientY }; }}\n                      onPointerUp={onUp}\n                      aria-label={`${t.name} tile, column ${pos[t.id].c + 1}, row ${pos[t.id].r + 1}. Press to slide it into a free space, or use the arrow keys.`}\n                      style={{ width: w, height: h }}\n                      className=\"group absolute left-0 top-0 cursor-pointer rounded-[10px] outline-none focus-visible:ring-2 focus-visible:ring-[#4F98FB] focus-visible:ring-offset-2 focus-visible:ring-offset-[#1b2030]\"\n                    >\n                      <span aria-hidden=\"true\" style={{ background: g.face, boxShadow: extrude(g.edge, 8, \"0 14px 9px rgba(0,0,0,0.6)\"), animation: stuck === t.id ? \"sliding-block-puzzle-tray-shake 0.3s ease-in-out\" : undefined }} className=\"absolute inset-0 block overflow-hidden rounded-[10px] transition-transform duration-150 group-active:translate-y-[2px] motion-reduce:transition-none\">\n                        <Face id={t.id} />\n                        <i className=\"pointer-events-none absolute inset-0 rounded-[10px] bg-[linear-gradient(128deg,rgba(255,255,255,0.5)_0%,rgba(255,255,255,0.1)_22%,rgba(255,255,255,0)_40%)] shadow-[inset_0_1px_0_rgba(255,255,255,0.7),inset_0_0_0_1px_rgba(255,255,255,0.12)]\" />\n                      </span>\n                    </motion.button>\n                  );\n                })}\n              </div>\n            </div>\n\n            {/* the phone lying beside it, showing the same arrangement as a page */}\n            <div aria-hidden=\"true\" style={{ left: PHX, top: PHY, width: PHW, height: PHH, boxShadow: `inset 0 1px 0 rgba(255,255,255,0.45), ${Array.from({ length: 6 }, (_, i) => `0 ${i + 1}px 0 ${i < 1 ? \"#3a3f4b\" : \"#050608\"}`).join(\", \")}, 0 20px 22px rgba(60,48,26,0.45), 14px 36px 44px rgba(60,48,26,0.35)` }} className=\"absolute rounded-[24px] bg-[#0B0D12]\">\n              <span style={{ inset: BEZ }} className=\"absolute overflow-hidden rounded-[18px] bg-[#fffdf8]\">\n                <span className=\"absolute inset-x-0 top-0 flex items-center justify-between px-[11px]\" style={{ height: HEAD }}>\n                  <span className=\"text-[10px] font-extrabold tracking-[-0.03em] text-[#0B0D12]\">{shop}</span>\n                  <span className=\"flex flex-col gap-[2.5px]\"><i className=\"block h-[1.5px] w-[11px] rounded-full bg-[#0B0D12]\" /><i className=\"block h-[1.5px] w-[11px] rounded-full bg-[#0B0D12]\" /></span>\n                </span>\n                <span style={{ top: HEAD - 2, width: BW, height: BH, transform: `scale(${K})`, transformOrigin: \"0 0\" }} className=\"absolute left-0 block\">\n                  {tiles.map((t) => (\n                    <motion.span key={t.id} initial={false} animate={{ x: G + pos[t.id].c * P, y: G + pos[t.id].r * P }} transition={spring} style={{ width: t.w * P - G, height: t.h * P - G, background: GLAZE[t.glaze].face }} className=\"absolute left-0 top-0 block overflow-hidden rounded-[10px] shadow-[inset_0_0_0_1px_rgba(11,13,18,0.08)]\">\n                      <Face id={t.id} />\n                    </motion.span>\n                  ))}\n                </span>\n                <i className=\"pointer-events-none absolute inset-0 bg-[linear-gradient(122deg,rgba(255,255,255,0.4)_0%,rgba(255,255,255,0.05)_24%,rgba(255,255,255,0)_38%)]\" />\n              </span>\n            </div>\n            </div>\n          </motion.div>\n        </div>\n      </div>\n      <p className=\"relative flex max-w-[92%] items-center justify-center gap-2 text-center text-[12px] font-semibold tracking-[-0.005em] text-[#0B0D12]/85 sm:text-[13.5px]\">\n        <span aria-hidden=\"true\" className=\"h-[7px] w-[7px] shrink-0 rounded-full bg-[#4F98FB]\" />\n        <span aria-live={auto ? \"off\" : \"polite\"}>{said}</span>\n      </p>\n    </div>\n  );\n}\n"}]}
