Aurora background

A container that puts a slow, drifting field of blurred colour lights behind its children. Props set intensity (0 to 1), cycle speed in seconds, blur, the colours (your chart tokens by default) and the fade (edges, bottom or none). Pure CSS, no canvas, no client JavaScript; the drift only runs when the visitor allows motion.

BackgroundsComponentTailwind v4

Source

import type { CSSProperties, HTMLAttributes } from "react";
import { cn } from "@/lib/utils";

export type AuroraBackgroundProps = HTMLAttributes<HTMLDivElement> & {
  /** How strong the colour is, from 0 (off) to 1. */
  intensity?: number;
  /** Seconds for one slow drift cycle. Larger is calmer. */
  speed?: number;
  /** Up to five CSS colours. Defaults to the theme's chart tokens. */
  colors?: string[];
  /** Fade the colour out toward the edges, toward the bottom, or not at all. */
  fade?: "edges" | "bottom" | "none";
  /** Blur radius of the colour field in pixels. */
  blur?: number;
};

const DEFAULT_COLORS = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)", "var(--chart-5)"];

// Each light sits somewhere on the canvas and wanders around it on its own clock.
const LIGHTS = [
  { left: "-10%", top: "-20%", width: "60%", height: "75%", pace: 1, delay: 0 },
  { left: "45%", top: "-25%", width: "65%", height: "70%", pace: 1.35, delay: -6 },
  { left: "55%", top: "35%", width: "55%", height: "75%", pace: 1.15, delay: -11 },
  { left: "-5%", top: "40%", width: "55%", height: "70%", pace: 1.6, delay: -3 },
  { left: "25%", top: "10%", width: "50%", height: "60%", pace: 0.85, delay: -8 },
];

const FADES = {
  edges: "radial-gradient(ellipse 85% 80% at 50% 45%, #000 40%, transparent 100%)",
  bottom: "linear-gradient(to bottom, #000 35%, transparent 100%)",
  none: "none",
};

const KEYFRAMES = `@keyframes chm-aurora-wander{0%{transform:translate3d(0,0,0) scale(1) rotate(0deg)}33%{transform:translate3d(12%,-8%,0) scale(1.12) rotate(14deg)}66%{transform:translate3d(-10%,10%,0) scale(.92) rotate(-10deg)}100%{transform:translate3d(0,0,0) scale(1) rotate(0deg)}}`;

export function AuroraBackground({
  intensity = 0.5,
  speed = 22,
  colors = DEFAULT_COLORS,
  fade = "edges",
  blur = 72,
  className,
  style,
  children,
  ...props
}: AuroraBackgroundProps) {
  const palette = colors.length ? colors : DEFAULT_COLORS;
  const strength = Math.min(Math.max(intensity, 0), 1);
  return (
    <div
      className={cn("relative isolate overflow-hidden bg-background", className)}
      style={{ "--chm-aurora-speed": `${speed}s`, ...style } as CSSProperties}
      {...props}
    >
      <style href="chm-aurora-background" precedence="default">{KEYFRAMES}</style>
      <div
        aria-hidden="true"
        className="pointer-events-none absolute inset-0 -z-10"
        style={{ maskImage: FADES[fade], WebkitMaskImage: FADES[fade], opacity: strength }}
      >
        <div className="absolute inset-0" style={{ filter: `blur(${blur}px) saturate(1.4)` }}>
          {LIGHTS.map((light, index) => (
            <div
              key={index}
              className="absolute rounded-full motion-safe:animate-[chm-aurora-wander_var(--chm-aurora-duration)_ease-in-out_infinite] [will-change:transform]"
              style={{
                left: light.left,
                top: light.top,
                width: light.width,
                height: light.height,
                background: `radial-gradient(closest-side, ${palette[index % palette.length]}, transparent)`,
                animationDelay: `calc(var(--chm-aurora-speed) * ${light.delay / 20})`,
                "--chm-aurora-duration": `calc(var(--chm-aurora-speed) * ${light.pace})`,
                animationDirection: index % 2 ? "reverse" : "normal",
              } as CSSProperties}
            />
          ))}
        </div>
      </div>
      {children}
    </div>
  );
}