Spotlight Card

Card with a soft spotlight that follows the pointer.

Spotlight Card

Move your pointer over this card.

Install

$npx shadcn@latest add https://norvault.septiandwirestu.com/r/spotlight-card.json

Or, with the @norvault namespace configured:

$npx shadcn@latest add @norvault/spotlight-card

Source

"use client";

import { useRef } from "react";
import { cn } from "@/lib/utils";

export type SpotlightCardProps = React.ComponentProps<"div"> & {
  /** Any CSS color. Defaults to a faint tint of the foreground color. */
  spotlightColor?: string;
  /** Spotlight diameter in px. */
  size?: number;
};

export function SpotlightCard({
  spotlightColor = "color-mix(in oklch, var(--foreground) 14%, transparent)",
  size = 360,
  className,
  style,
  children,
  onPointerMove,
  ...props
}: SpotlightCardProps) {
  const ref = useRef<HTMLDivElement>(null);

  return (
    <div
      ref={ref}
      onPointerMove={(e) => {
        const el = ref.current;
        if (el) {
          const rect = el.getBoundingClientRect();
          el.style.setProperty("--spot-x", `${e.clientX - rect.left}px`);
          el.style.setProperty("--spot-y", `${e.clientY - rect.top}px`);
        }
        onPointerMove?.(e);
      }}
      className={cn(
        "group/spotlight relative overflow-hidden rounded-xl border bg-card p-6 text-card-foreground",
        className,
      )}
      style={
        {
          "--spot-color": spotlightColor,
          "--spot-size": `${size}px`,
          ...style,
        } as React.CSSProperties
      }
      {...props}
    >
      <div
        aria-hidden
        className="pointer-events-none absolute inset-0 opacity-0 transition-opacity duration-300 group-hover/spotlight:opacity-100"
        style={{
          background:
            "radial-gradient(var(--spot-size) circle at var(--spot-x, 50%) var(--spot-y, 50%), var(--spot-color), transparent 70%)",
        }}
      />
      <div className="relative">{children}</div>
    </div>
  );
}