Grid with light beams
A faint grid that fades at the edges, with short pulses of light running along its lines and jumping to a new line each time.
"use client";
import {
useEffect,
useRef,
type AnimationEvent,
type CSSProperties,
type ReactNode,
} from "react";
import "./grid-beams.css";
type GridBeamsProps = {
children?: ReactNode;
/** Grid cell size in pixels. Beams always travel on the lines. */
cell?: number;
/** How many beams run at once. */
beams?: number;
/** Beam color. */
color?: string;
className?: string;
style?: CSSProperties;
};
/**
* GridBeams
* A faint grid with short pulses of light that run along its lines. Each
* time a pulse finishes it jumps to a new random line and direction, so
* the pattern never loops visibly.
*/
export function GridBeams({
children,
cell = 48,
beams = 6,
color = "#ff5a1f",
className = "",
style,
}: GridBeamsProps) {
const ref = useRef<HTMLDivElement>(null);
// Put a beam on a random grid line, running across or down.
const place = (beam: HTMLElement) => {
const box = ref.current;
if (!box) return;
const across = Math.random() < 0.55;
const lines = Math.floor(
(across ? box.clientHeight : box.clientWidth) / cell,
);
const at = (1 + Math.floor(Math.random() * Math.max(1, lines - 1))) * cell;
beam.dataset.dir = across ? "x" : "y";
beam.style.setProperty("--at", `${at}px`);
beam.style.setProperty("--dur", `${2.2 + Math.random() * 2.2}s`);
};
useEffect(() => {
const box = ref.current;
if (!box) return;
const size = () => {
box.style.setProperty("--w", `${box.clientWidth}px`);
box.style.setProperty("--h", `${box.clientHeight}px`);
};
size();
box.querySelectorAll<HTMLElement>(".gu-beams__beam").forEach((b, i) => {
place(b);
b.style.animationDelay = `${i * 0.6}s`;
});
box.dataset.ready = "true";
const ro = new ResizeObserver(size);
ro.observe(box);
const io = new IntersectionObserver(([e]) => {
box.dataset.paused = String(!e.isIntersecting);
});
io.observe(box);
return () => {
ro.disconnect();
io.disconnect();
};
// place only reads the ref and cell.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cell, beams]);
const onLoop = (e: AnimationEvent<HTMLSpanElement>) => {
e.currentTarget.style.animationDelay = "0s";
place(e.currentTarget);
};
return (
<div
ref={ref}
className={`gu-beams ${className}`}
style={
{ "--cell": `${cell}px`, "--beam": color, ...style } as CSSProperties
}
>
<div className="gu-beams__layer" aria-hidden="true">
{Array.from({ length: beams }, (_, i) => (
<span
key={i}
className="gu-beams__beam"
onAnimationIteration={onLoop}
/>
))}
</div>
<div className="gu-beams__content">{children}</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Grid with light beams
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A faint grid that fades out toward the edges, with short pulses of light running along its lines. Each time a pulse finishes, it jumps to a new random line and direction, so the pattern never loops visibly.
## Behavior and motion
- The grid is two 1px linear gradients at the `cell` size (default 48px), color: the text color at 12%, masked by an ellipse (75% × 70%) so it fades out at the edges.
- Each beam is a 120px × 1px streak (or 1px × 120px when vertical) with a transparent-to-color gradient and a 6px drop-shadow glow in the beam color.
- On start and on every animationiteration, a beam picks a direction (55% across, 45% down), a random grid line (a multiple of `cell`, so it always sits exactly on a line) and a random duration of 2.2–4.4s. It travels from just outside one edge to the other (the box width and height are kept in CSS variables by a ResizeObserver). Beams start 0.6s apart.
- An IntersectionObserver pauses the beams while the box is off screen.
## Look
- Demo: a 400px hero, "Deploy on a Friday." centered at up to 4.6rem, a short line at 75% opacity and a solid pill button. Dark mode: ink background with orange beams. Light mode: warm white #fbf8f2 with ultramarine beams.
## Accessibility
- The grid and beams are one aria-hidden layer behind the content.
- prefers-reduced-motion: the grid stays and the beams are switched off.
## Props
children, cell (px), beams (how many at once), color, className, style.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### GridBeams.tsx
```tsx
"use client";
import {
useEffect,
useRef,
type AnimationEvent,
type CSSProperties,
type ReactNode,
} from "react";
import "./grid-beams.css";
type GridBeamsProps = {
children?: ReactNode;
/** Grid cell size in pixels. Beams always travel on the lines. */
cell?: number;
/** How many beams run at once. */
beams?: number;
/** Beam color. */
color?: string;
className?: string;
style?: CSSProperties;
};
/**
* GridBeams
* A faint grid with short pulses of light that run along its lines. Each
* time a pulse finishes it jumps to a new random line and direction, so
* the pattern never loops visibly.
*/
export function GridBeams({
children,
cell = 48,
beams = 6,
color = "#ff5a1f",
className = "",
style,
}: GridBeamsProps) {
const ref = useRef<HTMLDivElement>(null);
// Put a beam on a random grid line, running across or down.
const place = (beam: HTMLElement) => {
const box = ref.current;
if (!box) return;
const across = Math.random() < 0.55;
const lines = Math.floor(
(across ? box.clientHeight : box.clientWidth) / cell,
);
const at = (1 + Math.floor(Math.random() * Math.max(1, lines - 1))) * cell;
beam.dataset.dir = across ? "x" : "y";
beam.style.setProperty("--at", `${at}px`);
beam.style.setProperty("--dur", `${2.2 + Math.random() * 2.2}s`);
};
useEffect(() => {
const box = ref.current;
if (!box) return;
const size = () => {
box.style.setProperty("--w", `${box.clientWidth}px`);
box.style.setProperty("--h", `${box.clientHeight}px`);
};
size();
box.querySelectorAll<HTMLElement>(".gu-beams__beam").forEach((b, i) => {
place(b);
b.style.animationDelay = `${i * 0.6}s`;
});
box.dataset.ready = "true";
const ro = new ResizeObserver(size);
ro.observe(box);
const io = new IntersectionObserver(([e]) => {
box.dataset.paused = String(!e.isIntersecting);
});
io.observe(box);
return () => {
ro.disconnect();
io.disconnect();
};
// place only reads the ref and cell.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cell, beams]);
const onLoop = (e: AnimationEvent<HTMLSpanElement>) => {
e.currentTarget.style.animationDelay = "0s";
place(e.currentTarget);
};
return (
<div
ref={ref}
className={`gu-beams ${className}`}
style={
{ "--cell": `${cell}px`, "--beam": color, ...style } as CSSProperties
}
>
<div className="gu-beams__layer" aria-hidden="true">
{Array.from({ length: beams }, (_, i) => (
<span
key={i}
className="gu-beams__beam"
onAnimationIteration={onLoop}
/>
))}
</div>
<div className="gu-beams__content">{children}</div>
</div>
);
}
```
### grid-beams.css
```css
.gu-beams {
--line: color-mix(in srgb, currentColor 12%, transparent);
position: relative;
isolation: isolate;
overflow: hidden;
}
/* The grid itself, fading out toward the edges. */
.gu-beams__layer {
position: absolute;
inset: 0;
z-index: -1;
background-image:
linear-gradient(var(--line) 1px, transparent 1px),
linear-gradient(90deg, var(--line) 1px, transparent 1px);
background-size: var(--cell) var(--cell);
-webkit-mask-image: radial-gradient(
ellipse 75% 70% at 50% 45%,
#000 40%,
transparent 100%
);
mask-image: radial-gradient(
ellipse 75% 70% at 50% 45%,
#000 40%,
transparent 100%
);
}
/* A beam is a short bright streak with a glowing head. */
.gu-beams__beam {
position: absolute;
left: 0;
top: 0;
opacity: 0;
animation: var(--dur, 3s) linear infinite both;
filter: drop-shadow(0 0 6px var(--beam));
}
.gu-beams[data-ready="true"] .gu-beams__beam {
opacity: 1;
}
.gu-beams__beam[data-dir="x"] {
top: var(--at);
width: 120px;
height: 1px;
margin-top: 0;
background: linear-gradient(90deg, transparent, var(--beam));
animation-name: gu-beams-across;
}
.gu-beams__beam[data-dir="y"] {
left: var(--at);
width: 1px;
height: 120px;
background: linear-gradient(180deg, transparent, var(--beam));
animation-name: gu-beams-down;
}
@keyframes gu-beams-across {
from {
transform: translateX(-120px);
}
to {
transform: translateX(var(--w, 800px));
}
}
@keyframes gu-beams-down {
from {
transform: translateY(-120px);
}
to {
transform: translateY(var(--h, 600px));
}
}
.gu-beams[data-paused="true"] .gu-beams__beam {
animation-play-state: paused;
}
.gu-beams__content {
position: relative;
}
/* Reduced motion: the grid stays, the beams are switched off. */
@media (prefers-reduced-motion: reduce) {
.gu-beams__beam {
display: none;
}
}
```
You might also want
Next piece: Habit app landing pageAurora background
Animations, beginner
Dot ripple field
Animations, intermediate
Letter scatter headline
Animations, beginner