{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spotlight-card",
  "type": "registry:component",
  "title": "Spotlight card",
  "description": "A card surface whose border and background catch a soft light that follows the pointer, shared across a bento grid.",
  "categories": [
    "layout"
  ],
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/registry/overtrue/spotlight-card.tsx",
      "type": "registry:component",
      "target": "components/overtrue/spotlight-card.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\n/** A point inside a card or group, as 0–1 fractions of its width and height. */\nexport interface SpotlightPoint {\n  x: number;\n  y: number;\n}\n\ntype SpotlightRegistry = {\n  register: (element: HTMLElement) => () => void;\n};\n\nconst SpotlightGroupContext = React.createContext<SpotlightRegistry | null>(\n  null,\n);\n\n// How bright the parked light is compared with the light under the pointer.\nconst RESTING_OPACITY = 0.85;\n\n/**\n * Drive the light for a set of cards from one element's pointer events. Positions\n * are written straight into CSS variables inside requestAnimationFrame, so moving\n * the pointer never re-renders React.\n */\nfunction useSpotlight(\n  target: React.RefObject<HTMLElement | null>,\n  cards: () => Iterable<HTMLElement>,\n  options: { enabled: boolean; rest?: SpotlightPoint },\n) {\n  const cardsRef = React.useRef(cards);\n  cardsRef.current = cards;\n  const refresh = React.useRef<() => void>(() => {});\n  const restX = options.rest?.x;\n  const restY = options.rest?.y;\n\n  React.useEffect(() => {\n    const element = target.current;\n    if (!options.enabled || !element) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const rest =\n      restX != null && restY != null ? { x: restX, y: restY } : null;\n    let frame = 0;\n    let hovering = false;\n    // Positions are relative to the tracked element so scrolling never skews them.\n    let current: { x: number; y: number } | null = null;\n    let goal: { x: number; y: number } | null = null;\n\n    const restPoint = () =>\n      rest\n        ? {\n            x: element.offsetWidth * rest.x,\n            y: element.offsetHeight * rest.y,\n          }\n        : null;\n\n    const paint = () => {\n      if (!current) return;\n      const origin = element.getBoundingClientRect();\n      const level = hovering ? 1 : rest ? RESTING_OPACITY : 0;\n      for (const card of cardsRef.current()) {\n        const rect = card.getBoundingClientRect();\n        const x = current.x - (rect.left - origin.left);\n        const y = current.y - (rect.top - origin.top);\n        const inside = x >= 0 && y >= 0 && x <= rect.width && y <= rect.height;\n        card.style.setProperty(\"--spotlight-x\", `${x.toFixed(1)}px`);\n        card.style.setProperty(\"--spotlight-y\", `${y.toFixed(1)}px`);\n        card.style.setProperty(\"--spotlight-edge\", String(level));\n        card.style.setProperty(\n          \"--spotlight-fill\",\n          inside ? String(level) : \"0\",\n        );\n      }\n    };\n\n    const tick = () => {\n      frame = 0;\n      if (!goal) return;\n      if (!current || reduced.matches) current = { ...goal };\n      else {\n        // Ease towards the goal: quick under the pointer, slower on the way back to rest.\n        const ease = hovering ? 0.35 : 0.12;\n        current = {\n          x: current.x + (goal.x - current.x) * ease,\n          y: current.y + (goal.y - current.y) * ease,\n        };\n        if (\n          Math.abs(goal.x - current.x) < 0.5 &&\n          Math.abs(goal.y - current.y) < 0.5\n        )\n          current = { ...goal };\n      }\n      paint();\n      if (current.x !== goal.x || current.y !== goal.y)\n        frame = requestAnimationFrame(tick);\n    };\n\n    const schedule = () => {\n      if (!frame) frame = requestAnimationFrame(tick);\n    };\n\n    const settle = () => {\n      hovering = false;\n      goal = restPoint();\n      if (goal) {\n        schedule();\n        return;\n      }\n      if (frame) cancelAnimationFrame(frame);\n      frame = 0;\n      for (const card of cardsRef.current()) {\n        card.style.setProperty(\"--spotlight-edge\", \"0\");\n        card.style.setProperty(\"--spotlight-fill\", \"0\");\n      }\n    };\n\n    const onMove = (event: PointerEvent) => {\n      // Touch has no hover, and reduced motion keeps the light still.\n      if (event.pointerType === \"touch\" || reduced.matches) return;\n      const origin = element.getBoundingClientRect();\n      const point = {\n        x: event.clientX - origin.left,\n        y: event.clientY - origin.top,\n      };\n      // Without a resting light there is nothing to glide from.\n      if (!hovering && !rest) current = point;\n      hovering = true;\n      goal = point;\n      schedule();\n    };\n\n    refresh.current = () => {\n      if (hovering) {\n        paint();\n        return;\n      }\n      goal = restPoint();\n      if (!goal) return;\n      current = { ...goal };\n      paint();\n    };\n\n    const observer = new ResizeObserver(() => refresh.current());\n    observer.observe(element);\n    element.addEventListener(\"pointermove\", onMove, { passive: true });\n    element.addEventListener(\"pointerleave\", settle);\n    element.addEventListener(\"pointercancel\", settle);\n    reduced.addEventListener(\"change\", settle);\n    refresh.current();\n\n    return () => {\n      observer.disconnect();\n      element.removeEventListener(\"pointermove\", onMove);\n      element.removeEventListener(\"pointerleave\", settle);\n      element.removeEventListener(\"pointercancel\", settle);\n      reduced.removeEventListener(\"change\", settle);\n      if (frame) cancelAnimationFrame(frame);\n      refresh.current = () => {};\n    };\n  }, [target, options.enabled, restX, restY]);\n\n  return refresh;\n}\n\nexport interface SpotlightCardProps extends React.ComponentProps<\"div\"> {\n  /** Any CSS color for the glow. Defaults to the theme's primary color. */\n  color?: string;\n  /** Glow strength from 0 to 2. Defaults to 1. */\n  intensity?: number;\n  /** Radius of the surface glow in px. The border glow reaches 55% of it. */\n  size?: number;\n  /** Apply the standard p-6 padding. Turn off for full-bleed media or custom padding. */\n  padding?: boolean;\n  /** Keep a faint highlight along the top edge. */\n  sheen?: boolean;\n  /**\n   * Park a softer light at this point while the pointer is away, as 0–1 fractions\n   * of the card. Ignored inside a SpotlightGroup, which has its own.\n   */\n  restingLight?: SpotlightPoint;\n}\n\nconst borderMask: React.CSSProperties = {\n  padding: 1,\n  WebkitMask:\n    \"linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)\",\n  WebkitMaskComposite: \"xor\",\n  mask: \"linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0)\",\n};\n\n/**\n * A card whose surface and 1px border catch a soft light that follows the pointer.\n * Wrap several in `SpotlightGroup` so neighboring cards light up together.\n * Touch input and reduced motion keep the light still.\n */\nfunction SpotlightCard({\n  color = \"var(--color-primary)\",\n  intensity = 1,\n  size = 340,\n  padding = true,\n  sheen = true,\n  restingLight,\n  className,\n  children,\n  ref,\n  ...props\n}: SpotlightCardProps) {\n  const local = React.useRef<HTMLDivElement>(null);\n  React.useImperativeHandle(ref, () => local.current as HTMLDivElement);\n  const group = React.useContext(SpotlightGroupContext);\n  const strength = Math.min(2, Math.max(0, intensity));\n\n  React.useEffect(() => {\n    if (!group || !local.current) return;\n    return group.register(local.current);\n  }, [group]);\n\n  useSpotlight(local, () => (local.current ? [local.current] : []), {\n    enabled: !group,\n    rest: restingLight,\n  });\n\n  const mix = (amount: number) =>\n    `color-mix(in oklab, ${color} ${Math.round(Math.min(100, amount))}%, transparent)`;\n\n  return (\n    <div\n      data-slot=\"spotlight-card\"\n      ref={local}\n      className={cn(\n        \"relative isolate min-w-0 rounded-xl border bg-card text-card-foreground shadow-xs\",\n        padding && \"p-6\",\n        className,\n      )}\n      {...props}\n    >\n      {sheen ? (\n        <span\n          aria-hidden=\"true\"\n          data-slot=\"spotlight-card-sheen\"\n          className=\"pointer-events-none absolute inset-x-[14%] -top-px -z-10 h-px\"\n          style={{\n            background: `linear-gradient(90deg, transparent, ${mix(strength * 36)}, transparent)`,\n          }}\n        />\n      ) : null}\n      <span\n        aria-hidden=\"true\"\n        data-slot=\"spotlight-card-glow\"\n        className=\"pointer-events-none absolute inset-0 -z-10 rounded-[inherit] transition-opacity duration-500 ease-out motion-reduce:transition-none\"\n        style={{\n          opacity: \"var(--spotlight-fill, 0)\",\n          background: `radial-gradient(${size}px circle at var(--spotlight-x, 50%) var(--spotlight-y, 0px), ${mix(strength * 15)}, transparent 70%)`,\n        }}\n      />\n      <span\n        aria-hidden=\"true\"\n        data-slot=\"spotlight-card-edge\"\n        className=\"pointer-events-none absolute -inset-px -z-10 rounded-[inherit] transition-opacity duration-500 ease-out motion-reduce:transition-none\"\n        style={{\n          ...borderMask,\n          opacity: \"var(--spotlight-edge, 0)\",\n          background: `radial-gradient(${Math.round(size * 0.55)}px circle at var(--spotlight-x, 50%) var(--spotlight-y, 0px), ${mix(strength * 100)}, ${mix(strength * 38)} 40%, transparent 100%)`,\n        }}\n      />\n      {children}\n    </div>\n  );\n}\n\nexport interface SpotlightGroupProps extends React.ComponentProps<\"div\"> {\n  /**\n   * Park a softer light at this point while the pointer is away, as 0–1 fractions\n   * of the group, so the grid looks lit before anyone points at it.\n   */\n  restingLight?: SpotlightPoint;\n}\n\n/**\n * Shares one pointer listener between the spotlight cards inside it, so the light\n * spills onto neighboring borders as the pointer crosses the gaps. Lay the cards\n * out with your own grid classes on this element.\n */\nfunction SpotlightGroup({\n  restingLight,\n  className,\n  children,\n  ref,\n  ...props\n}: SpotlightGroupProps) {\n  const local = React.useRef<HTMLDivElement>(null);\n  React.useImperativeHandle(ref, () => local.current as HTMLDivElement);\n  const cards = React.useRef(new Set<HTMLElement>());\n  const refresh = useSpotlight(local, () => cards.current, {\n    enabled: true,\n    rest: restingLight,\n  });\n  const registry = React.useMemo<SpotlightRegistry>(\n    () => ({\n      register(element) {\n        cards.current.add(element);\n        refresh.current();\n        return () => {\n          cards.current.delete(element);\n        };\n      },\n    }),\n    [refresh],\n  );\n\n  return (\n    <SpotlightGroupContext.Provider value={registry}>\n      <div\n        data-slot=\"spotlight-group\"\n        ref={local}\n        className={cn(\"min-w-0\", className)}\n        {...props}\n      >\n        {children}\n      </div>\n    </SpotlightGroupContext.Provider>\n  );\n}\n\nexport { SpotlightCard, SpotlightGroup };\n"
    }
  ],
  "docs": "Documentation: https://ui.overtrue.xyz/components/spotlight-card. Requires an initialized shadcn/ui project with Tailwind CSS v4."
}
