{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tile-flip-hero",
  "title": "Tile Flip Hero",
  "description": "A photo hero cut into a grid of tiles that flip in a wave to reveal the next picture. Click any tile and the wave starts from there. Autoplay can be paused.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/tile-flip-hero/tile-flip-hero.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TileFlipTheme = \"dark\" | \"light\";\n\nexport interface TileSlide {\n  image: string;\n  alt?: string;\n  /** Small line above the title. */\n  place?: string;\n  title: string;\n  description?: string;\n  cta?: { label: string; href?: string; onClick?: () => void };\n}\n\nexport interface TileFlipHeroProps extends React.ComponentPropsWithoutRef<\"section\"> {\n  theme?: TileFlipTheme;\n  slides?: TileSlide[];\n  /** Tiles across and down. */\n  columns?: number;\n  rows?: number;\n  /** Seconds between flips. 0 turns autoplay off. */\n  interval?: number;\n  /** Space between tiles, in px. */\n  gap?: number;\n  /** Height from tablet width up, in px. */\n  minHeight?: number;\n  onSlideChange?: (index: number) => void;\n}\n\nconst PALETTES = {\n  dark: {\n    bg: \"#090909\",\n    text: \"#FFFFFF\",\n    sub: \"rgba(255,255,255,0.8)\",\n    scrim: \"9,9,9\",\n    btn: \"#FFFFFF\",\n    btnText: \"#090909\",\n    chip: \"rgba(9,9,9,0.5)\",\n  },\n  light: {\n    bg: \"#F7F6F2\",\n    text: \"#141310\",\n    sub: \"rgba(20,19,16,0.76)\",\n    scrim: \"247,246,242\",\n    btn: \"#141310\",\n    btnText: \"#F7F6F2\",\n    chip: \"rgba(247,246,242,0.72)\",\n  },\n};\n\nconst DEFAULT_SLIDES: TileSlide[] = [\n  {\n    image: \"/demo/pages/travel-agency/hero.webp\",\n    alt: \"Hot air balloons over the rock valleys of Cappadocia at sunrise\",\n    place: \"Cappadocia, Türkiye\",\n    title: \"Rise before the sun\",\n    description:\n      \"Four mornings in the valleys, with a cave room to come back to.\",\n    cta: { label: \"See this trip\" },\n  },\n  {\n    image: \"/demo/pages/yacht-boat-rental/hero.webp\",\n    alt: \"Boats anchored in a turquoise bay, seen from above\",\n    place: \"Göcek, Türkiye\",\n    title: \"Anchor somewhere quiet\",\n    description:\n      \"A week between twelve bays, with a skipper who knows them all.\",\n    cta: { label: \"See this trip\" },\n  },\n  {\n    image: \"/demo/pages/car-rental/hero.webp\",\n    alt: \"A silver car on a winding mountain road\",\n    place: \"Transfăgărășan, Romania\",\n    title: \"Take the long road\",\n    description:\n      \"Nine days of switchbacks, with the car and the hotels sorted.\",\n    cta: { label: \"See this trip\" },\n  },\n];\n\nconst TURN = 760;\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction Slice({\n  slide,\n  col,\n  row,\n  columns,\n  rows,\n}: {\n  slide: TileSlide;\n  col: number;\n  row: number;\n  columns: number;\n  rows: number;\n}) {\n  return (\n    // Every tile holds the whole photo, shifted so only its own piece shows.\n    // eslint-disable-next-line @next/next/no-img-element\n    <img\n      src={slide.image}\n      alt=\"\"\n      draggable={false}\n      className=\"absolute max-w-none object-cover select-none\"\n      style={{\n        width: `${columns * 100}%`,\n        height: `${rows * 100}%`,\n        left: `${-col * 100}%`,\n        top: `${-row * 100}%`,\n      }}\n    />\n  );\n}\n\nexport function TileFlipHero({\n  theme = \"dark\",\n  slides = DEFAULT_SLIDES,\n  columns = 8,\n  rows = 6,\n  interval = 5,\n  gap = 2,\n  minHeight = 720,\n  onSlideChange,\n  className,\n  style,\n  ...props\n}: TileFlipHeroProps) {\n  const pal = PALETTES[theme];\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLElement>(null);\n  const n = slides.length;\n  // Each step turns every tile half a turn. The side coming round is given its new slide while it faces away.\n  const [wave, setWave] = React.useState({ step: 0, col: 0, row: 0 });\n  const [playing, setPlaying] = React.useState(true);\n  const [seen, setSeen] = React.useState(true);\n  const { step } = wave;\n  const index = step % n;\n  const even = step % 2 === 0 ? step : step - 1;\n  const odd = step % 2 === 1 ? step : step - 1;\n  const sideA = slides[even % n];\n  const sideB = slides[(odd + n) % n];\n\n  const go = React.useCallback(\n    (col: number, row: number, to?: number) => {\n      setWave((w) => {\n        // Forward to the requested slide, or just the next one.\n        const ahead = to === undefined ? 1 : (to - (w.step % n) + n) % n || n;\n        return { step: w.step + ahead, col, row };\n      });\n    },\n    [n],\n  );\n\n  React.useEffect(() => {\n    onSlideChange?.(index);\n  }, [index, onSlideChange]);\n\n  // Have the next photo ready before its tiles come round.\n  React.useEffect(() => {\n    const next = new Image();\n    next.src = slides[(index + 1) % n].image;\n  }, [index, n, slides]);\n\n  React.useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const io = new IntersectionObserver(([entry]) =>\n      setSeen(entry.isIntersecting),\n    );\n    io.observe(el);\n    return () => io.disconnect();\n  }, []);\n\n  React.useEffect(() => {\n    if (!interval || !playing || !seen || reduce || n < 2) return;\n    const timer = window.setTimeout(() => {\n      // The wave starts from a different corner each time.\n      const corner = step % 4;\n      go(corner % 2 ? columns - 1 : 0, corner > 1 ? rows - 1 : 0);\n    }, interval * 1000);\n    return () => window.clearTimeout(timer);\n  }, [interval, playing, seen, reduce, n, step, columns, rows, go]);\n\n  const tiles: { col: number; row: number }[] = [];\n  for (let row = 0; row < rows; row++) {\n    for (let col = 0; col < columns; col++) tiles.push({ col, row });\n  }\n\n  return (\n    <section\n      ref={rootRef}\n      aria-roledescription=\"carousel\"\n      aria-label=\"Featured trips\"\n      className={cn(\"@container relative w-full overflow-hidden\", className)}\n      style={\n        {\n          \"--h\": `${minHeight}px`,\n          background: pal.bg,\n          color: pal.text,\n          fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n          ...style,\n        } as React.CSSProperties\n      }\n      {...props}\n    >\n      <div className=\"relative flex min-h-[640px] flex-col justify-end @[640px]:min-h-[var(--h)]\">\n        <div\n          aria-hidden=\"true\"\n          className=\"absolute inset-0 grid cursor-pointer\"\n          // Worked out from the position, so the gaps between tiles count too.\n          onClick={(e) => {\n            const r = e.currentTarget.getBoundingClientRect();\n            go(\n              Math.min(\n                columns - 1,\n                Math.floor(((e.clientX - r.left) / r.width) * columns),\n              ),\n              Math.min(\n                rows - 1,\n                Math.floor(((e.clientY - r.top) / r.height) * rows),\n              ),\n            );\n          }}\n          style={{\n            gap,\n            gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))`,\n            gridTemplateRows: `repeat(${rows}, minmax(0, 1fr))`,\n          }}\n        >\n          {tiles.map(({ col, row }) => {\n            const distance = Math.hypot(col - wave.col, row - wave.row);\n            const delay = reduce ? 0 : Math.round(distance * 62);\n            const showA = step % 2 === 0;\n            const face =\n              \"absolute inset-0 overflow-hidden [-webkit-backface-visibility:hidden] [backface-visibility:hidden]\";\n            const swap = {\n              transitionProperty: \"opacity\",\n              transitionDuration: \"0ms\",\n              transitionDelay: reduce ? \"0ms\" : `${delay + TURN * 0.42}ms`,\n            };\n            return (\n              <div\n                key={`${col}-${row}`}\n                className=\"group relative [perspective:900px]\"\n              >\n                <div\n                  className=\"relative size-full [transform-style:preserve-3d]\"\n                  style={{\n                    transform: `rotateY(${step * 180}deg)`,\n                    transition: reduce\n                      ? \"none\"\n                      : `transform ${TURN}ms cubic-bezier(0.45,0.05,0.2,1) ${delay}ms`,\n                  }}\n                >\n                  <div\n                    className={face}\n                    style={{ ...swap, opacity: showA ? 1 : 0 }}\n                  >\n                    <Slice\n                      slide={sideA}\n                      col={col}\n                      row={row}\n                      columns={columns}\n                      rows={rows}\n                    />\n                  </div>\n                  <div\n                    className={face}\n                    style={{\n                      ...swap,\n                      opacity: showA ? 0 : 1,\n                      transform: \"rotateY(180deg)\",\n                    }}\n                  >\n                    <Slice\n                      slide={sideB}\n                      col={col}\n                      row={row}\n                      columns={columns}\n                      rows={rows}\n                    />\n                  </div>\n                </div>\n                {/* Kept outside the 3D element: a filter on it would flatten the tile and hide both faces. */}\n                <span className=\"pointer-events-none absolute inset-0 bg-white opacity-0 transition-opacity duration-200 group-hover:opacity-10\" />\n              </div>\n            );\n          })}\n        </div>\n\n        <div\n          aria-hidden=\"true\"\n          className=\"pointer-events-none absolute inset-0\"\n          style={{\n            background: `linear-gradient(to top, rgba(${pal.scrim},0.9) 0%, rgba(${pal.scrim},0.55) 26%, rgba(${pal.scrim},0) 56%)`,\n          }}\n        />\n\n        <div className=\"pointer-events-none relative flex flex-col gap-5 p-6 @[720px]:flex-row @[720px]:items-end @[720px]:justify-between @[720px]:p-10\">\n          {/* All the captions share one cell, so a longer one never changes the height. */}\n          <div className=\"grid min-w-0 grid-cols-[minmax(0,1fr)]\">\n            {slides.map((s, i) => {\n              const on = i === index;\n              return (\n                <div\n                  key={s.title}\n                  aria-hidden={!on}\n                  inert={!on}\n                  className=\"col-start-1 row-start-1 self-end motion-safe:transition-[opacity,transform] motion-safe:duration-500\"\n                  style={{\n                    opacity: on ? 1 : 0,\n                    transform: on ? \"none\" : \"translateY(10px)\",\n                    transitionDelay: on ? \"280ms\" : \"0ms\",\n                  }}\n                >\n                  {s.place && (\n                    <p\n                      className=\"m-0 text-[13px] font-medium\"\n                      style={{ color: pal.sub }}\n                    >\n                      {s.place}\n                    </p>\n                  )}\n                  <h1 className=\"m-0 mt-1.5 max-w-[14ch] text-[42px] leading-[1.02] font-medium tracking-[-0.035em] @[720px]:text-[64px]\">\n                    {s.title}\n                  </h1>\n                  {s.description && (\n                    <p\n                      className=\"m-0 mt-3 max-w-[40ch] text-[15px] leading-snug\"\n                      style={{ color: pal.sub }}\n                    >\n                      {s.description}\n                    </p>\n                  )}\n                  {s.cta &&\n                    (s.cta.href ? (\n                      <a\n                        href={s.cta.href}\n                        className=\"pointer-events-auto mt-4 inline-flex h-10 items-center rounded-full px-5 text-[13.5px] font-semibold no-underline transition-opacity hover:opacity-85\"\n                        style={{ background: pal.btn, color: pal.btnText }}\n                      >\n                        {s.cta.label}\n                      </a>\n                    ) : (\n                      <button\n                        type=\"button\"\n                        onClick={s.cta.onClick}\n                        className=\"pointer-events-auto mt-4 inline-flex h-10 cursor-pointer items-center rounded-full px-5 text-[13.5px] font-semibold transition-opacity hover:opacity-85\"\n                        style={{ background: pal.btn, color: pal.btnText }}\n                      >\n                        {s.cta.label}\n                      </button>\n                    ))}\n                </div>\n              );\n            })}\n          </div>\n\n          <div\n            className=\"pointer-events-auto flex h-10 shrink-0 items-center gap-1 self-start rounded-full pr-1 pl-3.5 backdrop-blur-md @[720px]:self-auto\"\n            style={{ background: pal.chip }}\n          >\n            {slides.map((s, i) => (\n              <button\n                key={s.title}\n                type=\"button\"\n                aria-label={`Show ${s.title}`}\n                aria-current={i === index}\n                onClick={() => {\n                  if (i !== index)\n                    go(Math.floor(columns / 2), Math.floor(rows / 2), i);\n                }}\n                className=\"h-6 cursor-pointer px-1\"\n              >\n                <span\n                  className=\"block h-1.5 rounded-full transition-[width,opacity] duration-300\"\n                  style={{\n                    width: i === index ? 22 : 6,\n                    opacity: i === index ? 1 : 0.45,\n                    background: pal.text,\n                  }}\n                />\n              </button>\n            ))}\n            {interval > 0 && (\n              <button\n                type=\"button\"\n                aria-label={playing ? \"Pause\" : \"Play\"}\n                onClick={() => setPlaying((p) => !p)}\n                className=\"ml-1 grid size-8 cursor-pointer place-items-center rounded-full\"\n              >\n                <svg\n                  width=\"12\"\n                  height=\"12\"\n                  viewBox=\"0 0 12 12\"\n                  fill=\"currentColor\"\n                  aria-hidden=\"true\"\n                >\n                  {playing ? (\n                    <path d=\"M2.5 1.5h2.4v9H2.5zM7.1 1.5h2.4v9H7.1z\" />\n                  ) : (\n                    <path d=\"M2.5 1.2v9.6L10.5 6z\" />\n                  )}\n                </svg>\n              </button>\n            )}\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}