{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"z-swap-plate-card","type":"registry:component","title":"Z-Swap Plate Card","description":"A work card on the lattice: every third in a run breaks by the rule, two gutters out with the bracket, and hovering the card it overlaps lifts that card above it, the bracket going with the lift, in 240ms.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/z-swap-plate-card.tsx","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\n\n/**\n * ZSwapPlateCard — a work card on the lattice that swaps layers with the neighbour it overlaps.\n *\n * In a run (direct children of a grid with the class bg-swap-run and a\n * column gap of --bg-g), every third card breaks by the rule, two gutters\n * out at its leading edge with the bracket, and the card it overlaps keeps\n * a gutter clear. Hover or focus that card and the two swap layers, the\n * bracket going with the lift, in 240ms. Your image, or a construction\n * plate drawn in code from a seed.\n *\n * Part of the Broken Grid kit: a Klein-blue field #2416f0, paper plates\n * #f1f0e8, ink #0a0a1f, and acid #e2ff3a only where a rule fires. One rule,\n * \"The Third\": every third module in a run overshoots its leading edge by two\n * gutters (overlapping its neighbour by one) and wears the acid bracket; the\n * ends never break. Radius 0, no shadows; reflows in 380ms, no bounce. Big\n * Shoulders through var(--font-big-shoulders) for display over Geist.\n * Respects prefers-reduced-motion. No dependencies beyond React. Paste it as\n * its own file: it repeats the kit's small helpers, which would clash in one\n * module. Load Big Shoulders with next/font (variable:\n * \"--font-big-shoulders\") on a parent, or from Google Fonts or @fontsource.\n * Needs Tailwind v4 (on v3.4, add the @tailwindcss/container-queries plugin).\n */\n\nconst MONO = 'var(--font-mono, \"Geist Mono\", ui-monospace, \"SFMono-Regular\", Menlo, Consolas, monospace)';\n\nconst DISPLAY = 'var(--font-big-shoulders, \"Big Shoulders\", \"Big Shoulders Display\", \"Arial Narrow\", \"Roboto Condensed\", sans-serif)';\n\n/**\n * A Klein-blue field, paper plates, blue-black ink, and acid for a rule firing.\n * Secondary and tertiary text clear 4.5:1 on their grounds (ink2 7.4, ink3 5.0\n * on paper; onField2 5.9, onField3 4.8 on the field); `edgeOnPaper` is the 3:1\n * boundary for inputs.\n */\nconst FIELD = {\n  field: \"#2416f0\",\n  raised: \"#3a2ff5\",\n  pressed: \"#1a0ec4\",\n  paper: \"#f1f0e8\",\n  ink: \"#0a0a1f\",\n  ink2: \"rgba(10,10,31,0.72)\",\n  ink3: \"rgba(10,10,31,0.6)\",\n  onField2: \"rgba(241,240,232,0.86)\",\n  onField3: \"rgba(241,240,232,0.76)\",\n  lineOnField: \"rgba(241,240,232,0.22)\",\n  lineOnPaper: \"rgba(10,10,31,0.14)\",\n  edgeOnPaper: \"rgba(10,10,31,0.5)\",\n  edgeOnField: \"rgba(241,240,232,0.6)\",\n  acid: \"#e2ff3a\",\n} as const;\n\n/** Layout changes: 380ms, no stagger, no bounce. Role hand-offs take 240ms. */\nconst BG_EASE = \"cubic-bezier(.2,.8,.2,1)\";\n\nconst BG_HANDOFF_MS = 240;\n\n/** Links: strip tabs and newlines, refuse backslashes, allow http(s), mailto, tel and same-site paths. */\nfunction safeHref(raw: string): string {\n  const v = raw.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (!v || v.includes(\"\\\\\")) return \"#\";\n  if (/^(https?:|mailto:|tel:)/i.test(v)) return v;\n  if (/^[/#?]/.test(v) && !/^\\/\\//.test(v)) return v;\n  return \"#\";\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/**\n * The rule, \"The Third\": in a run of `count` modules, module `index` (from 0)\n * breaks when it is every `every`th (3, 6, 9...) and is neither end of the run.\n * Use it for your own runs, so the break is computed and never placed by hand.\n */\nexport function bgBreaks(index: number, count: number, every = 3): boolean {\n  if (every < 2 || index <= 0 || index >= count - 1) return false;\n  return (index + 1) % every === 0;\n}\n\n// Each part carries only the CSS it needs, so any one of them works on its own.\n// revert-layer keeps an element's own corners when a host page's focus rule rounds or flattens them.\nconst FOCUS_CSS = \".bg-scope :focus-visible,.bg-scope:focus-visible{outline:2px solid var(--bg-ring,#e2ff3a)!important;outline-offset:3px;border-radius:revert-layer}\";\n\n/**\n * The bracket a module wears when the rule breaks it: an L of 2px, one gutter\n * long, 6px outside its leading corner, so it reads against the ground. Its\n * colour is --bg-mark (acid on the field, ink on paper; see bgMark).\n */\nfunction BgBracket({ on, size = 24, className = \"\" }: { on: boolean; size?: number; className?: string }) {\n  const reduced = useReducedMotion();\n  return (\n    <span\n      aria-hidden\n      className={\"pointer-events-none absolute left-0 top-0 block \" + className}\n      style={{\n        width: size,\n        height: size,\n        borderTop: `2px solid var(--bg-mark, ${FIELD.acid})`,\n        borderLeft: `2px solid var(--bg-mark, ${FIELD.acid})`,\n        transform: \"translate(-6px, -6px)\",\n        opacity: on ? 1 : 0,\n        transition: reduced ? \"none\" : `opacity ${BG_HANDOFF_MS}ms ${BG_EASE}`,\n      }}\n    />\n  );\n}\n\n/** A small deterministic generator, so a seed always draws the same plate on the server and in the browser. */\nfunction bgRandom(seed: number) {\n  let s = (seed * 2654435761) >>> 0 || 1;\n  return () => {\n    s ^= s << 13;\n    s ^= s >>> 17;\n    s ^= s << 5;\n    return ((s >>> 0) % 10000) / 10000;\n  };\n}\n\n/**\n * A construction plate: two or three primitives (a quarter circle, a bar, a\n * cross, a dot field) snapped to a 4 x 4 sub-lattice, drawn in code from a\n * seed. It stands in for work imagery and never looks like stock.\n */\nfunction BgConstruction({ seed, ink = FIELD.ink, ground = FIELD.paper, accent, className = \"\" }: { seed: number; ink?: string; ground?: string; accent?: string; className?: string }) {\n  const r = bgRandom(seed);\n  const cell = 25;\n  const shapes: ReactNode[] = [];\n  const pick = (n: number) => Math.floor(r() * n);\n  const count = 2 + pick(2);\n  for (let i = 0; i < count; i++) {\n    const kind = pick(4);\n    const cx = pick(3) * cell;\n    const cy = pick(3) * cell;\n    const fill = i === 0 && accent ? accent : ink;\n    if (kind === 0) {\n      const q = pick(4);\n      const x = cx + (q === 1 || q === 2 ? 2 * cell : 0);\n      const y = cy + (q >= 2 ? 2 * cell : 0);\n      const sx = q === 1 || q === 2 ? -1 : 1;\n      const sy = q >= 2 ? -1 : 1;\n      shapes.push(<path key={i} d={`M${x} ${y} L${x + sx * 2 * cell} ${y} A${2 * cell} ${2 * cell} 0 0 ${sx * sy > 0 ? 1 : 0} ${x} ${y + sy * 2 * cell} Z`} fill={fill} />);\n    } else if (kind === 1) {\n      const across = r() > 0.5;\n      shapes.push(<rect key={i} x={across ? 0 : cx} y={across ? cy + cell * 0.5 : 0} width={across ? 100 : cell} height={across ? cell : 100} fill={fill} />);\n    } else if (kind === 2) {\n      shapes.push(\n        <g key={i} fill={fill}>\n          <rect x={cx + cell * 0.8} y={cy} width={cell * 0.4} height={cell * 2} />\n          <rect x={cx} y={cy + cell * 0.8} width={cell * 2} height={cell * 0.4} />\n        </g>,\n      );\n    } else {\n      const dots: ReactNode[] = [];\n      for (let a = 0; a < 4; a++) for (let b = 0; b < 4; b++) dots.push(<circle key={a * 4 + b} cx={cx + 6 + a * 12.5} cy={cy + 6 + b * 12.5} r={2.6} />);\n      shapes.push(\n        <g key={i} fill={fill}>\n          {dots}\n        </g>,\n      );\n    }\n  }\n  return (\n    <svg aria-hidden viewBox=\"0 0 100 100\" preserveAspectRatio=\"xMidYMid slice\" className={\"block h-full w-full \" + className} style={{ background: ground }}>\n      {shapes}\n    </svg>\n  );\n}\n\n/**\n * One module of a run: a cell (what reflows move) holding a plate the rule\n * may break. Broken, the plate grows two gutters out at its leading edge (one\n * closes the gap, one overlaps the neighbour) while its padding grows by the\n * same, so its content never moves and never reaches the overlap; it rides\n * one layer up, wears the bracket and gets a firmer leading edge, so the\n * overlap reads as two plates, not one slab. The module before a breaker\n * `yields`: its trailing padding grows by a gutter, so nothing it holds sits\n * under the overlap. The hand-off (breaking, or letting go) takes 240ms.\n * Depth is the overlap and a 1px hairline, no shadow. Put runs in a `.bg-grid`\n * (or any grid whose column gap is --bg-g).\n */\nexport function BgPlate({\n  flip,\n  index,\n  breaks,\n  yields = false,\n  ground = FIELD.paper,\n  color = FIELD.ink,\n  line = FIELD.lineOnPaper,\n  edge,\n  pad = 24,\n  lift = 0,\n  itemRole,\n  className = \"\",\n  plateClassName = \"\",\n  style,\n  plateStyle,\n  children,\n}: {\n  /** A stable id, for reflows. */\n  flip?: string;\n  /** The module's number in its run, from 1 (shown by the inspector). */\n  index: number;\n  breaks: boolean;\n  /** The next module breaks: keep a gutter of trailing padding clear of its overlap. */\n  yields?: boolean;\n  ground?: string;\n  color?: string;\n  line?: string;\n  /** The breaker's leading edge; defaults to half-strength text colour. */\n  edge?: string;\n  /** Inner padding in px; the overshoot is added to it at the leading edge. */\n  pad?: number;\n  /** Extra layers up. */\n  lift?: number;\n  /** A role for the cell, such as listitem inside a list. */\n  itemRole?: string;\n  className?: string;\n  plateClassName?: string;\n  style?: CSSProperties;\n  plateStyle?: CSSProperties;\n  children?: ReactNode;\n}) {\n  const reduced = useReducedMotion();\n  const hand = `${BG_HANDOFF_MS}ms ${BG_EASE}`;\n  const rim = edge ?? (color === FIELD.paper ? FIELD.edgeOnField : FIELD.edgeOnPaper);\n  return (\n    <div data-bg-flip={flip} role={itemRole} className={\"relative \" + className} style={{ zIndex: (breaks ? 2 : 1) + lift, ...style }}>\n      <div\n        data-bg-module=\"\"\n        data-bg-index={index}\n        data-bg-break={breaks ? \"true\" : \"false\"}\n        data-bg-yield={yields ? \"true\" : undefined}\n        className={\"relative h-full \" + plateClassName}\n        style={{\n          background: ground,\n          color,\n          boxShadow: breaks ? `inset 1px 0 0 ${rim}, inset 0 0 0 1px ${line}` : `inset 0 0 0 1px ${line}`,\n          marginInlineStart: breaks ? \"calc(var(--bg-g, 24px) * -2)\" : \"0px\",\n          paddingInlineStart: breaks ? `calc(${pad}px + var(--bg-g, 24px) * 2)` : `${pad}px`,\n          paddingInlineEnd: yields ? `calc(${pad}px + var(--bg-g, 24px))` : `${pad}px`,\n          transition: reduced ? \"none\" : `margin-inline-start ${hand}, padding-inline-start ${hand}, padding-inline-end ${hand}, background-color ${hand}, box-shadow ${hand}`,\n          ...plateStyle,\n        }}\n      >\n        <BgBracket on={breaks} className=\"bg-own\" />\n        {children}\n      </div>\n    </div>\n  );\n}\n\nexport type BgWork = { title: string; client: string; year: string; tags: string[]; seed: number; href: string; image?: ReactNode };\n\n// Only the card just before a breaker (the one it overlaps) trades places with it: hovering or focusing\n// that card lifts it above the breaker, and the bracket goes with the lift. Scoped to .bg-swap-run, so\n// other runs on the page are left alone.\nconst BG_SWAP = \".bg-swap-run>[data-bg-flip]:has(+[data-bg-flip]>[data-bg-break=true])\";\n\nconst CARD_CSS =\n  `${BG_SWAP}:is(:hover,:focus-within){z-index:6!important}` +\n  `${BG_SWAP}:is(:hover,:focus-within)+[data-bg-flip]>[data-bg-break=true]>.bg-own{opacity:0!important}` +\n  `${BG_SWAP}:is(:hover,:focus-within) .bg-lift{opacity:1!important}`;\n\n/**\n * ZSwapPlateCard — a work card on the lattice that swaps layers with the\n * neighbour it overlaps.\n *\n * In a run, every third card breaks by the rule: two gutters out at its\n * leading edge, one layer up, the bracket on its corner. The card it overlaps\n * keeps a gutter of padding clear of the overlap; hover or focus that card\n * and the two swap layers, the neighbour rising above the breaker and the\n * bracket going with it, in 240ms, then everything returns. The picture is\n * your `image` (an <img> or next/image, filling a 4:3 frame), or a\n * construction plate drawn in code from `seed`. A real link on the title.\n *\n * Put the cards, as direct children, in a grid with the class `bg-swap-run`\n * whose column gap is --bg-g (the kit's `.bg-grid` sets both; otherwise set\n * --bg-g to your gap), inside an @container.\n */\nexport function ZSwapPlateCard({\n  work,\n  index,\n  count,\n  every = 3,\n  className = \"\",\n}: {\n  work: BgWork;\n  /** The card's place in its run, from 0. */\n  index: number;\n  count: number;\n  every?: number;\n  className?: string;\n}) {\n  const breaks = bgBreaks(index, count, every);\n  return (\n    <BgPlate\n      flip={work.title}\n      index={index + 1}\n      breaks={breaks}\n      yields={bgBreaks(index + 1, count, every)}\n      pad={16}\n      className={\"bg-scope h-full \" + className}\n      style={{ [\"--bg-ring\" as string]: FIELD.ink } as CSSProperties}\n      plateClassName=\"flex flex-col gap-4 pb-5 pt-4\"\n    >\n      <style>{CARD_CSS + FOCUS_CSS}</style>\n      {/* The bracket this card wears only while it is lifted above a breaker. The ring inside the paper plate is ink; the brackets outside it take the section's mark. */}\n      <span aria-hidden className=\"bg-lift pointer-events-none absolute left-0 top-0 block h-6 w-6\" style={{ borderTop: `2px solid var(--bg-mark, ${FIELD.acid})`, borderLeft: `2px solid var(--bg-mark, ${FIELD.acid})`, transform: \"translate(-6px, -6px)\", opacity: 0, transition: `opacity ${BG_HANDOFF_MS}ms ${BG_EASE}` }} />\n      <div className=\"flex items-center justify-between text-[11px] uppercase\" style={{ fontFamily: MONO, letterSpacing: \"0.08em\", color: FIELD.ink3 }}>\n        <span>{String(index + 1).padStart(2, \"0\")}</span>\n        <span>{work.year}</span>\n      </div>\n      <div className=\"aspect-[4/3] w-full overflow-hidden [&>img]:h-full [&>img]:w-full [&>img]:object-cover\" style={{ boxShadow: `inset 0 0 0 1px ${FIELD.lineOnPaper}` }}>\n        {work.image ?? <BgConstruction seed={work.seed} accent={index % 2 ? FIELD.field : undefined} />}\n      </div>\n      <div className=\"flex flex-col gap-1\">\n        <a href={safeHref(work.href)} className=\"text-[clamp(26px,2.9cqw,40px)] leading-[0.9] no-underline\" style={{ fontFamily: DISPLAY, fontWeight: 800, textTransform: \"uppercase\", color: FIELD.ink }}>\n          {work.title}\n        </a>\n        <p className=\"m-0 text-[14px]\" style={{ color: FIELD.ink2 }}>\n          {work.client}\n        </p>\n      </div>\n      <ul className=\"m-0 flex list-none flex-wrap gap-x-3 gap-y-1 p-0 text-[11px] uppercase\" style={{ fontFamily: MONO, letterSpacing: \"0.08em\", color: FIELD.ink3 }}>\n        {work.tags.map((t) => (\n          <li key={t}>{t}</li>\n        ))}\n      </ul>\n    </BgPlate>\n  );\n}\n","type":"registry:component","target":"components/viberdy/z-swap-plate-card.tsx"}],"meta":{"tier":"free","tags":["card","portfolio","grid","overlap","hover","broken grid","work"],"href":"https://viberdy.dev/library/z-swap-plate-card","aiPrompt":"Build a React + Tailwind card, `ZSwapPlateCard({ work, index, count, every, className })` where work = { title, client, year, tags, seed, href, image? }.\n\nBroken Grid grammar: a visible grid that holds, and one rule that breaks it the same way everywhere. A Klein-blue field #2416f0 as the ground (raised #3a2ff5, pressed #1a0ec4), paper plates #f1f0e8, blue-black ink #0a0a1f; secondary and tertiary text at 72/60% ink on paper and 86/76% paper on the field (all 4.5:1 or better), input boundaries at 50% ink (3:1). The mark means one thing, a rule firing or the current state (the bracket on a breaking module, an index tag, a selected chip): acid #e2ff3a on the field, ink on paper, where acid would vanish; never a wash, a gradient or text on paper. The lattice: twelve columns from a 48rem container (gutter 1.875%, 24px at 1280), four on a phone (8px gutters); the margin is two gutters and 8px (56px at 1280, 24px on a phone), so a breaker in the first column runs into the margin with its bracket still on the page; rows square with the columns; hairlines through the gutter centres (paper at 22% on the field), small crosses where they meet; drawn in full only at the anchors (hero, footer), elsewhere as column ticks. THE RULE, \"The Third\": in every run of modules, module 3, 6, 9... (never the first or the last) overshoots its leading edge by two gutters, one closing the gap and one overlapping its neighbour, rides one layer above it, wears the bracket (a 2px L one gutter long, 6px outside its leading corner, read against the ground) and gets a firmer leading edge (half-strength text colour) so the overlap reads as two plates; its padding grows by the same so its content never moves, and the module it overlaps keeps a gutter of trailing padding clear. Type follows the same law with one column pitch as the unit. The break belongs to the position: reorder a run and the break passes to whichever module is now third; compute it, never place it. Radius 0, no shadows, no grain; depth is overlap and a 1px hairline (inset box-shadow, never a border). Type: Big Shoulders at 800 in capitals through var(--font-big-shoulders, \"Big Shoulders\", ...) for display (up to 164px, leading 0.84-0.86), Geist Mono 11px uppercase +0.08em for labels (never smaller), Geist 16-17px/1.5 for reading. Motion: layouts change in 380ms on cubic-bezier(.2,.8,.2,1), all at once, no stagger, no spring, no overshoot (the layout overshoots, the motion never does); a role hand-off (breaking or letting go) takes 240ms; a drag follows 1:1 and snaps in 320ms; controls press in 120ms, release in 200ms, labels step in 160ms; tags step (steps(4)), never glide; scroll only ever changes a layout at checkpoints (never a continuous scrub); no ambient loops; reduced motion makes every change instant.\n\nThe card is a plate (a cell holding a paper plate with a 1px ink hairline, padding 16px): a mono row (number, year), a 4:3 picture (the image if given, else a construction plate of two or three primitives, a quarter circle, a bar, a cross or a dot field, on a 4 x 4 sub-lattice, from a seeded generator so server and browser agree), the title as a link in Big Shoulders 800 caps at clamp(26px, 2.9cqw, 40px), the client in Geist, and mono tags at 11px. It breaks when bgBreaks(index, count, every): margin-inline-start -2 gutters, padding-inline-start + 2 gutters (240ms transitions), z-index 2, the bracket, a firmer leading edge. The card before a breaker adds a gutter to its trailing padding. A small CSS block scoped to .bg-swap-run does the swap: the cell just before a breaking cell (`>[data-bg-flip]:has(+[data-bg-flip]>[data-bg-break=true])`), when hovered or focus-within, gets z-index 6, hides the breaker's bracket and shows its own (opacity 240ms).","license":"https://viberdy.dev/license"}}