Interactive grid
Also called hover grid background, grid trail effect, or Interactive Grid Pattern
A background grid of squares where the cell under the pointer lights up and fades out after the pointer moves on, leaving a short glowing trail.
import { useRef, type PointerEvent, type ReactNode } from 'react';
const CELL = 40; // px
const COLS = 32;
const ROWS = 14;
export function InteractiveGridHero({ children }: { children: ReactNode }) {
const gridRef = useRef<HTMLDivElement>(null);
const lit = useRef<HTMLElement | null>(null);
// The new cell turns on instantly; the one it left fades out with its own
// CSS transition, which is what draws the trail.
function light(cell: HTMLElement | null) {
if (cell === lit.current) return;
if (lit.current) {
lit.current.style.transitionDuration = '';
lit.current.style.opacity = '0';
}
if (cell) {
cell.style.transitionDuration = '0ms';
cell.style.opacity = '1';
}
lit.current = cell;
}
function onPointerMove(event: PointerEvent<HTMLElement>) {
const grid = gridRef.current;
if (!grid || event.pointerType !== 'mouse') return;
const rect = grid.getBoundingClientRect();
const col = Math.floor((event.clientX - rect.left) / CELL);
const row = Math.floor((event.clientY - rect.top) / CELL);
const inside = col >= 0 && col < COLS && row >= 0 && row < ROWS;
const cell = inside
? grid.children[row * COLS + col]?.firstElementChild
: null;
light(cell instanceof HTMLElement ? cell : null);
}
return (
<section
onPointerMove={onPointerMove}
onPointerLeave={() => light(null)}
className='relative grid h-[560px] place-items-center overflow-hidden'
>
<div
ref={gridRef}
aria-hidden
className='absolute top-1/2 left-1/2 grid -translate-1/2 mask-radial-from-40% mask-radial-to-75%'
style={{
gridTemplateColumns: `repeat(${COLS}, ${CELL}px)`,
gridAutoRows: CELL
}}
>
{Array.from({ length: COLS * ROWS }, (_, index) => (
<div key={index} className='border-r border-b border-black/8'>
<span className='block size-full bg-blue-500/35 opacity-0 transition-opacity duration-900 ease-out motion-reduce:duration-0' />
</div>
))}
</div>
<div className='pointer-events-none relative text-center'>{children}</div>
</section>
);
}Add an interactive grid behind the hero: fill the section with 40px square cells with a 1px border at 8% gray, faded toward the edges with a radial mask-image. Track pointermove on the section, find the cell under the pointer from its coordinates, and show that cell’s fill (blue at 35% opacity) instantly. When the pointer moves to another cell, fade the previous one out over 900ms with ease-out. Update the cells’ styles directly, outside React state, keep the headline on top with pointer-events: none, and drop the trail when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Interactive grid?
An interactive grid turns a plain grid background into something that answers the pointer. Each square is its own element, or a cell drawn on a canvas. The cell under the pointer lights up at once, and when the pointer moves on, that cell fades back out over half a second or so. A quick sweep leaves a trail of fading squares behind it.
Use it behind a hero headline, an empty state, or a sign-up section, often on sites for developer and design tools, where a little play rewards people for moving the mouse. Keep the lit color faint, fade the grid toward the edges with a mask, and make sure the text on top stays readable over lit cells.
Listen for pointermove once, on the container, and work out the cell from the coordinates, so hundreds of squares never need their own hover listeners. Set the lit cell’s opacity directly and let a CSS transition handle the fade, so moving the mouse never re-renders the grid. Touch screens get the still grid, and people who prefer reduced motion can get cells that switch off without a trail.
Often confused with
- Grid background
- A grid background is a still pattern of lines or dots. An interactive grid lights up the cells under the pointer and lets them fade.
- Flickering grid
- A flickering grid twinkles at random on its own, as an ambient loop. An interactive grid lights cells only where the pointer goes.
Related terms
All termsGrid background
A decorative background of thin grid lines, dots, stripes, or hexagons, often faded toward the edges, used behind hero sections and product shots.
Spotlight card
A card whose border and background light up with a soft radial glow that follows the mouse pointer, often across a whole grid of cards.
Custom cursor
A designed shape, such as a dot or a ring, that replaces or trails the mouse pointer and reacts to what it passes over.
Hover effect
A visual change, such as a lift, glow, or zoom, that happens when the mouse pointer moves over an element to show it can be clicked.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.