Skip to the content

Motion

confetti

A short burst of flat paper pieces in the chart colours from a point, falling and fading on a canvas over the page, started from code (the handle's fire) and never under reduced motion; it says nothing and takes no clicks.

Effects and 3D data

When to use it

Marking a rare success the person worked for: a first project published, a goal reached. Never for routine actions, never on page load. Say the success in words as well (a toast, a heading): the burst is decoration.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Effects and 3D data page.

Word rotate and pattern

Maps for teams, schools, or newsrooms

Animated beam

A map becomes a film.

Orbit

  • Maps
  • Charts
  • Films
  • Motion
Rivier

Confetti

Not yet published.

Point cloud

Ninety finds
#widthheightdepthkind
11.22.52.5Clay
23.22.73.2Stone
32.92.53.8Glass
40.30.64.3Clay
52.71.24.5Stone
642.54.4Glass
70.52.14.1Clay
81.63.13.5Stone
94.13.32.9Glass
101.61.32.1Clay
111.21.21.4Stone
123.11.80.9Glass
132.21.30.6Clay
142.12.70.5Stone
152.23.70.7Glass
161.62.11.2Clay
173.11.81.8Stone
182.61.82.5Glass
190.50.63.2Clay
2031.93.9Stone
213.73.44.3Glass
220.32.54.5Clay
231.92.74.4Stone
244.22.44.1Glass
251.20.63.5Clay
261.21.22.8Stone
273.42.52.1Glass
282.12.21.4Clay
291.73.10.9Stone
302.43.30.6Glass
311.91.20.5Clay
322.81.20.7Stone
332.41.81.2Glass
340.81.41.8Clay
353.12.82.5Stone
363.43.73.2Glass
370.22.13.9Clay
382.31.84.3Stone
394.21.84.5Glass
400.90.64.4Clay
411.324.1Stone
423.83.43.5Glass
4322.52.8Clay
441.52.62.1Stone
452.62.41.4Glass
462.10.60.9Clay
472.51.30.6Stone
482.22.60.5Glass
491.12.20.7Clay
503.23.11.2Stone
5133.21.8Glass
520.21.22.5Clay
532.61.23.3Stone
5441.93.9Glass
550.61.44.3Clay
561.52.84.5Stone
5743.74.4Glass
581.724.1Clay
591.31.73.5Stone
6031.72.8Glass
612.20.72.1Clay
622.221.4Stone
632.23.40.9Glass
641.52.50.6Clay
653.12.60.5Stone
662.72.30.7Glass
670.40.51.2Clay
682.91.31.8Stone
693.82.62.6Glass
700.32.23.3Clay
711.83.13.9Stone
724.23.24.3Glass
731.31.14.5Clay
741.21.14.4Stone
753.31.94.1Glass
762.21.53.5Clay
771.82.92.8Stone
782.33.72.1Glass
791.821.4Clay
802.91.70.9Stone
812.41.70.6Glass
820.70.70.5Clay
833.12.10.7Stone
843.53.51.2Glass
850.22.51.8Clay
862.22.62.6Stone
874.22.33.3Glass
8810.53.9Clay
891.31.34.3Stone
903.72.74.5Glass

Model annotations

  1. Lid — Hinged at the back.
  2. Side — A handle was here.
  3. Front — The maker's mark.

Add it

npx shadcn@latest add @rivier-ui/confetti

The registry is named once in components.json: see Installation.

Example

Import it from @/components/motion/confetti.

const confetti = useRef<ConfettiHandle>(null);
<Confetti handleRef={confetti} />
<Button onClick={(e) => confetti.current?.fire({ x: e.clientX, y: e.clientY })}>Publish</Button>

What it needs

A client component: it uses state, effects or browser behaviour. No packages beyond React.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/motion/confetti.tsx
"use client";
/**
 * @name confetti
 * @category motion
 * @description A short burst of flat paper pieces in the chart colours from a point, falling and fading on a canvas over the page, started from code (the handle's `fire`) and never under reduced motion; it says nothing and takes no clicks.
 * @when Marking a rare success the person worked for: a first project published, a goal reached. Never for routine actions, never on page load. Say the success in words as well (a toast, a heading): the burst is decoration.
 * @example
 * const confetti = useRef<ConfettiHandle>(null);
 * <Confetti handleRef={confetti} />
 * <Button onClick={(e) => confetti.current?.fire({ x: e.clientX, y: e.clientY })}>Publish</Button>
 */
import { useEffect, useImperativeHandle, useRef, type ComponentProps, type Ref } from "react";
import { cn } from "@/lib/cn";
import { useMotionSafe } from "@/lib/motion";
import { readThemeColor } from "@/lib/theme-color";
 
export type ConfettiHandle = {
  /** A burst from a point in the window (clientX, clientY); the middle of the window when left out. */
  fire: (from?: { x: number; y: number }) => void;
};
 
export type ConfettiProps = Omit<ComponentProps<"canvas">, "children"> & {
  handleRef?: Ref<ConfettiHandle>;
  /** Pieces in one burst. Default 120. */
  count?: number;
  /** Seconds a burst lasts. Default 1.8. */
  duration?: number;
};
 
type Piece = { x: number; y: number; vx: number; vy: number; turn: number; spin: number; w: number; h: number; color: string };
 
export function Confetti({ handleRef, count = 120, duration = 1.8, className, ...props }: ConfettiProps) {
  const canvas = useRef<HTMLCanvasElement>(null);
  const pieces = useRef<Piece[]>([]);
  const frame = useRef(0);
  const started = useRef(0);
  const safe = useMotionSafe();
  useEffect(() => () => cancelAnimationFrame(frame.current), []);
  useImperativeHandle(
    handleRef,
    () => ({
      fire(from) {
        const node = canvas.current;
        if (!safe || !node) return;
        const ratio = Math.min(2, window.devicePixelRatio || 1);
        node.width = window.innerWidth * ratio;
        node.height = window.innerHeight * ratio;
        const context = node.getContext("2d");
        if (!context) return;
        context.setTransform(ratio, 0, 0, ratio, 0, 0);
        const colors = ["--chart-1", "--chart-2", "--chart-3", "--chart-4", "--chart-5"].map((name) => readThemeColor(name, node));
        const x = from?.x ?? window.innerWidth / 2;
        const y = from?.y ?? window.innerHeight / 2;
        pieces.current = Array.from({ length: count }, (_, i) => {
          const angle = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 0.9;
          const speed = 6 + Math.random() * 8;
          return { x, y, vx: Math.cos(angle) * speed, vy: Math.sin(angle) * speed, turn: Math.random() * Math.PI, spin: (Math.random() - 0.5) * 0.4, w: 6 + Math.random() * 4, h: 3 + Math.random() * 3, color: colors[i % colors.length] ?? "currentColor" };
        });
        started.current = performance.now();
        cancelAnimationFrame(frame.current);
        const step = (now: number): void => {
          const age = (now - started.current) / 1000;
          context.clearRect(0, 0, window.innerWidth, window.innerHeight);
          if (age > duration) return;
          const fade = Math.max(0, 1 - age / duration);
          for (const p of pieces.current) {
            p.vy += 0.35;
            p.vx *= 0.985;
            p.x += p.vx;
            p.y += p.vy;
            p.turn += p.spin;
            context.save();
            context.globalAlpha = fade;
            context.translate(p.x, p.y);
            context.rotate(p.turn);
            context.fillStyle = p.color;
            context.fillRect(-p.w / 2, -p.h / 2, p.w, p.h);
            context.restore();
          }
          frame.current = requestAnimationFrame(step);
        };
        frame.current = requestAnimationFrame(step);
      },
    }),
    [safe, count, duration],
  );
  return <canvas ref={canvas} data-slot="confetti" aria-hidden="true" className={cn("pointer-events-none fixed inset-0 z-[70] size-full", className)} {...props} />;
}