All files / confetti/src Confetti.tsx

96.07% Statements 49/51
90.62% Branches 29/32
85.71% Functions 12/14
97.72% Lines 43/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                                                      2x                                                                                                                                                                                                                                               44x   44x 44x 19x     44x 44x 44x 44x 44x 44x             44x       44x 33x 33x 17x 17x     17x 21x   17x 17x 5x   17x 17x 17x 17x         44x 21x   6x   1x   1x     1x         44x     44x 16x 16x 16x 1x     44x                       44x                 44x 37x 20x    
/**
 * `<Confetti />` — drop-in confetti canvas: a portal full-viewport overlay
 * by default, or an inline canvas filling its parent.
 */
import {
  useEffect,
  useImperativeHandle,
  useMemo,
  useRef,
  useState,
  type CSSProperties,
  type ReactNode,
  type Ref,
} from "react";
import { createPortal } from "react-dom";
import { useIsomorphicLayoutEffect } from "@usefy/use-isomorphic-layout-effect";
import { useLatest } from "@usefy/use-latest";
import { usePageVisibility } from "@usefy/use-page-visibility";
import { useReducedMotion } from "@usefy/use-reduced-motion";
import { createConfettiEngine } from "./engine/createEngine";
import type {
  ConfettiEngine,
  EmitHandle,
  EmitOptions,
  FireOptions,
} from "./types";
 
const NOOP_EMIT_HANDLE: EmitHandle = {
  stop() {
    /* no-op */
  },
  get active() {
    return false;
  },
};
 
/**
 * Imperative handle exposed through {@link ConfettiProps.controllerRef} —
 * mirrors the spotlight-tour `TourController` ergonomics. The controller
 * object is referentially stable for the component's lifetime; calls made
 * before the canvas has mounted are safe no-ops (`fire` resolves
 * immediately, `emit` returns an inert handle).
 *
 * @example
 * ```tsx
 * const controller = useRef<ConfettiController>(null);
 * <Confetti controllerRef={controller} />
 * <button onClick={() => controller.current?.fire()}>Party</button>
 * ```
 */
export interface ConfettiController {
  /** Fire one burst; resolves when that burst's particles have all died. */
  fire(opts?: FireOptions): Promise<void>;
  /** Start a continuous emitter (snow, rain, cannons…). */
  emit(opts?: EmitOptions): EmitHandle;
  /** Stop all emitters; live particles finish naturally. */
  stop(): void;
  /** Kill everything and wipe the canvas now. */
  clear(): void;
}
 
/**
 * Props for {@link Confetti}.
 *
 * @example
 * ```tsx
 * <Confetti
 *   controllerRef={controller}
 *   zIndex={1200}
 *   onComplete={() => console.log("settled")}
 * />
 * ```
 */
export interface ConfettiProps {
  /**
   * `"overlay"` (default): a portal fixed full-viewport canvas appended to
   * `document.body` after hydration. `"inline"`: the canvas renders in
   * place and fills its parent (give the parent `position: relative`).
   */
  variant?: "overlay" | "inline";
  /** Stacking order of the overlay canvas. Default `1100`. */
  zIndex?: number;
  /** Receives the imperative {@link ConfettiController}. */
  controllerRef?: Ref<ConfettiController>;
  /**
   * Called each time the engine goes idle (all particles dead, no emitter).
   * Not called when the component unmounts mid-animation.
   */
  onComplete?: () => void;
  /**
   * Fire one burst right after mount — `true` for defaults, or a
   * {@link FireOptions} object. Respects reduced motion (no burst when the
   * user prefers reduced motion and `reducedMotion` is `"respect"`).
   */
  fireOnMount?: boolean | FireOptions;
  /**
   * `"respect"` (default): `prefers-reduced-motion: reduce` turns every
   * fire/emit into a resolved no-op. `"ignore"`: animate anyway.
   */
  reducedMotion?: "respect" | "ignore";
  /** Extra class for the canvas element. */
  className?: string;
  /** Extra inline styles, merged over the built-in canvas styles. */
  style?: CSSProperties;
}
 
/**
 * Confetti canvas component.
 *
 * - **SSR-safe**: the overlay renders nothing until after hydration; the
 *   engine only exists client-side.
 * - **Never blocks the UI**: the canvas is `aria-hidden` with
 *   `pointer-events: none`.
 * - **Zero React renders during animation**: all motion happens inside the
 *   imperative engine; React is only involved on mount/unmount.
 * - Pauses automatically while the page is hidden.
 * - StrictMode-safe: the dev double-mount leaves exactly one canvas and no
 *   orphaned rAF/observers.
 *
 * @example
 * ```tsx
 * import { Confetti, type ConfettiController } from "@usefy/confetti";
 * import { useRef } from "react";
 *
 * function CheckoutSuccess() {
 *   const confetti = useRef<ConfettiController>(null);
 *   return (
 *     <>
 *       <Confetti controllerRef={confetti} />
 *       <button onClick={() => confetti.current?.fire({ origin: { y: 0.7 } })}>
 *         Complete purchase
 *       </button>
 *     </>
 *   );
 * }
 * ```
 */
export function Confetti(props: ConfettiProps): ReactNode {
  const {
    variant = "overlay",
    zIndex = 1100,
    controllerRef,
    onComplete,
    fireOnMount = false,
    reducedMotion = "respect",
    className,
    style,
  } = props;
 
  const [mounted, setMounted] = useState(false);
  useEffect(() => {
    setMounted(true);
  }, []);
 
  const canvasElRef = useRef<HTMLCanvasElement | null>(null);
  const engineRef = useRef<ConfettiEngine | null>(null);
  const onCompleteRef = useLatest(onComplete);
  const fireOnMountRef = useLatest(fireOnMount);
  const prefersReduced = useReducedMotion();
  const skipMotionRef = useLatest(reducedMotion === "respect" && prefersReduced);
 
  // When can the canvas actually exist? Inline renders it from the very
  // first render; overlay only after the mount gate opens the portal.
  // Depending on this (rather than `mounted` directly) keeps the inline
  // variant from destroying+recreating a perfectly good engine when the
  // mount flag flips — which would also double-fire `fireOnMount`.
  const canvasReady = variant === "inline" || mounted;
 
  // Engine lifecycle — (re)created whenever the canvas element can change
  // (readiness gate, variant switch) or the engine option changes.
  useIsomorphicLayoutEffect(() => {
    const canvas = canvasElRef.current;
    if (!canvas) return;
    const engine = createConfettiEngine(canvas, { reducedMotion });
    engineRef.current = engine;
    // Idle-edge subscription (never per-frame). Unsubscribed BEFORE destroy
    // in cleanup so unmounting mid-animation does not report completion.
    const unsubscribe = engine.onActiveChange((active) => {
      if (!active) onCompleteRef.current?.();
    });
    const fom = fireOnMountRef.current;
    if (fom && !skipMotionRef.current) {
      void engine.fire(typeof fom === "object" ? fom : undefined);
    }
    return () => {
      unsubscribe();
      engine.destroy();
      engineRef.current = null;
    };
  }, [canvasReady, variant, reducedMotion]);
 
  // Stable imperative controller (safe no-ops before the engine exists).
  const controller = useMemo<ConfettiController>(
    () => ({
      fire: (opts?: FireOptions) =>
        engineRef.current ? engineRef.current.fire(opts) : Promise.resolve(),
      emit: (opts?: EmitOptions) =>
        engineRef.current ? engineRef.current.emit(opts) : NOOP_EMIT_HANDLE,
      stop: () => {
        engineRef.current?.stop();
      },
      clear: () => {
        engineRef.current?.clear();
      },
    }),
    [],
  );
  useImperativeHandle(controllerRef, () => controller, [controller]);
 
  // Pause while the tab is hidden — no burst "wall" on return (SPEC §2.1).
  usePageVisibility((visible) => {
    const engine = engineRef.current;
    Iif (!engine) return;
    if (visible) engine.resume();
    else engine.pause();
  });
 
  const canvasStyle: CSSProperties = {
    position: variant === "overlay" ? "fixed" : "absolute",
    top: 0,
    left: 0,
    width: "100%",
    height: "100%",
    pointerEvents: "none",
    zIndex,
    ...style,
  };
 
  const canvas = (
    <canvas
      ref={canvasElRef}
      aria-hidden="true"
      data-usefy-confetti={variant}
      className={className}
      style={canvasStyle}
    />
  );
 
  if (variant === "inline") return canvas;
  if (!mounted) return null; // SSR / first client render: nothing yet
  return createPortal(canvas, document.body);
}