Skip to content

Confetti

Feedback

A bounded decorative celebration using the native Web Animations API.

User-triggered launch celebrationRepeatable burstsPanel-scoped particlesReduced-motion fallback
Open preview

Responsive viewport — resize the window to explore. Direction and density apply only to this preview. Theme applies to the docs too.

Usage

Wrap a success card or panel and increment burstKey when a user completes an action. The component animates 36 deterministic decorative particles within that panel. It does not signal business success by itself: keep a real heading/status/action in children. New bursts cancel previous animations. Without Web Animations support the panel still works, and no runtime dependency is needed. No animation runs for an initial zero key.

Import
import { Confetti } from "@/components/ui/confetti";

Shipped as editable source in src/components/ui/confetti.tsx in both apps. Copy the implementation below when moving it to another project. Copy the complete example from Source code for all imports and state. Add "use client" when using hooks or event handlers. See installation and theming for project setup.

Component source

The complete implementation is included in both apps. It uses the React, Radix, Lucide, and shared-core dependencies already installed in Apex. Keep your project’s semantic theme tokens when reusing it.

src/components/ui/confetti.tsx
"use client";
import { useEffect, useRef, type ReactNode } from "react";
import { cn } from "@dashboardpack/core/lib/utils";
export interface ConfettiProps {
  burstKey: number;
  children: ReactNode;
  duration?: number;
  className?: string;
}
export function Confetti({
  burstKey,
  children,
  duration = 1800,
  className,
}: ConfettiProps) {
  const root = useRef<HTMLDivElement>(null);
  useEffect(() => {
    if (
      !burstKey ||
      !root.current ||
      matchMedia("(prefers-reduced-motion: reduce)").matches
    )
      return;
    const bounds = root.current.getBoundingClientRect();
    const animations = Array.from(
      root.current.querySelectorAll<HTMLElement>("[data-confetti]"),
    ).flatMap((particle, index) => {
      if (!particle.animate) return [];
      const angle = (index / 36) * Math.PI * 2;
      const distance = 50 + (index % 7) * 18;
      return [
        particle.animate(
          [
            {
              transform: "translate(0, 0) rotate(0deg)",
              opacity: 1,
              offset: 0,
            },
            {
              transform: `translate(${Math.cos(angle) * distance}px, ${-Math.abs(Math.sin(angle)) * distance - 40}px) rotate(${index * 31}deg)`,
              opacity: 1,
              offset: 0.45,
            },
            {
              transform: `translate(${Math.cos(angle) * distance * 1.5}px, ${bounds.height * 0.65}px) rotate(${index * 67}deg)`,
              opacity: 0,
              offset: 1,
            },
          ],
          {
            duration: Math.max(600, Math.min(4000, duration)),
            easing: "cubic-bezier(.2,.6,.5,1)",
          },
        ),
      ];
    });
    return () => animations.forEach((animation) => animation.cancel());
  }, [burstKey, duration]);
  return (
    <div
      ref={root}
      className={cn("relative isolate overflow-hidden rounded-xl", className)}
    >
      {children}
      <div
        aria-hidden
        className="pointer-events-none absolute inset-0 overflow-hidden"
      >
        {Array.from({ length: 36 }, (_, index) => (
          <span
            key={index}
            data-confetti
            style={{
              insetInlineStart: "50%",
              top: "35%",
              backgroundColor: [
                "var(--primary)",
                "var(--success)",
                "var(--warning)",
                "var(--muted-foreground)",
              ][index % 4],
            }}
            className="absolute h-2 w-1.5 rounded-sm opacity-0"
          />
        ))}
      </div>
    </div>
  );
}

API reference

Common props and defaults. Slot-specific props are named below; native elements also accept their standard HTML attributes. The component TypeScript definitions are the complete type reference.

Confetti props
PropType / DefaultDescription
burstKeynumber

Default: required

Change a nonzero key to trigger a new burst.
childrenReactNode

Default: required

Actual card content, actions, and success feedback.
durationnumber

Default: 1800

Animation milliseconds, bounded to 600–4000.
classNamestring

Default: —

Extend the container with semantic theme utilities.

Build with these too