{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "desk-top",
  "title": "DeskTop",
  "description": "A cream desk seen from above, with light, a tilt-revealed front edge, and an optional pulled-out drawer.",
  "registryDependencies": [
    "@jbm/tokens",
    "@jbm/scene-geometry"
  ],
  "files": [
    {
      "path": "registry/jbm/ui/desk-top.tsx",
      "content": "import { color, stroke } from \"../lib/tokens\"\nimport { unit, type Box, type Pt } from \"../lib/geometry\"\n\nexport type DeskTopDrawerSide = \"start\" | \"end\" | \"top\" | \"bottom\"\nexport type DeskTopProps = {\n  /** Desk footprint in parent SVG units, front edge band and drawer region included. */\n  box: Box\n  /**\n   * Light on the whole desk, 0–1 (1 = full cream). Fills mix toward the palette's line token,\n   * reaching it at 0.72, then toward ink. Clamped.\n   */\n  light?: number\n  /** Camera tilt, 0–1: reveals the front edge band along the bottom (0 = straight down). Clamped. */\n  edge?: number\n  /** Edge that holds an open drawer, its front and pull on that edge; omit for no drawer. */\n  drawer?: DeskTopDrawerSide\n  /**\n   * Drawer region depth across its edge, in parent units: along the width for start/end, along\n   * the height for top/bottom. Defaults to 40% of that dimension.\n   */\n  drawerSize?: number\n}\n\n/** Front band height at full tilt, in parent units. */\nexport const DESK_EDGE = 22\nconst SEAM = 6\n/** The drawer's side and back walls; its front is thicker and carries the pull. */\nconst WALL = 10\nconst FRONT = { min: 16, max: 34 }\n/** The deepest light the visual language uses (the back folder): the desk is line-grey there. */\nconst DEEP = 0.72\n\ntype Lab = [number, number, number]\nconst srgbToLinear = (c: number) =>\n  c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4)\nfunction toLab(hex: string): Lab {\n  const n = parseInt(hex.slice(1), 16)\n  const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((c) =>\n    srgbToLinear(c / 255)\n  )\n  const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)\n  const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)\n  const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)\n  return [\n    0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,\n    1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,\n    0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,\n  ]\n}\nfunction toHex([L, A, B]: Lab): string {\n  const l2 = (L + 0.3963377774 * A + 0.2158037573 * B) ** 3\n  const m2 = (L - 0.1055613458 * A - 0.0638541728 * B) ** 3\n  const s2 = (L - 0.0894841775 * A - 1.291485548 * B) ** 3\n  const out = [\n    4.0767416621 * l2 - 3.3077115913 * m2 + 0.2309699292 * s2,\n    -1.2684380046 * l2 + 2.6097574011 * m2 - 0.3413193965 * s2,\n    -0.0041960863 * l2 - 0.7034186147 * m2 + 1.707614701 * s2,\n  ].map((c) => {\n    const v = c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055\n    return Math.round(unit(v) * 255)\n      .toString(16)\n      .padStart(2, \"0\")\n  })\n  return `#${out.join(\"\")}`\n}\nconst mix = (a: Lab, b: Lab, t: number): Lab =>\n  [0, 1, 2].map((i) => a[i] + (b[i] - a[i]) * t) as Lab\n\n/** Scale a #rrggbb colour's OKLab lightness by k, keeping its chroma. Clamped. */\nexport function deskShade(hex: string, k: number): string {\n  const [L, A, B] = toLab(hex)\n  return toHex([L * unit(k), A, B])\n}\n\n/**\n * A token colour under light k, on the palette: mixed in OKLab from the colour toward the line\n * token as k falls from 1 to 0.72 (the visual language's deepest folder), then from line toward\n * ink. 1 returns the colour; lower is always darker.\n */\nexport function deskLight(hex: string, k: number): string {\n  const c = unit(k)\n  const line = toLab(color.line)\n  if (c >= DEEP) return toHex(mix(toLab(hex), line, (1 - c) / (1 - DEEP)))\n  return toHex(mix(line, toLab(color.ink), (DEEP - c) / DEEP))\n}\n\n/**\n/**\n * Top-down desk geometry. The box never changes: tilt takes the front band out of the surface's\n * depth, and a drawer takes its side of the box, pulled out past the desk's edge (the seam).\n */\nexport function deskTopLayout({\n  box,\n  light = 1,\n  edge = 0,\n  drawer,\n  drawerSize,\n}: DeskTopProps) {\n  const band = DESK_EDGE * unit(edge)\n  // The plan view (surface plus drawer region) sits above the front band.\n  const plan = { x: box.x, y: box.y, w: box.w, h: box.h - band }\n  const across = drawer === \"top\" || drawer === \"bottom\" ? plan.h : plan.w\n  const depth = drawer\n    ? Math.max(\n        SEAM + WALL + FRONT.min + 12,\n        Math.min(across * 0.8, drawerSize ?? across * 0.4)\n      )\n    : 0\n  let surface: Box = plan\n  let region: Box | null = null\n  if (drawer === \"start\") {\n    region = { ...plan, w: depth - SEAM }\n    surface = { ...plan, x: plan.x + depth, w: plan.w - depth }\n  } else if (drawer === \"end\") {\n    region = { ...plan, x: plan.x + plan.w - depth + SEAM, w: depth - SEAM }\n    surface = { ...plan, w: plan.w - depth }\n  } else if (drawer === \"top\") {\n    region = { ...plan, h: depth - SEAM }\n    surface = { ...plan, y: plan.y + depth, h: plan.h - depth }\n  } else if (drawer === \"bottom\") {\n    region = { ...plan, y: plan.y + plan.h - depth + SEAM, h: depth - SEAM }\n    surface = { ...plan, h: plan.h - depth }\n  }\n  // Drawer parts in local units: u runs out from the seam's centre line to the region's outer\n  // edge, v along the edge. The drawer box is narrower than the desk, so it reads as pulled out.\n  const vertical = drawer === \"start\" || drawer === \"end\"\n  const U = depth - SEAM / 2\n  const V = vertical ? plan.h : plan.w\n  const seamAt = !drawer\n    ? 0\n    : drawer === \"end\"\n      ? surface.x + surface.w + SEAM / 2\n      : drawer === \"start\"\n        ? surface.x - SEAM / 2\n        : drawer === \"bottom\"\n          ? surface.y + surface.h + SEAM / 2\n          : surface.y - SEAM / 2\n  const out = drawer === \"start\" || drawer === \"top\" ? -1 : 1\n  const local = (u0: number, u1: number, v0: number, v1: number): Box => {\n    const a = seamAt + out * u0,\n      b = seamAt + out * u1\n    const lo = Math.min(a, b),\n      len = Math.abs(b - a)\n    return vertical\n      ? { x: lo, y: plan.y + v0, w: len, h: v1 - v0 }\n      : { x: plan.x + v0, y: lo, w: v1 - v0, h: len }\n  }\n  const inset = Math.min(V * 0.12, 48)\n  const thick = Math.min(FRONT.max, Math.max(FRONT.min, (depth - SEAM) * 0.16))\n  // The pull, as Cajon's: one flat ink bar at the shared outline, centred on the front wall. From\n  // above it shows its top face, a thin bar along the edge.\n  const t = thick * 0.28\n  const len = Math.min(thick * 1.46, (V - 2 * inset) * 0.45)\n  const parts = drawer\n    ? {\n        /** The drawer box as drawn, from the seam line to its front. */\n        drawn: local(0, U, inset, V - inset),\n        /** Its opening as drawn: open toward the seam, where it runs on under the desk. */\n        opening: local(0, U - thick, inset + WALL, V - inset - WALL),\n        body: local(SEAM / 2, U, inset, V - inset),\n        well: local(SEAM / 2, U - thick, inset + WALL, V - inset - WALL),\n        front: local(U - thick, U, inset, V - inset),\n        pull: local(U - thick / 2 - t / 2, U - thick / 2 + t / 2, V / 2 - len / 2, V / 2 + len / 2),\n      }\n    : null\n  // The desk slab: the surface out to the seam line.\n  const slab: Box = !drawer\n    ? plan\n    : vertical\n      ? drawer === \"end\"\n        ? { ...plan, w: seamAt - plan.x }\n        : { ...plan, x: seamAt, w: plan.x + plan.w - seamAt }\n      : drawer === \"bottom\"\n        ? { ...plan, h: seamAt - plan.y }\n        : { ...plan, y: seamAt, h: plan.y + plan.h - seamAt }\n  const k = unit(light)\n  return {\n    box,\n    /** The flat top that sheets and axes lie on. */\n    surface,\n    /** Its centre, where Ejes cross by default. */\n    center: {\n      x: surface.x + surface.w / 2,\n      y: surface.y + surface.h / 2,\n    } satisfies Pt,\n    /**\n     * The drawer: its region (panel), the pulled-out box inside it (body), the recessed opening\n     * (well), the thicker front wall on the outer edge (front), and the pull bar on it; or null.\n     */\n    drawer:\n      region && parts\n        ? {\n            panel: region,\n            body: parts.body,\n            well: parts.well,\n            front: parts.front,\n            pull: parts.pull,\n          }\n        : null,\n    /** The front edge band revealed by tilt; h is 0 when edge is 0. */\n    front: { x: box.x, y: box.y + box.h - band, w: box.w, h: band },\n    /** The whole plan (surface, seam, and drawer region) above the band. */\n    plan,\n    /** The desk top as drawn: the surface out to the seam line. */\n    slab,\n    /** Drawing geometry for DeskTop; not part of the documented layout. */\n    drawn: parts && { body: parts.drawn, opening: parts.opening },\n    fill: {\n      surface: deskLight(color.bg, k),\n      /** Kept for compatibility: the pull is ink, like Cajon's. */\n      handle: deskLight(color.card, k),\n      front: deskLight(color.bg, k - 0.1),\n      panel: deskLight(color.card, k),\n      well: deskLight(color.bg, k - 0.16),\n    },\n  }\n}\n\n/** A cream desk seen from above: an empty surface, an optional open drawer, and a front edge. */\nexport function DeskTop({ light = 1, edge = 0, ...props }: DeskTopProps) {\n  const l = deskTopLayout({ ...props, light, edge })\n  const { front, slab, drawer, drawn, fill } = l\n  const side = props.drawer\n  const R = 4\n  const banded = front.h > 0\n  // A band thinner than two lines would merge into one heavy edge: it fades in over its first\n  // 6 units while the corners it squares off lose their radius, so small tilts stay continuous.\n  const fade = Math.min(1, front.h / 6)\n  const square = R * (1 - fade)\n  // The band is the desk's front face under the slab; under a side drawer it is the drawer box's\n  // own near face, and a drawer pulled toward the camera shows only its own.\n  const slabOnBand = banded && side !== \"bottom\"\n  const slabR: Corners = {\n    tl: R,\n    tr: R,\n    br: slabOnBand ? square : R,\n    bl: slabOnBand ? square : R,\n  }\n  const deskBand: Box | null = slabOnBand ? { ...front, x: slab.x, w: slab.w } : null\n  const body = drawn?.body\n  const nearFace: Box | null =\n    banded && body && side !== \"top\"\n      ? side === \"bottom\"\n        ? { ...front, x: body.x, w: body.w }\n        : { x: body.x, y: body.y + body.h, w: body.w, h: front.h }\n      : null\n  // Drawer corners: square where the box runs on under the desk, rounded at its free end.\n  const bodyR: Corners =\n    side === \"end\"\n      ? { tl: 0, tr: R, br: nearFace ? square : R, bl: 0 }\n      : side === \"start\"\n        ? { tl: R, tr: 0, br: 0, bl: nearFace ? square : R }\n        : side === \"top\"\n          ? { tl: R, tr: R, br: 0, bl: 0 }\n          : { tl: 0, tr: 0, br: nearFace ? square : R, bl: nearFace ? square : R }\n  const faceR: Corners =\n    side === \"end\"\n      ? { tl: 0, tr: 0, br: R, bl: 0 }\n      : side === \"start\"\n        ? { tl: 0, tr: 0, br: 0, bl: R }\n        : { tl: 0, tr: 0, br: R, bl: R }\n  const pull = drawer?.pull\n  return (\n    <g\n      role=\"img\"\n      aria-label={`Desk seen from above${side ? \", with a drawer\" : \"\"}`}\n      stroke={color.ink}\n      strokeWidth={stroke.outline}\n      strokeLinejoin=\"round\"\n    >\n      {/* The slab goes over the drawer, so its outline is the one seam line. */}\n      {deskBand && (\n        <path\n          d={roundedRect(deskBand, { tl: 0, tr: 0, br: R, bl: R })}\n          fill={fill.front}\n          opacity={fade < 1 ? fade : undefined}\n        />\n      )}\n      {drawer && body && drawn && (\n        <>\n          {nearFace && (\n            <path\n              d={roundedRect(nearFace, faceR)}\n              fill={fill.front}\n              opacity={fade < 1 ? fade : undefined}\n            />\n          )}\n          <path d={roundedRect(body, bodyR)} fill={fill.panel} />\n          <path d={openRect(drawn.opening, side!, 3)} fill={fill.well} />\n        </>\n      )}\n      <path d={roundedRect(slab, slabR)} fill={fill.surface} />\n      {pull && <rect {...rect(pull)} fill={color.ink} />}\n    </g>\n  )\n}\n\ntype Corners = { tl: number; tr: number; br: number; bl: number }\n/** Closed rectangle path with a radius per corner. */\nfunction roundedRect({ x, y, w, h }: Box, c: Corners) {\n  const W = Math.max(0, w),\n    H = Math.max(0, h)\n  const [tl, tr, br, bl] = [c.tl, c.tr, c.br, c.bl].map((r) =>\n    Math.min(r, W / 2, H / 2)\n  )\n  const arc = (r: number, ex: number, ey: number) =>\n    r ? `A${r} ${r} 0 0 1 ${ex} ${ey}` : \"\"\n  return `M${x + tl} ${y}H${x + W - tr}${arc(tr, x + W, y + tr)}V${y + H - br}${arc(br, x + W - br, y + H)}H${x + bl}${arc(bl, x, y + H - bl)}V${y + tl}${arc(tl, x + tl, y)}Z`\n}\n\n/**\n * The drawer's opening: rounded on its three drawn sides and open on the seam side, where it\n * runs on under the desk (the fill closes along the seam line, the stroke does not).\n */\nfunction openRect({ x, y, w, h }: Box, side: DeskTopDrawerSide, r: number) {\n  const W = Math.max(0, w),\n    H = Math.max(0, h)\n  const q = Math.min(r, W / 2, H / 2)\n  const a = (ex: number, ey: number) => `A${q} ${q} 0 0 1 ${ex} ${ey}`\n  // Start at the seam end of one side and walk the three drawn sides clockwise.\n  if (side === \"end\")\n    return `M${x} ${y}H${x + W - q}${a(x + W, y + q)}V${y + H - q}${a(x + W - q, y + H)}H${x}`\n  if (side === \"start\")\n    return `M${x + W} ${y + H}H${x + q}${a(x, y + H - q)}V${y + q}${a(x + q, y)}H${x + W}`\n  if (side === \"bottom\")\n    return `M${x + W} ${y}V${y + H - q}${a(x + W - q, y + H)}H${x + q}${a(x, y + H - q)}V${y}`\n  return `M${x} ${y + H}V${y + q}${a(x + q, y)}H${x + W - q}${a(x + W, y + q)}V${y + H}`\n}\n\nfunction rect({ x, y, w, h }: Box) {\n  return { x, y, width: Math.max(0, w), height: Math.max(0, h) }\n}\n",
      "type": "registry:component",
      "target": "src/jbm/ui/desk-top.tsx"
    }
  ],
  "categories": [
    "UI Bits"
  ],
  "type": "registry:component"
}