{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "swinging-id-badge",
  "title": "Swinging ID Badge",
  "description": "An event ID badge hanging from a printed lanyard that you can grab and fling: it swings back on real pendulum physics, the strap stretches and settles, the card turns on its clip and the foil strip shimmers as it moves; click to turn it over to a QR code and access zones.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/swinging-id-badge/swinging-id-badge.tsx",
      "content": "\"use client\";\n/* eslint-disable @next/next/no-img-element -- Registry components work in any React app. */\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 SwingingIdBadgeTheme = \"dark\" | \"light\";\n\nexport interface SwingingIdBadgeProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  name?: string;\n  role?: string;\n  company?: string;\n  /** Event or pass label, printed on the strap and the badge. */\n  event?: string;\n  badgeId?: string;\n  photo?: string;\n  photoAlt?: string;\n  /** Access zones listed on the back. */\n  zones?: string[];\n  validUntil?: string;\n  /** Strap length in px, from the anchor to the clip. */\n  length?: number;\n  /** Stronger gravity swings faster. */\n  gravity?: number;\n  /** 0 = swings for a long time, 1 = settles quickly. */\n  damping?: number;\n  /** Click (or Enter) turns the badge over. */\n  flippable?: boolean;\n  accent?: string;\n  theme?: SwingingIdBadgeTheme;\n}\n\nconst W = 236;\nconst H = 336;\nconst CLIP = 30;\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\n/** A QR-looking grid seeded from the badge id: three finder squares and stable noise in between. */\nfunction QrGlyph({ seed, color }: { seed: string; color: string }) {\n  const n = 21;\n  let h = 2166136261;\n  for (const c of seed) h = Math.imul(h ^ c.charCodeAt(0), 16777619);\n  const rand = () => {\n    h ^= h << 13;\n    h ^= h >>> 17;\n    h ^= h << 5;\n    return ((h >>> 0) % 1000) / 1000;\n  };\n  const finder = (x: number, y: number) => {\n    const inBox = (ox: number, oy: number) => x >= ox && x < ox + 7 && y >= oy && y < oy + 7;\n    for (const [ox, oy] of [\n      [0, 0],\n      [n - 7, 0],\n      [0, n - 7],\n    ] as const) {\n      if (inBox(ox, oy)) {\n        const dx = x - ox;\n        const dy = y - oy;\n        const ring = dx === 0 || dx === 6 || dy === 0 || dy === 6;\n        const core = dx >= 2 && dx <= 4 && dy >= 2 && dy <= 4;\n        return ring || core ? 1 : 0;\n      }\n    }\n    return -1;\n  };\n  const cells: React.ReactNode[] = [];\n  for (let y = 0; y < n; y++)\n    for (let x = 0; x < n; x++) {\n      const f = finder(x, y);\n      const on = f === -1 ? rand() > 0.52 : f === 1;\n      if (on) cells.push(<rect key={`${x}-${y}`} x={x} y={y} width={1.02} height={1.02} />);\n    }\n  return (\n    <svg viewBox={`0 0 ${n} ${n}`} className=\"h-full w-full\" fill={color} aria-hidden=\"true\">\n      {cells}\n    </svg>\n  );\n}\n\nexport function SwingingIdBadge({\n  name = \"Mara Lindqvist\",\n  role = \"Design Engineer\",\n  company = \"Northline\",\n  event = \"Build Week 2026\",\n  badgeId = \"NL-0427\",\n  photo = \"/demo/39.webp\",\n  photoAlt = \"\",\n  zones = [\"Main stage\", \"Studios\", \"Workshop floor\", \"Speaker lounge\"],\n  validUntil = \"14 Mar 2026\",\n  length = 190,\n  gravity = 2400,\n  damping = 0.35,\n  flippable = true,\n  accent = \"#d4ff3f\",\n  theme = \"dark\",\n  className,\n  style,\n  ...props\n}: SwingingIdBadgeProps) {\n  const reduce = useReducedMotion();\n  const light = theme === \"light\";\n  const rigRef = React.useRef<HTMLDivElement>(null);\n  const strapRef = React.useRef<HTMLDivElement>(null);\n  const cardRef = React.useRef<HTMLDivElement>(null);\n  const holoRef = React.useRef<HTMLDivElement>(null);\n  const stageRef = React.useRef<HTMLDivElement>(null);\n  const raf = React.useRef(0);\n  // Pendulum state: angle (rad), angular velocity, strap length and its velocity, plus the card's yaw.\n  const sim = React.useRef({ a: 0, w: 0, l: length, lv: 0, yaw: 0, yv: 0, last: 0 });\n  const drag = React.useRef<{ id: number; grab: number; x: number; y: number; lastA: number; lastT: number; moved: boolean } | null>(null);\n  const [flipped, setFlipped] = React.useState(false);\n  const params = React.useRef({ length, gravity, damping });\n  React.useEffect(() => {\n    params.current = { length, gravity, damping };\n  }, [length, gravity, damping]);\n\n  const paint = React.useCallback(() => {\n    const s = sim.current;\n    if (rigRef.current) rigRef.current.style.transform = `translateX(-50%) rotate(${s.a}rad)`;\n    if (strapRef.current) strapRef.current.style.height = `${s.l}px`;\n    if (cardRef.current) cardRef.current.style.setProperty(\"--yaw\", `${s.yaw}deg`);\n    // The foil shimmer slides with the swing.\n    if (holoRef.current) holoRef.current.style.backgroundPosition = `${50 + s.a * 120 + s.yaw * 1.5}% 50%`;\n  }, []);\n\n  const run = React.useCallback(() => {\n    cancelAnimationFrame(raf.current);\n    const tick = (now: number) => {\n      const s = sim.current;\n      const p = params.current;\n      const dt = Math.min(1 / 30, s.last ? (now - s.last) / 1000 : 1 / 60);\n      s.last = now;\n      const steps = 3;\n      const h = dt / steps;\n      for (let i = 0; i < steps; i++) {\n        if (!drag.current) {\n          // Pendulum: gravity pulls the angle home, damping bleeds energy.\n          const c = 0.4 + p.damping * 4;\n          s.w += (-(p.gravity / s.l) * Math.sin(s.a) - c * s.w) * h;\n          s.a += s.w * h;\n          // The strap stretches while dragged and springs back to its rest length.\n          s.lv += (-(s.l - p.length) * 220 - s.lv * 18) * h;\n          s.l += s.lv * h;\n        }\n        // The card yaws with the swing speed, on a lazier spring, so it seems to turn on the clip.\n        const yawTarget = Math.max(-38, Math.min(38, -s.w * 11));\n        s.yv += ((yawTarget - s.yaw) * 90 - s.yv * 11) * h;\n        s.yaw += s.yv * h;\n      }\n      paint();\n      const energy = Math.abs(s.a) + Math.abs(s.w) + Math.abs(s.l - p.length) / 40 + Math.abs(s.yaw) / 40 + Math.abs(s.yv) / 40;\n      if (drag.current || energy > 0.0015) raf.current = requestAnimationFrame(tick);\n      else {\n        Object.assign(s, { a: 0, w: 0, l: p.length, lv: 0, yaw: 0, yv: 0, last: 0 });\n        paint();\n      }\n    };\n    raf.current = requestAnimationFrame(tick);\n  }, [paint]);\n\n  React.useEffect(() => {\n    sim.current.l = length;\n    paint();\n    return () => cancelAnimationFrame(raf.current);\n  }, [length, paint]);\n\n  const anchor = () => {\n    const r = stageRef.current!.getBoundingClientRect();\n    // getBoundingClientRect is in screen pixels; convert back to CSS pixels in case a parent is scaled.\n    const scale = r.width / stageRef.current!.offsetWidth || 1;\n    return { x: r.left + r.width / 2, y: r.top, scale };\n  };\n\n  const onPointerDown = (event: React.PointerEvent<HTMLDivElement>) => {\n    if (reduce || event.button !== 0) return;\n    event.currentTarget.setPointerCapture(event.pointerId);\n    const a = anchor();\n    const dx = (event.clientX - a.x) / a.scale;\n    const dy = (event.clientY - a.y) / a.scale;\n    drag.current = { id: event.pointerId, grab: Math.max(40, Math.hypot(dx, dy)), x: event.clientX, y: event.clientY, lastA: sim.current.a, lastT: performance.now(), moved: false };\n    sim.current.w = 0;\n    run();\n  };\n  const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (!d) return;\n    const a = anchor();\n    const dx = (event.clientX - a.x) / a.scale;\n    const dy = Math.max(10, (event.clientY - a.y) / a.scale);\n    const s = sim.current;\n    // CSS rotate() is clockwise, which swings the hanging end to the left, so a pointer on the left is a positive angle.\n    const angle = Math.max(-1.25, Math.min(1.25, Math.atan2(-dx, dy)));\n    const now = performance.now();\n    // A press only counts as a drag once the pointer itself has travelled; a still press on a swaying badge is a click.\n    if (!d.moved && Math.hypot(event.clientX - d.x, event.clientY - d.y) < 5) return;\n    d.moved = true;\n    // Keep the grabbed point under the pointer: the strap stretches or slackens a little to follow.\n    const want = (Math.hypot(dx, dy) / d.grab) * params.current.length;\n    s.l = Math.max(params.current.length * 0.85, Math.min(params.current.length * 1.35, want));\n    const dt = Math.max(1, now - d.lastT) / 1000;\n    s.w = s.w * 0.6 + ((angle - s.a) / dt) * 0.4;\n    s.a = angle;\n    d.lastA = angle;\n    d.lastT = now;\n  };\n  const onPointerUp = () => {\n    const d = drag.current;\n    drag.current = null;\n    if (!d) return;\n    sim.current.w = Math.max(-9, Math.min(9, sim.current.w));\n    sim.current.last = 0;\n    run();\n    if (!d.moved && flippable) setFlipped((f) => !f);\n  };\n\n  const push = (dir: number) => {\n    if (reduce) return;\n    sim.current.w += dir * 2.6;\n    run();\n  };\n\n  const ink = \"#121212\";\n  const paper = \"#f6f6f2\";\n\n  return (\n    <div className={cn(\"relative mx-auto select-none\", className)} style={{ width: W + 120, height: length + CLIP + H + 40, ...style }} {...props}>\n      {/* Anchor: a small rail the strap hangs from. */}\n      <div aria-hidden=\"true\" className=\"absolute top-0 left-1/2 h-2 w-16 -translate-x-1/2 rounded-b-md\" style={{ background: light ? \"#cfcfca\" : \"#2a2a2a\" }} />\n      <div ref={stageRef} className=\"absolute top-1 left-0 h-0 w-full\" />\n      <div ref={rigRef} className=\"absolute top-1 left-1/2 flex origin-top flex-col items-center\" style={{ transform: \"translateX(-50%)\", willChange: \"transform\" }}>\n        {/* Strap: woven ribbon with the event printed down it. */}\n        <div\n          ref={strapRef}\n          aria-hidden=\"true\"\n          className=\"relative w-[22px] overflow-hidden\"\n          style={{\n            height: length,\n            background: `repeating-linear-gradient(90deg, ${accent} 0 2px, ${accent}e6 2px 4px)`,\n            boxShadow: \"inset 2px 0 0 rgba(0,0,0,.18), inset -2px 0 0 rgba(0,0,0,.18)\",\n          }}\n        >\n          <span className=\"absolute top-2 left-1/2 -translate-x-1/2 font-mono text-[8.5px] font-semibold tracking-[0.3em] whitespace-nowrap text-black/70 uppercase [writing-mode:vertical-rl]\">\n            {`${company} · ${event} · ${company} · ${event}`}\n          </span>\n        </div>\n        {/* Clip: a metal swivel and a jaw. */}\n        <div aria-hidden=\"true\" className=\"relative -mt-1 flex flex-col items-center\" style={{ height: CLIP }}>\n          <span className=\"block size-3 rounded-full border-2 border-[#b9b9b4] bg-transparent\" />\n          <span className=\"-mt-0.5 block h-4 w-9 rounded-[4px] bg-gradient-to-b from-[#e2e2de] via-[#a9a9a4] to-[#d6d6d1] shadow-[0_1px_2px_rgba(0,0,0,.4)]\" />\n        </div>\n\n        <div className=\"[perspective:900px]\" style={{ width: W, height: H }}>\n          <div\n            ref={cardRef}\n            role={flippable ? \"button\" : undefined}\n            tabIndex={0}\n            aria-pressed={flippable ? flipped : undefined}\n            aria-label={`${name}, ${role} at ${company}. ID ${badgeId}.${flippable ? \" Press Enter to turn the badge over.\" : \"\"} Arrow keys swing it.`}\n            className={cn(\"relative h-full w-full cursor-grab touch-none outline-none active:cursor-grabbing\", \"focus-visible:ring-2 focus-visible:ring-offset-4\")}\n            style={{\n              transformStyle: \"preserve-3d\",\n              transform: \"rotateY(var(--yaw, 0deg))\",\n              borderRadius: 16,\n              [\"--tw-ring-color\" as string]: accent,\n              [\"--tw-ring-offset-color\" as string]: light ? \"#f1f1ef\" : \"#0a0a0a\",\n            }}\n            onPointerDown={onPointerDown}\n            onPointerMove={onPointerMove}\n            onPointerUp={onPointerUp}\n            onPointerCancel={onPointerUp}\n            onKeyDown={(event) => {\n              if ((event.key === \"Enter\" || event.key === \" \") && flippable) {\n                event.preventDefault();\n                setFlipped((f) => !f);\n              } else if (event.key === \"ArrowLeft\" || event.key === \"ArrowRight\") {\n                event.preventDefault();\n                push(event.key === \"ArrowLeft\" ? -1 : 1);\n              }\n            }}\n          >\n            {/* The swing's yaw is written every frame on the outer layer; the flip eases on this inner one. */}\n            <div\n              className=\"absolute inset-0\"\n              style={{ transformStyle: \"preserve-3d\", transform: `rotateY(${flipped ? 180 : 0}deg)`, transition: reduce ? \"none\" : \"transform 650ms cubic-bezier(.65,0,.35,1)\" }}\n            >\n              {/* Front */}\n              <div\n                className=\"absolute inset-0 overflow-hidden rounded-2xl [backface-visibility:hidden]\"\n                style={{\n                  background: paper,\n                  color: ink,\n                  boxShadow: \"0 30px 50px -24px rgba(0,0,0,.65), 0 2px 0 rgba(255,255,255,.4) inset\",\n                  visibility: flipped ? \"hidden\" : \"visible\",\n                  transition: `visibility 0s linear ${reduce ? 0 : 325}ms`,\n                }}\n              >\n                {/* Slot punched for the clip. */}\n                <span aria-hidden=\"true\" className=\"absolute top-2.5 left-1/2 h-1.5 w-12 -translate-x-1/2 rounded-full bg-black/80\" />\n                <div className=\"flex items-center justify-between px-4 pt-7 font-mono text-[9px] tracking-[0.18em] uppercase\">\n                  <span className=\"font-semibold\">{company}</span>\n                  <span className=\"opacity-60\">{event}</span>\n                </div>\n                <div className=\"mx-4 mt-3 overflow-hidden rounded-[10px]\" style={{ height: 150 }}>\n                  <img src={photo} alt={photoAlt} draggable={false} className=\"pointer-events-none h-full w-full object-cover grayscale\" />\n                </div>\n                <div className=\"px-4 pt-3.5\">\n                  <p className=\"text-[19px] leading-tight font-semibold tracking-[-0.01em]\">{name}</p>\n                  <p className=\"mt-0.5 text-[12px] opacity-60\">{role}</p>\n                </div>\n                <div className=\"absolute inset-x-4 bottom-4 flex items-end justify-between\">\n                  <div>\n                    <p className=\"font-mono text-[8.5px] tracking-[0.18em] uppercase opacity-50\">ID</p>\n                    <p className=\"font-mono text-[12px] font-semibold\">{badgeId}</p>\n                  </div>\n                  {/* Foil strip: its gradient slides with the swing, like a hologram catching the light. */}\n                  <div\n                    ref={holoRef}\n                    aria-hidden=\"true\"\n                    className=\"h-7 w-20 rounded-[6px]\"\n                    style={{\n                      background: \"linear-gradient(115deg, #c9c9c9 0%, #f4d1ff 18%, #bfe8ff 32%, #d9ffd1 46%, #fff3b8 60%, #ffc9e6 74%, #c9c9c9 100%)\",\n                      backgroundSize: \"300% 100%\",\n                      backgroundPosition: \"50% 50%\",\n                      boxShadow: \"inset 0 0 0 1px rgba(0,0,0,.08)\",\n                    }}\n                  />\n                </div>\n              </div>\n\n              {/* Back */}\n              <div\n                className=\"absolute inset-0 overflow-hidden rounded-2xl [backface-visibility:hidden]\"\n                style={{\n                  background: ink,\n                  color: paper,\n                  transform: \"rotateY(180deg)\",\n                  boxShadow: \"0 30px 50px -24px rgba(0,0,0,.65)\",\n                  visibility: flipped ? \"visible\" : \"hidden\",\n                  transition: `visibility 0s linear ${reduce ? 0 : 325}ms`,\n                }}\n              >\n                <span aria-hidden=\"true\" className=\"absolute top-2.5 left-1/2 h-1.5 w-12 -translate-x-1/2 rounded-full bg-black\" />\n                <div className=\"flex items-center justify-between px-4 pt-7 font-mono text-[9px] tracking-[0.18em] uppercase\">\n                  <span style={{ color: accent }}>Access</span>\n                  <span className=\"opacity-60\">{badgeId}</span>\n                </div>\n                <div className=\"mx-auto mt-4 size-[104px] rounded-md bg-[#f6f6f2] p-2\">\n                  <QrGlyph seed={badgeId} color={ink} />\n                </div>\n                <ul className=\"mt-4 space-y-1.5 px-4\">\n                  {zones.map((zone) => (\n                    <li key={zone} className=\"flex items-center justify-between border-b border-white/10 pb-1.5 text-[11.5px]\">\n                      {zone}\n                      <span className=\"size-1.5 rounded-full\" style={{ background: accent }} />\n                    </li>\n                  ))}\n                </ul>\n                <div className=\"absolute inset-x-4 bottom-4 flex justify-between font-mono text-[8.5px] tracking-[0.14em] uppercase opacity-60\">\n                  <span>Valid until {validUntil}</span>\n                  <span>If found, return</span>\n                </div>\n              </div>\n            </div>\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}