Spotlight Grid Free React component
A background grid that wakes up under the cursor. A soft spotlight reveals bright grid lines around the pointer, and every cell it crosses glows and fades behind it like a trail. Drop your hero or section inside it.
Get the code
npx shadcn@latest add https://alphenex.com/r/spotlight-grid.jsonWorks with any React project through the shadcn CLI. Prefer to paste it yourself? Copy the two files below into components/alphenex/. No Tailwind or animation library needed.
import SpotlightGrid from '@/components/alphenex/spotlight-grid';
<SpotlightGrid>
<h1>Your hero goes here</h1>
</SpotlightGrid>'use client';
import { useEffect, useRef } from 'react';
import './spotlight-grid.css';
/** '#3fc4f5' -> '63, 196, 245' for use inside rgba(). */
function rgb(hex) {
const h = hex.replace('#', '');
const full = h.length === 3 ? [...h].map((c) => c + c).join('') : h;
const n = parseInt(full, 16);
return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
}
/**
* SpotlightGrid
* A background grid that wakes up under the cursor: a soft spotlight shows
* the lines around the pointer, and every cell it passes glows, then fades.
* Put a hero or a section inside it as children.
*
* Free from Alphenex Lab: https://alphenex.com/lab/spotlight-grid (MIT)
*/
export default function SpotlightGrid({
children,
cell = 44, // grid cell size in px
color = '#3fc4f5', // glow colour
radius = 220, // spotlight size in px
trail = 700, // how long a lit cell takes to fade, in ms
background = '#07090c',
className = '',
style,
...props
}) {
const rootRef = useRef(null);
const canvasRef = useRef(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return undefined;
const ctx = canvas.getContext('2d');
const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const lit = new Map(); // "col,row" -> time it was lit
const tint = rgb(color);
let frame = 0;
let width = 0;
let height = 0;
const resize = () => {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
width = root.clientWidth;
height = root.clientHeight;
canvas.width = width * dpr;
canvas.height = height * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
};
const draw = () => {
const now = performance.now();
ctx.clearRect(0, 0, width, height);
for (const [key, born] of lit) {
const life = 1 - (now - born) / trail;
if (life <= 0) {
lit.delete(key);
continue;
}
const [col, row] = key.split(',').map(Number);
ctx.fillStyle = `rgba(${tint}, ${0.42 * life * life})`;
ctx.fillRect(col * cell + 1, row * cell + 1, cell - 1, cell - 1);
}
frame = lit.size ? requestAnimationFrame(draw) : 0;
};
const onMove = (event) => {
const rect = root.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
root.style.setProperty('--alx-spot-x', `${x}px`);
root.style.setProperty('--alx-spot-y', `${y}px`);
root.classList.add('is-active');
if (reduce) return;
lit.set(`${Math.floor(x / cell)},${Math.floor(y / cell)}`, performance.now());
if (!frame) frame = requestAnimationFrame(draw);
};
const onLeave = () => root.classList.remove('is-active');
resize();
const observer = new ResizeObserver(resize);
observer.observe(root);
root.addEventListener('pointermove', onMove, { passive: true });
root.addEventListener('pointerleave', onLeave);
return () => {
observer.disconnect();
cancelAnimationFrame(frame);
root.removeEventListener('pointermove', onMove);
root.removeEventListener('pointerleave', onLeave);
};
}, [cell, color, trail]);
return (
<div
ref={rootRef}
className={`alx-spot ${className}`}
style={{
'--alx-spot-cell': `${cell}px`,
'--alx-spot-color': color,
'--alx-spot-radius': `${radius}px`,
'--alx-spot-bg': background,
...style,
}}
{...props}
>
<div className="alx-spot__lines" aria-hidden="true" />
<div className="alx-spot__lines alx-spot__lines--lit" aria-hidden="true" />
<canvas ref={canvasRef} className="alx-spot__cells" aria-hidden="true" />
<div className="alx-spot__content">{children}</div>
</div>
);
}
/* SpotlightGrid, Alphenex Lab (MIT) */
.alx-spot {
--alx-spot-x: 50%;
--alx-spot-y: 50%;
position: relative;
isolation: isolate;
overflow: hidden;
background: var(--alx-spot-bg, #07090c);
touch-action: pan-y;
}
.alx-spot__lines,
.alx-spot__cells {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
/* Faint grid everywhere. */
.alx-spot__lines {
background-image:
linear-gradient(to right, rgba(255, 255, 255, 0.06) 1px, transparent 1px),
linear-gradient(to bottom, rgba(255, 255, 255, 0.06) 1px, transparent 1px);
background-size: var(--alx-spot-cell) var(--alx-spot-cell);
}
/* Bright grid, shown only inside the spotlight around the cursor. */
.alx-spot__lines--lit {
background-image:
linear-gradient(to right, var(--alx-spot-color) 1px, transparent 1px),
linear-gradient(to bottom, var(--alx-spot-color) 1px, transparent 1px);
opacity: 0;
transition: opacity 0.4s ease;
-webkit-mask-image: radial-gradient(circle var(--alx-spot-radius) at var(--alx-spot-x) var(--alx-spot-y), #000 0%, transparent 70%);
mask-image: radial-gradient(circle var(--alx-spot-radius) at var(--alx-spot-x) var(--alx-spot-y), #000 0%, transparent 70%);
}
.alx-spot.is-active .alx-spot__lines--lit {
opacity: 0.55;
}
.alx-spot__content {
position: relative;
z-index: 1;
}
Paste this into Claude, ChatGPT, Cursor, Lovable or v0 to build it from scratch.
Build a React "SpotlightGrid" background component with no libraries. Layer 1: a faint grid made with two repeating linear-gradients. Layer 2: the same grid in a bright colour, shown only inside a radial-gradient CSS mask centred on the cursor (store the cursor position in CSS variables on pointermove). Layer 3: a canvas where every grid cell the cursor passes is filled with the glow colour and fades out over a set time (store lit cells in a Map with the time they were lit, and only run requestAnimationFrame while any cell is still fading). Resize the canvas with a ResizeObserver and devicePixelRatio. Children render on top. Props: cell, color, radius, trail, background, children.How the spotlight grid works
The grid is drawn twice with CSS gradients: a faint version everywhere, and a bright version masked with a radial gradient that follows the cursor through two CSS variables. Moving the mouse only updates those variables, so the spotlight costs almost nothing.
The glowing trail is a canvas. Each cell the cursor enters is saved with the time it lit up, and every frame redraws the lit cells with an opacity that falls to zero over the trail length. When the last cell has faded the animation loop stops.
Your content sits on top as children, so the grid works as the background for a hero, a pricing block or a whole section.
Frequently asked questions
Want motion like this on your website?
We design and build custom websites with motion, 3D and SEO built in. Websites from $500.