{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hairline-tile-button","type":"registry:component","title":"Hairline Tile Button","description":"A button that behaves like a live bento grid: on hover it re-tiles into compartments along lit hairline seams, compresses toward your press, and its keycaps are a real keyboard shortcut.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/hairline-tile-button.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent, ReactNode } from \"react\";\n\n/**\n * HairlineTileButton — a button that behaves like a live bento grid.\n *\n * At rest it is one flat tile: a tonal face, a 1px hairline, no shadow.\n * Point at it (or tab to it) and the face re-tiles into compartments along\n * hairline seams, one after another, 60ms apart on the kit's soft spring: the\n * label tile yields width, the keycaps take a compartment of their own, and\n * an arrow tile grows in from the trailing edge. Nothing lifts; the grid\n * divides. The layout never moves, only the tile surfaces do, so the button\n * keeps its width and never nudges its neighbours.\n *\n * - LIGHT ON THE SEAMS. A light under the pointer catches the outer hairline,\n *   every compartment's hairline and the seams between them.\n * - PRESS. The compartments compress toward the point you press (a tonal step\n *   down, no lift) and spring back on release. Touch presses re-tile too.\n * - REAL KEYCAPS. Pass `shortcut` (\"shift+d\", \"mod+k\", \"/\") and the keycaps\n *   are live: pressing that combination clicks the button and visibly\n *   depresses each cap. The listener only acts while the button is on screen\n *   and no text field has focus, and a held modifier depresses its own cap.\n *\n * Two variants: \"tile\" (the quiet default) and \"accent\", the one filled\n * colour a bento screen allows. Optional mono metadata above the label and a\n * leading icon. A link when you pass href (sanitised), otherwise a button.\n * Under prefers-reduced-motion the re-tile and the press are instant and\n * nothing compresses.\n *\n * Radius 14 is concentric with the kit's 20px tiles at a 6px inset; the\n * compartments sit 4px inside it at radius 10. Mono type comes from\n * --font-mono (or the site's --font-geist-mono) with a system fallback.\n *\n * One button per shortcut on a screen. Needs Tailwind v4 (or v3.4+) for the\n * few utility classes; the state styles ship in the component's own <style>.\n * No dependencies beyond React.\n */\n\ntype Variant = \"tile\" | \"accent\";\ntype Size = \"md\" | \"lg\";\n\nconst MONO = \"var(--font-mono, var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace))\";\nconst SPRING = \"cubic-bezier(0.34,1.25,0.64,1)\";\nconst OUT = \"cubic-bezier(0.16,1,0.3,1)\";\nconst RING_MASK =\n  \"-webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);-webkit-mask-composite:xor;\" +\n  \"mask:linear-gradient(#000 0 0) content-box exclude,linear-gradient(#000 0 0);\";\n\n/** A state rule for \"open\": hover (only where hover exists), keyboard focus, or a press. */\nfunction openRule(sel: string, body: string): string {\n  return (\n    \".htb:focus-visible\" + sel + \",.htb[data-press]\" + sel + \"{\" + body + \"}\" +\n    \"@media (hover:hover){.htb:hover\" + sel + \"{\" + body + \"}}\"\n  );\n}\n\n// Static CSS: every colour arrives through custom properties set inline from\n// validated hex values, so nothing user-supplied is ever written in here.\nconst HTB_CSS =\n  \".htb{position:relative;isolation:isolate;display:inline-flex;align-items:stretch;overflow:hidden;border-radius:14px;\" +\n  \"background:var(--htb-grout);color:var(--htb-ink);text-align:left;text-decoration:none;user-select:none;-webkit-user-select:none;\" +\n  \"-webkit-tap-highlight-color:transparent;touch-action:manipulation;cursor:pointer;outline:none;\" +\n  \"transition:background-color 200ms \" + OUT + \"}\" +\n  \".htb:focus-visible{outline:2px solid var(--htb-ring);outline-offset:2px;border-radius:14px}\" +\n  \".htb:disabled{cursor:default;opacity:.4;pointer-events:none}\" +\n  \".htb-plate{position:relative;display:flex;flex:1 1 auto;min-width:0;transform-origin:var(--htb-px,50%) var(--htb-py,50%);\" +\n  \"transition:transform 320ms \" + SPRING + \"}\" +\n  \".htb[data-press] .htb-plate{transform:scale(.965);transition:transform 110ms \" + OUT + \"}\" +\n  \".htb-under{position:absolute;inset:0;pointer-events:none;opacity:0;\" +\n  \"background:radial-gradient(64px circle at var(--htb-x,-999px) var(--htb-y,-999px),var(--htb-seam),transparent 70%);\" +\n  \"transition:opacity 200ms \" + OUT + \"}\" +\n  \".htb-s{position:absolute;pointer-events:none;top:4px;bottom:4px;width:0;border-radius:10px;background:var(--htb-tile);\" +\n  \"box-shadow:inset 0 0 0 1px var(--htb-hair);\" +\n  \"transition:top 280ms \" + OUT + \",right 280ms \" + OUT + \",bottom 280ms \" + OUT + \",left 280ms \" + OUT + \",width 280ms \" + OUT +\n  \",border-radius 280ms \" + OUT + \",background-color 200ms \" + OUT + \",box-shadow 200ms \" + OUT + \"}\" +\n  \".htb-s-l{top:0;right:0;bottom:0;left:0;width:auto;border-radius:14px;background:var(--htb-face);box-shadow:inset 0 0 0 1px transparent;transition-delay:80ms}\" +\n  \".htb-s-k{right:calc(4px + var(--htb-ag));background:var(--htb-well);transition-delay:40ms}\" +\n  \".htb-s-a{right:4px;transition-delay:0ms}\" +\n  \".htb-s::before{content:\\\"\\\";position:absolute;inset:0;border-radius:inherit;background:#000;opacity:0;transition:opacity 110ms \" + OUT + \"}\" +\n  \".htb-s::after{content:\\\"\\\";position:absolute;inset:0;border-radius:inherit;padding:1px;opacity:0;\" +\n  \"background:radial-gradient(80px circle at var(--lx,-999px) var(--ly,-999px),var(--htb-glint),transparent 70%);\" +\n  RING_MASK + \"transition:opacity 200ms \" + OUT + \"}\" +\n  \".htb[data-press] .htb-s::before{opacity:var(--htb-veil)}\" +\n  \".htb-row{position:relative;display:flex;flex:1 1 auto;min-width:0;align-items:stretch;gap:4px;padding:4px}\" +\n  \".htb-label{display:flex;flex:1 1 auto;min-width:0;align-items:center;gap:12px;padding:0 var(--htb-pad)}\" +\n  \".htb-keys{display:flex;flex:none;align-items:center;gap:4px;padding:0 6px}\" +\n  \".htb-arrow{display:flex;flex:none;align-items:center;justify-content:center;width:var(--htb-aw)}\" +\n  \".htb-glyph{display:inline-flex;opacity:.6;transform:translateX(-3px);transition:transform 320ms \" + SPRING + \",opacity 200ms \" + OUT + \"}\" +\n  \".htb-cap{display:flex;height:24px;min-width:24px;align-items:center;justify-content:center;border-radius:6px;padding:0 6px;\" +\n  \"font-size:11px;line-height:1;color:var(--htb-cap-ink);background:var(--htb-cap);\" +\n  \"box-shadow:inset 0 -1px 0 rgba(0,0,0,.35),inset 0 0 0 1px var(--htb-cap-ring);\" +\n  \"transition:transform 90ms linear,box-shadow 90ms linear,color 200ms \" + OUT + \"}\" +\n  \".htb-cap[data-down]{transform:translateY(1px);color:var(--htb-cap-hi);box-shadow:inset 0 1px 2px rgba(0,0,0,.45),inset 0 0 0 1px var(--htb-cap-ring)}\" +\n  \".htb-edge,.htb-ring{position:absolute;inset:0;pointer-events:none;border-radius:14px}\" +\n  \".htb-edge{box-shadow:inset 0 0 0 1px var(--htb-line);transition:box-shadow 200ms \" + OUT + \"}\" +\n  \".htb-ring{padding:1px;opacity:0;\" +\n  \"background:radial-gradient(90px circle at var(--htb-x,-999px) var(--htb-y,-999px),var(--htb-glint-edge),transparent 70%);\" +\n  RING_MASK + \"transition:opacity 200ms \" + OUT + \"}\" +\n  openRule(\"\", \"background:var(--htb-grout-open)\") +\n  openRule(\" .htb-under\", \"opacity:1\") +\n  openRule(\n    \" .htb-s\",\n    \"transition-duration:320ms,320ms,320ms,320ms,320ms,320ms,200ms,200ms;\" +\n      \"transition-timing-function:\" + SPRING + \",\" + SPRING + \",\" + SPRING + \",\" + SPRING + \",\" + SPRING + \",\" + SPRING + \",\" + OUT + \",\" + OUT,\n  ) +\n  openRule(\n    \" .htb-s-l\",\n    \"top:4px;bottom:4px;left:4px;right:var(--htb-tail,48px);border-radius:10px;background:var(--htb-tile);\" +\n      \"box-shadow:inset 0 0 0 1px var(--htb-hair);transition-delay:0ms\",\n  ) +\n  openRule(\" .htb-s-k\", \"width:var(--htb-kw,0px);transition-delay:60ms\") +\n  openRule(\" .htb-s-a\", \"width:var(--htb-aw);transition-delay:120ms\") +\n  openRule(\" .htb-s::after\", \"opacity:1\") +\n  openRule(\" .htb-glyph\", \"opacity:1;transform:none;transition-delay:120ms,120ms\") +\n  openRule(\" .htb-cap\", \"color:var(--htb-cap-hi)\") +\n  openRule(\" .htb-edge\", \"box-shadow:inset 0 0 0 1px var(--htb-line-open)\") +\n  openRule(\" .htb-ring\", \"opacity:1\") +\n  \"@media (prefers-reduced-motion:reduce){.htb,.htb *,.htb *::before,.htb *::after{transition-duration:0ms!important;transition-delay:0ms!important}\" +\n  \".htb[data-press] .htb-plate{transform:none}}\";\n\n/** Allow relative paths, fragments, http(s), mailto and tel; anything else becomes \"#\". */\nfunction safeHref(href: string): string {\n  const v = href.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (v.indexOf(String.fromCharCode(92)) !== -1) return \"#\";\n  if (/^(https?:|mailto:|tel:)/i.test(v)) return v;\n  if (/^[/#?]/.test(v) && !/^\\/\\//.test(v)) return v;\n  return \"#\";\n}\n\n/** \"#rrggbb\" if valid, otherwise the fallback. Never interpolated unchecked. */\nfunction safeHex(hex: string | undefined, fallback: string): string {\n  return /^#[0-9a-f]{6}$/i.test(hex || \"\") ? (hex as string) : fallback;\n}\n\n/** Relative luminance, to pick dark or light text on the accent. */\nfunction isLight(hex: string): boolean {\n  const n = parseInt(hex.slice(1), 16);\n  const c = [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((v) => {\n    const s = v / 255;\n    return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);\n  });\n  return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2] > 0.18;\n}\n\n/** Mixes two \"#rrggbb\" colours: t = 0 gives a, t = 1 gives b. */\nfunction mix(a: string, b: string, t: number): string {\n  const x = parseInt(a.slice(1), 16);\n  const y = parseInt(b.slice(1), 16);\n  const ch = (s: number) => Math.round(((x >> s) & 255) * (1 - t) + ((y >> s) & 255) * t);\n  return \"#\" + ((1 << 24) | (ch(16) << 16) | (ch(8) << 8) | ch(0)).toString(16).slice(1);\n}\n\n// ---- Shortcuts ------------------------------------------------------------\n\ntype Mod = \"mod\" | \"ctrl\" | \"meta\" | \"alt\" | \"shift\";\ntype Combo = { key: string; mods: Mod[] };\n\nconst MOD_NAMES: Record<string, Mod> = {\n  mod: \"mod\",\n  ctrl: \"ctrl\",\n  control: \"ctrl\",\n  cmd: \"meta\",\n  command: \"meta\",\n  meta: \"meta\",\n  alt: \"alt\",\n  option: \"alt\",\n  opt: \"alt\",\n  shift: \"shift\",\n};\n/** Cap labels: [Mac, everywhere else]. */\nconst MOD_CAPS: Record<Mod, [string, string]> = {\n  mod: [\"⌘\", \"Ctrl\"],\n  ctrl: [\"⌃\", \"Ctrl\"],\n  meta: [\"⌘\", \"Meta\"],\n  alt: [\"⌥\", \"Alt\"],\n  shift: [\"⇧\", \"⇧\"],\n};\nconst MOD_ARIA: Record<Mod, [string, string]> = {\n  mod: [\"Meta\", \"Control\"],\n  ctrl: [\"Control\", \"Control\"],\n  meta: [\"Meta\", \"Meta\"],\n  alt: [\"Alt\", \"Alt\"],\n  shift: [\"Shift\", \"Shift\"],\n};\nconst KEY_CAPS: Record<string, string> = {\n  enter: \"↵\",\n  escape: \"Esc\",\n  space: \"Space\",\n  arrowup: \"↑\",\n  arrowdown: \"↓\",\n  arrowleft: \"←\",\n  arrowright: \"→\",\n  backspace: \"⌫\",\n  delete: \"Del\",\n  tab: \"Tab\",\n};\nconst KEY_ARIA: Record<string, string> = {\n  enter: \"Enter\",\n  escape: \"Escape\",\n  space: \"Space\",\n  arrowup: \"ArrowUp\",\n  arrowdown: \"ArrowDown\",\n  arrowleft: \"ArrowLeft\",\n  arrowright: \"ArrowRight\",\n  backspace: \"Backspace\",\n  delete: \"Delete\",\n  tab: \"Tab\",\n};\n\n/** \"shift+d\", \"mod+k\", \"alt+enter\", \"/\" -> a combo, or null if it cannot be read. */\nfunction parseShortcut(s: string | undefined): Combo | null {\n  if (!s) return null;\n  const parts = s.split(\"+\").map((p) => p.trim().toLowerCase());\n  const raw = parts.pop();\n  if (!raw) return null;\n  const key = raw === \"esc\" ? \"escape\" : raw === \"return\" ? \"enter\" : raw;\n  const mods: Mod[] = [];\n  for (const p of parts) {\n    const m = MOD_NAMES[p];\n    if (!m) return null;\n    if (mods.indexOf(m) === -1) mods.push(m);\n  }\n  return { key, mods };\n}\n\nfunction capLabel(key: string): string {\n  if (KEY_CAPS[key]) return KEY_CAPS[key];\n  return key.length === 1 ? key.toUpperCase() : key.charAt(0).toUpperCase() + key.slice(1);\n}\n\n/** Does this event carry the combo's key (modifiers aside)? Layout-proof for letters and digits. */\nfunction hitsKey(c: Combo, e: KeyboardEvent): boolean {\n  const k = e.key.toLowerCase();\n  const want = c.key === \"space\" ? \" \" : c.key;\n  if (k === want) return true;\n  if (/^[a-z]$/.test(want)) return e.code === \"Key\" + want.toUpperCase();\n  if (/^[0-9]$/.test(want)) return e.code === \"Digit\" + want;\n  return false;\n}\n\nfunction matches(c: Combo, e: KeyboardEvent, mac: boolean): boolean {\n  if (!hitsKey(c, e)) return false;\n  const has = (m: Mod) => c.mods.indexOf(m) !== -1;\n  const ctrl = has(\"ctrl\") || (has(\"mod\") && !mac);\n  const meta = has(\"meta\") || (has(\"mod\") && mac);\n  // Punctuation such as \"?\" needs Shift to be typed at all, so Shift is not held against it.\n  const shiftFree = c.key.length === 1 && !/[a-z0-9]/.test(c.key);\n  return e.ctrlKey === ctrl && e.metaKey === meta && e.altKey === has(\"alt\") && (shiftFree || e.shiftKey === has(\"shift\"));\n}\n\n/** True while focus is somewhere text can be typed. */\nfunction isTyping(t: EventTarget | null): boolean {\n  if (!(t instanceof HTMLElement)) return false;\n  if (t.isContentEditable || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement) return true;\n  return t instanceof HTMLInputElement && !/^(button|checkbox|color|file|hidden|image|radio|range|reset|submit)$/i.test(t.type);\n}\n\nconst noSubscribe = () => () => {};\n/** Mac or not, read after hydration so the server and first client render agree. */\nfunction useIsMac(): boolean {\n  return useSyncExternalStore(\n    noSubscribe,\n    () => /Mac|iPhone|iPad|iPod/.test(navigator.userAgent),\n    () => false,\n  );\n}\n\n// ---- The button ------------------------------------------------------------\n\nexport function HairlineTileButton({\n  children,\n  href,\n  onClick,\n  type = \"button\",\n  variant = \"tile\",\n  size = \"md\",\n  meta,\n  kbd,\n  shortcut,\n  icon,\n  arrow = true,\n  accent = \"#ff5a1f\",\n  disabled = false,\n  className = \"\",\n}: {\n  children: ReactNode;\n  /** Renders a link instead of a button. Sanitised. */\n  href?: string;\n  onClick?: () => void;\n  type?: \"button\" | \"submit\";\n  variant?: Variant;\n  size?: Size;\n  /** Mono metadata above the label, e.g. \"v4.2\" or \"Status\". */\n  meta?: string;\n  /** Keycap labels. With `shortcut` they only relabel its caps (same count); alone they are a static hint. */\n  kbd?: string[];\n  /** A live keyboard shortcut that clicks the button: \"shift+d\", \"mod+k\" (Cmd on Mac, Ctrl elsewhere), \"alt+enter\", \"/\". */\n  shortcut?: string;\n  icon?: ReactNode;\n  /** The trailing tile: true for an arrow, false for none, or your own glyph. */\n  arrow?: ReactNode;\n  /** The one accent colour, \"#rrggbb\". */\n  accent?: string;\n  disabled?: boolean;\n  className?: string;\n}) {\n  const a = safeHex(accent, \"#ff5a1f\");\n  const filled = variant === \"accent\";\n  const lightInk = filled && isLight(a);\n  const ink = filled ? (lightInk ? \"#0b0b0c\" : \"#ffffff\") : \"#f4f4f5\";\n  const mac = useIsMac();\n  const combo = parseShortcut(shortcut);\n  const aw = size === \"lg\" ? 48 : 36;\n  const glyph = arrow === true ? \"→\" : arrow === false || arrow === null || arrow === undefined ? null : arrow;\n\n  // The caps: the shortcut's own, relabelled by kbd when the counts agree, or a static kbd hint.\n  const caps: { id: string; label: string }[] = combo\n    ? combo.mods.map((m) => ({ id: m as string, label: MOD_CAPS[m][mac ? 0 : 1] })).concat({ id: \"key\", label: capLabel(combo.key) })\n    : (kbd ?? []).map((k) => ({ id: \"\", label: k }));\n  const labels = combo && kbd && kbd.length === caps.length ? kbd : null;\n  const ariaKeys = combo\n    ? combo.mods.map((m) => MOD_ARIA[m][mac ? 0 : 1]).concat(KEY_ARIA[combo.key] ?? (combo.key.length === 1 ? combo.key.toUpperCase() : combo.key)).join(\"+\")\n    : undefined;\n\n  const aRef = useRef<HTMLAnchorElement>(null);\n  const bRef = useRef<HTMLButtonElement>(null);\n  const light = useRef({ x: 0, y: 0, raf: 0 });\n  const asLink = !!href && !disabled;\n\n  // The label tile's open width is \"everything but the tail\": measure the tail\n  // (keycap and arrow compartments) whenever anything in the row resizes.\n  const hasKeys = caps.length > 0;\n  const hasGlyph = glyph !== null;\n  useEffect(() => {\n    const el = aRef.current ?? bRef.current;\n    const row = el?.querySelector<HTMLElement>(\".htb-row\");\n    const label = el?.querySelector<HTMLElement>(\".htb-label\");\n    if (!el || !row || !label) return;\n    const keys = el.querySelector<HTMLElement>(\".htb-keys\");\n    const measure = () => {\n      el.style.setProperty(\"--htb-tail\", Math.max(0, row.offsetWidth - label.offsetLeft - label.offsetWidth) + \"px\");\n      el.style.setProperty(\"--htb-kw\", (keys ? keys.offsetWidth : 0) + \"px\");\n    };\n    const ro = new ResizeObserver(measure);\n    ro.observe(row);\n    ro.observe(label);\n    if (keys) ro.observe(keys);\n    return () => ro.disconnect();\n  }, [asLink, hasKeys, hasGlyph]);\n\n  useEffect(() => {\n    const l = light.current;\n    return () => cancelAnimationFrame(l.raf);\n  }, []);\n\n  // The live shortcut: acts only while the button is on screen and nobody is typing.\n  const shortcutKey = shortcut ?? \"\";\n  useEffect(() => {\n    const c = parseShortcut(shortcutKey);\n    const el = aRef.current ?? bRef.current;\n    if (!c || !el || disabled) return;\n    let seen = false;\n    const io = new IntersectionObserver((es) => {\n      seen = es[es.length - 1].isIntersecting;\n    });\n    io.observe(el);\n    let held = false;\n    let since = 0;\n    let timer = 0;\n    const capEls = () => Array.from(el.querySelectorAll<HTMLElement>(\".htb-cap[data-cap]\"));\n    const paintMods = (e: KeyboardEvent | null) => {\n      capEls().forEach((cap) => {\n        const id = cap.getAttribute(\"data-cap\");\n        if (id === \"key\") return;\n        const down =\n          !!e &&\n          (id === \"shift\" ? e.shiftKey : id === \"alt\" ? e.altKey : id === \"ctrl\" ? e.ctrlKey : id === \"meta\" ? e.metaKey : id === \"mod\" ? (mac ? e.metaKey : e.ctrlKey) : false);\n        cap.toggleAttribute(\"data-down\", down);\n      });\n    };\n    const keyCap = (down: boolean) => capEls().forEach((cap) => cap.getAttribute(\"data-cap\") === \"key\" && cap.toggleAttribute(\"data-down\", down));\n    const release = () => {\n      held = false;\n      keyCap(false);\n      el.removeAttribute(\"data-press\");\n    };\n    const onDown = (e: KeyboardEvent) => {\n      if (!seen || e.isComposing || isTyping(e.target) || isTyping(document.activeElement)) return;\n      paintMods(e);\n      if (!matches(c, e, mac) || e.defaultPrevented) return;\n      e.preventDefault();\n      if (e.repeat) return;\n      window.clearTimeout(timer);\n      held = true;\n      since = e.timeStamp;\n      keyCap(true);\n      // The face compresses toward the keycap tile, then the button fires.\n      const keys = el.querySelector<HTMLElement>(\".htb-keys\");\n      if (keys) {\n        el.style.setProperty(\"--htb-px\", (keys.offsetLeft + keys.offsetWidth / 2).toFixed(1) + \"px\");\n        el.style.setProperty(\"--htb-py\", \"50%\");\n      }\n      el.setAttribute(\"data-press\", \"\");\n      el.click();\n    };\n    const onUp = (e: KeyboardEvent) => {\n      paintMods(e);\n      if (held && hitsKey(c, e)) {\n        held = false;\n        // Hold the press long enough to be seen, even for a quick tap.\n        timer = window.setTimeout(release, Math.max(0, 140 - (e.timeStamp - since)));\n      }\n    };\n    const clear = () => {\n      window.clearTimeout(timer);\n      release();\n      paintMods(null);\n    };\n    window.addEventListener(\"keydown\", onDown);\n    window.addEventListener(\"keyup\", onUp);\n    window.addEventListener(\"blur\", clear);\n    return () => {\n      io.disconnect();\n      window.removeEventListener(\"keydown\", onDown);\n      window.removeEventListener(\"keyup\", onUp);\n      window.removeEventListener(\"blur\", clear);\n      clear();\n    };\n  }, [shortcutKey, disabled, mac, asLink]);\n\n  /** The pointer in the button's own layout pixels, even inside a transform-scaled container. */\n  function point(e: ReactPointerEvent<HTMLElement>) {\n    const t = e.currentTarget;\n    const r = t.getBoundingClientRect();\n    const k = t.offsetWidth / (r.width || 1);\n    return { x: (e.clientX - r.left) * k, y: (e.clientY - r.top) * k };\n  }\n\n  // One light, written at most once per frame: the outer hairline, the seams\n  // under the compartments, and each compartment's own hairline.\n  function paintLight(t: HTMLElement) {\n    const { x, y } = light.current;\n    t.style.setProperty(\"--htb-x\", x.toFixed(1) + \"px\");\n    t.style.setProperty(\"--htb-y\", y.toFixed(1) + \"px\");\n    t.querySelectorAll<HTMLElement>(\".htb-s\").forEach((s) => {\n      s.style.setProperty(\"--lx\", (x - s.offsetLeft).toFixed(1) + \"px\");\n      s.style.setProperty(\"--ly\", (y - s.offsetTop).toFixed(1) + \"px\");\n    });\n  }\n  function onMove(e: ReactPointerEvent<HTMLElement>) {\n    const p = point(e);\n    light.current.x = p.x;\n    light.current.y = p.y;\n    if (light.current.raf) return;\n    const t = e.currentTarget;\n    light.current.raf = requestAnimationFrame(() => {\n      light.current.raf = 0;\n      paintLight(t);\n    });\n  }\n  function onDown(e: ReactPointerEvent<HTMLElement>) {\n    if (e.pointerType === \"mouse\" && e.button !== 0) return;\n    const t = e.currentTarget;\n    const p = point(e);\n    light.current.x = p.x;\n    light.current.y = p.y;\n    paintLight(t);\n    t.style.setProperty(\"--htb-px\", p.x.toFixed(1) + \"px\");\n    t.style.setProperty(\"--htb-py\", p.y.toFixed(1) + \"px\");\n    t.setAttribute(\"data-press\", \"\");\n  }\n  function onUp(e: ReactPointerEvent<HTMLElement>) {\n    e.currentTarget.removeAttribute(\"data-press\");\n  }\n  function onLeave(e: ReactPointerEvent<HTMLElement>) {\n    cancelAnimationFrame(light.current.raf);\n    light.current.raf = 0;\n    e.currentTarget.removeAttribute(\"data-press\");\n  }\n  function onFocus(e: { currentTarget: HTMLElement }) {\n    // A keyboard arrival has no pointer: park the light off the button.\n    const t = e.currentTarget;\n    if (!t.matches(\":focus-visible\")) return;\n    t.style.removeProperty(\"--htb-x\");\n    t.style.removeProperty(\"--htb-y\");\n    t.querySelectorAll<HTMLElement>(\".htb-s\").forEach((s) => {\n      s.style.removeProperty(\"--lx\");\n      s.style.removeProperty(\"--ly\");\n    });\n  }\n  function onBlur(e: { currentTarget: HTMLElement }) {\n    e.currentTarget.removeAttribute(\"data-press\");\n  }\n  function onKeyDown(e: ReactKeyboardEvent<HTMLElement>) {\n    if (e.repeat) return;\n    const t = e.currentTarget;\n    if (e.key === \"Enter\" || (e.key === \" \" && t.tagName === \"BUTTON\")) {\n      t.style.removeProperty(\"--htb-px\");\n      t.style.removeProperty(\"--htb-py\");\n      t.setAttribute(\"data-press\", \"\");\n    }\n  }\n  function onKeyUp(e: ReactKeyboardEvent<HTMLElement>) {\n    if (e.key === \"Enter\" || e.key === \" \") e.currentTarget.removeAttribute(\"data-press\");\n  }\n\n  const style = {\n    color: ink,\n    minHeight: size === \"lg\" ? 56 : 44,\n    \"--htb-ink\": ink,\n    \"--htb-face\": filled ? a : \"#0f1012\",\n    \"--htb-grout\": filled ? a : \"#0f1012\",\n    \"--htb-grout-open\": filled ? mix(a, \"#000000\", 0.34) : \"#08090a\",\n    \"--htb-tile\": filled ? a : \"#16171a\",\n    \"--htb-well\": filled ? mix(a, \"#000000\", 0.14) : \"#131417\",\n    \"--htb-hair\": filled ? \"rgba(255,255,255,0.3)\" : \"rgba(255,255,255,0.1)\",\n    \"--htb-line\": filled ? \"rgba(255,255,255,0.22)\" : \"rgba(255,255,255,0.08)\",\n    \"--htb-line-open\": filled ? \"rgba(255,255,255,0.4)\" : \"rgba(255,255,255,0.16)\",\n    \"--htb-glint\": filled ? \"rgba(255,255,255,0.8)\" : \"rgba(255,255,255,0.5)\",\n    \"--htb-glint-edge\": filled ? \"rgba(255,255,255,0.9)\" : \"rgba(255,255,255,0.55)\",\n    \"--htb-seam\": filled ? \"rgba(255,255,255,0.45)\" : \"rgba(255,255,255,0.24)\",\n    \"--htb-veil\": filled ? 0.14 : 0.32,\n    \"--htb-ring\": filled ? \"#ffffff\" : a,\n    \"--htb-pad\": size === \"lg\" ? \"16px\" : \"12px\",\n    \"--htb-aw\": aw + \"px\",\n    \"--htb-ag\": glyph !== null ? aw + 4 + \"px\" : \"0px\",\n    \"--htb-cap\": filled ? \"rgba(0,0,0,0.14)\" : \"#16171a\",\n    \"--htb-cap-ring\": filled ? \"rgba(255,255,255,0.18)\" : \"rgba(255,255,255,0.1)\",\n    \"--htb-cap-ink\": filled ? ink : \"rgba(255,255,255,0.6)\",\n    \"--htb-cap-hi\": filled ? ink : \"#ffffff\",\n  } as CSSProperties;\n\n  const cls = \"htb \" + (size === \"lg\" ? \"text-[15px] \" : \"text-[14px] \") + className;\n\n  const inner = (\n    <>\n      <span className=\"htb-plate\">\n        {/* Under the compartments: the light that shows through the seams. */}\n        <span aria-hidden className=\"htb-under\" />\n        {/* The tile surfaces. At rest the label tile is the whole face. */}\n        <span aria-hidden className=\"htb-s htb-s-l\" />\n        {caps.length > 0 && <span aria-hidden className=\"htb-s htb-s-k\" />}\n        {glyph !== null && <span aria-hidden className=\"htb-s htb-s-a\" />}\n        <span className=\"htb-row\">\n          <span className=\"htb-label\">\n            {icon && (\n              <span aria-hidden className=\"relative flex h-5 w-5 shrink-0 items-center justify-center [&>svg]:h-[18px] [&>svg]:w-[18px]\">\n                {icon}\n              </span>\n            )}\n            <span className=\"flex min-w-0 flex-col\">\n              {meta && (\n                <span className=\"text-[11px] uppercase leading-[14px] tracking-[0.12em]\" style={{ fontFamily: MONO, opacity: filled ? 0.7 : 0.5 }}>\n                  {meta}\n                </span>\n              )}\n              <span className=\"truncate font-medium tracking-[-0.01em]\">{children}</span>\n            </span>\n          </span>\n          {caps.length > 0 && (\n            <span aria-hidden className=\"htb-keys\">\n              {caps.map((c, i) => (\n                <kbd key={i} className=\"htb-cap\" data-cap={c.id || undefined} style={{ fontFamily: MONO }}>\n                  {labels ? labels[i] : c.label}\n                </kbd>\n              ))}\n            </span>\n          )}\n          {glyph !== null && (\n            <span aria-hidden className=\"htb-arrow\">\n              <span className=\"htb-glyph\">{glyph}</span>\n            </span>\n          )}\n        </span>\n      </span>\n      <span aria-hidden className=\"htb-edge\" />\n      <span aria-hidden className=\"htb-ring\" />\n    </>\n  );\n\n  return (\n    <>\n      <style>{HTB_CSS}</style>\n      {asLink ? (\n        <a\n          ref={aRef}\n          href={safeHref(href as string)}\n          onClick={onClick}\n          aria-keyshortcuts={ariaKeys}\n          className={cls}\n          style={style}\n          onPointerMove={onMove}\n          onPointerDown={onDown}\n          onPointerUp={onUp}\n          onPointerCancel={onUp}\n          onPointerLeave={onLeave}\n          onFocus={onFocus}\n          onBlur={onBlur}\n          onKeyDown={onKeyDown}\n          onKeyUp={onKeyUp}\n        >\n          {inner}\n        </a>\n      ) : (\n        <button\n          ref={bRef}\n          type={type}\n          onClick={onClick}\n          disabled={disabled}\n          aria-keyshortcuts={ariaKeys}\n          className={cls}\n          style={style}\n          onPointerMove={onMove}\n          onPointerDown={onDown}\n          onPointerUp={onUp}\n          onPointerCancel={onUp}\n          onPointerLeave={onLeave}\n          onFocus={onFocus}\n          onBlur={onBlur}\n          onKeyDown={onKeyDown}\n          onKeyUp={onKeyUp}\n        >\n          {inner}\n        </button>\n      )}\n    </>\n  );\n}\n","type":"registry:component","target":"components/viberdy/hairline-tile-button.tsx"}],"meta":{"tier":"free","tags":["button","bento","hairline","keyboard shortcut","flip","dark","tile"],"href":"https://viberdy.dev/library/hairline-tile-button","aiPrompt":"Create a React + Tailwind client component, HairlineTileButton: a dark bento-style button that behaves like a live bento grid. At rest it is one flat tile (a tonal face, a 1px hairline, no shadow). On hover or keyboard focus its face re-tiles into compartments along hairline seams, nothing lifts, a light under the pointer runs along the seams, a press compresses the compartments toward the pressed point, and its keycaps are a real keyboard shortcut.\n\nProps: children, href (renders a sanitised link: strip tabs and newlines, reject backslashes, allow http(s), mailto, tel and relative paths starting / # ? but not //), onClick, type (\"button\" | \"submit\"), variant (\"tile\" | \"accent\"), size (\"md\": 44px min height, 14px text, 12px label padding, 36px arrow tile | \"lg\": 56px, 15px, 16px, 48px), meta (mono metadata above the label), kbd (keycap labels: with shortcut they only relabel its caps when the count matches, alone they are a static hint), shortcut (a live combination: \"shift+d\", \"mod+k\" where mod is Cmd on Mac and Ctrl elsewhere, \"alt+enter\", \"/\"), icon (leading, 18px), arrow (ReactNode: true for \"→\", false for none, or a custom trailing glyph; default true), accent (\"#rrggbb\", regex-validated, default #ff5a1f), disabled, className.\n\nAnatomy (keep the layout fixed so the button never changes width): the root (a or button, class \"htb\") is inline-flex, overflow hidden, radius 14, background var(--grout). Inside, a \"plate\" (relative flex, the part that compresses) holds: an under-light layer, then absolutely positioned tile surfaces, then the in-flow row (relative, flex, 4px padding, 4px gaps) with three cells: the label cell (flex 1, icon + a column of meta and a truncated 500-weight label), the keys cell (the keycaps, 6px side padding) and the arrow cell (a square as wide as the size's arrow tile, glyph centred). After the plate: an outer hairline layer (inset 1px ring) and an outer ring-light layer.\n\nSurfaces: the label surface at rest covers the whole face (inset 0, radius 14, the face colour, no hairline). Open, it insets to top/left/bottom 4px and right = the measured tail (row width minus the label cell's right edge; write it to --htb-tail from a ResizeObserver on the row, label and keys cells, and the keys cell width to --htb-kw), radius 10, the raised tile colour, an inset 1px hairline. The keys surface sits at right = 4px + arrow width + 4px, top/bottom 4px, width 0 at rest and --htb-kw open, a recessed well colour. The arrow surface sits at right 4px, width 0 at rest and the arrow width open. So the label tile yields width and the other tiles grow in from the trailing edge. Stagger opening 0 / 60 / 120ms (label, keys, arrow) on the spring cubic-bezier(0.34,1.25,0.64,1) over 320ms; closing reverses it (arrow 0, keys 40, label 80ms) on cubic-bezier(0.16,1,0.3,1) over 280ms. The root's background steps to a darker grout while open, so the seams read as channels between tiles. The arrow glyph goes from 60% opacity and -3px to full and 0, with the arrow tile's delay.\n\nThe open state is a static stylesheet rendered in a <style> tag beside the element: .htb:focus-visible, .htb[data-press] and, inside @media (hover:hover), .htb:hover. Build the CSS with a helper that writes one rule for all three. Colours only arrive through custom properties set inline from validated hex values. Tile variant: face and rest grout #0f1012, open grout #08090a, tiles #16171a, keys well #131417, compartment hairline white 0.1, outer hairline white 0.08 (0.16 open). Accent variant: face and tiles are the accent, the open grout is the accent mixed 34% toward black, the well 14% toward black, hairlines white 0.3 / 0.22 / 0.4, and the text is #0b0b0c or white by the accent's relative luminance (above 0.18 means dark text). Focus ring: .htb:focus-visible { outline: 2px solid (the accent, or white on the accent variant); outline-offset: 2px; border-radius: 14px }, so an unlayered global :focus-visible rule in the host cannot replace it.\n\nLight: on pointermove, coalesced to one write per animation frame, map the pointer into the button's layout pixels (rect-to-offsetWidth ratio, so it works in transform-scaled containers) and write --htb-x/--htb-y on the root and --lx/--ly on every surface (pointer minus the surface's offsetLeft/offsetTop). Three layers use it, all fading in over 200ms while open: the outer ring (a 1px ring made with padding 1px plus the mask-composite exclude trick, radial 90px of white 0.55, 0.9 on accent); each surface's own ring (an ::after, radial 80px); and the under-light (radial 64px, white 0.24, 0.45 on accent) that shows only through the seams. Cancel a pending frame on pointerleave. On keyboard focus (matches(\":focus-visible\")) remove the position variables so no stale light shows.\n\nPress: on pointerdown (primary button, or any touch or pen), write the point as --htb-px/--htb-py and set data-press; remove it on pointerup, pointercancel, pointerleave and blur. Enter (and Space on a button) sets it with the origin at the centre. While pressed the plate scales to 0.965 about that point in 110ms (the compartments compress toward the finger; nothing lifts) and every surface gets a black veil (::before at opacity 0.32, or 0.14 on accent) as a tonal step; release springs back over 320ms. A touch press also opens the tiles.\n\nReal keycaps: parse the shortcut into modifiers (mod, ctrl, meta, alt, shift, with common aliases) and a key. Caps show ⌘/Ctrl for mod, ⌃/Ctrl, ⌘/Meta, ⌥/Alt and ⇧, with Mac read through useSyncExternalStore (server snapshot false) so hydration never mismatches; keys map Enter to ↵, Escape to Esc, arrows to arrow glyphs, letters to capitals. Set aria-keyshortcuts (\"Shift+D\", \"Meta+K\"). In an effect, add window keydown/keyup/blur listeners and an IntersectionObserver: act only while the button is on screen and neither the event target nor document.activeElement is a text field (text inputs, textarea, select, contenteditable). Match the key by e.key or, for letters and digits, e.code (layout-proof), and require the modifiers exactly (Shift is not held against punctuation such as \"?\"). On a match, preventDefault, and unless it is a repeat: depress the key cap (data-down), set data-press with the origin at the keys cell's centre, and call element.click(). On keyup of the key, release after at least 140ms so a quick tap is still seen. Held modifiers depress their own caps on every key event; blur clears everything. Caps: 24px tall, radius 6, mono 11px, inset 0 -1px 0 black 0.35 plus a 1px ring; down they move 1px, lose the bottom edge and brighten, 90ms linear.\n\nReduced motion: every transition and delay goes to 0ms and the plate never scales, so the re-tile and the press are instant. No drop shadows anywhere. Mono stack: var(--font-mono, var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace)).\n\nOutput one self-contained \"use client\" TSX file with no dependencies beyond React. Build strings with + rather than template literals.","license":"https://viberdy.dev/license"}}