{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-confetti",
  "title": "Confetti",
  "description": "A canvas burst of confetti from a button or a function call, in the brand colours and with no package.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/theme.json"
  ],
  "files": [
    {
      "path": "components/core/confetti.tsx",
      "content": "'use client';\n\nimport { Button, type ButtonProps } from '@/registry/ndeko/ui/button';\n\nexport type ConfettiShape = 'square' | 'circle' | 'ribbon';\n\nexport type ConfettiOrigin = { x: number; y: number };\n\nexport type ConfettiOptions = {\n  particleCount?: number;\n  origin?: ConfettiOrigin | Element;\n  angle?: number;\n  spread?: number;\n  velocity?: number;\n  decay?: number;\n  gravity?: number;\n  drift?: number;\n  duration?: number;\n  colors?: string[];\n  shapes?: ConfettiShape[];\n  scalar?: number;\n};\n\ntype Burst = {\n  remaining: number;\n  resolve: () => void;\n};\n\ntype Particle = {\n  burst: Burst;\n  x: number;\n  y: number;\n  speedX: number;\n  speedY: number;\n  fall: number;\n  drift: number;\n  decay: number;\n  rotation: number;\n  spin: number;\n  flip: number;\n  flipSpeed: number;\n  wobble: number;\n  wobbleSpeed: number;\n  size: number;\n  color: string;\n  shape: ConfettiShape;\n  opacity: number;\n  age: number;\n  lifetime: number;\n  isStill: boolean;\n};\n\ntype Stage = {\n  canvas: HTMLCanvasElement;\n  context: CanvasRenderingContext2D;\n  particles: Particle[];\n  frame: number;\n  lastTime: number;\n  pixelRatio: number;\n  resize: () => void;\n};\n\nconst FRAME_MS = 1000 / 60;\n\nconst MAX_STEP_MS = 50;\n\nconst FADE_SHARE = 0.35;\n\nconst STILL_LIFETIME_MS = 900;\n\nconst STILL_SCATTER = 80;\n\nconst MAX_PIXEL_RATIO = 2;\n\nconst DEFAULT_COLORS = [\n  'var(--primary)',\n  'var(--primary-ink)',\n  'var(--muted-foreground)',\n];\n\nconst DEFAULT_SHAPES: ConfettiShape[] = ['ribbon', 'square', 'circle'];\n\nconst DEFAULTS = {\n  particleCount: 60,\n  angle: 90,\n  spread: 60,\n  velocity: 32,\n  decay: 0.9,\n  gravity: 1,\n  drift: 0,\n  duration: 3000,\n  scalar: 1,\n};\n\nlet stage: Stage | null = null;\n\nfunction randomBetween(min: number, max: number) {\n  const span = max - min;\n  return min + Math.random() * span;\n}\n\nfunction pickFrom<Item>(items: Item[]) {\n  const index = Math.floor(Math.random() * items.length);\n  return items[index];\n}\n\nfunction prefersReducedMotion() {\n  const query = window.matchMedia('(prefers-reduced-motion: reduce)');\n  return query.matches;\n}\n\nconst TOKEN_PATTERN = /^var\\((--[\\w-]+)\\)$/;\n\n/** Canvas cannot read CSS variables, so `var(--token)` colours are resolved against the document first. */\nfunction resolveColor(color: string) {\n  const match = TOKEN_PATTERN.exec(color.trim());\n  if (!match) {\n    return color;\n  }\n  const styles = getComputedStyle(document.documentElement);\n  const value = styles.getPropertyValue(match[1]).trim();\n  if (!value) {\n    return 'currentColor';\n  }\n  return value;\n}\n\nfunction originPointOf(origin: ConfettiOrigin | Element | undefined) {\n  const width = window.innerWidth;\n  const height = window.innerHeight;\n  if (!origin) {\n    return { x: width / 2, y: height / 2 };\n  }\n  if (origin instanceof Element) {\n    const box = origin.getBoundingClientRect();\n    const centreX = box.left + box.width / 2;\n    const centreY = box.top + box.height / 2;\n    return { x: centreX, y: centreY };\n  }\n  return { x: origin.x * width, y: origin.y * height };\n}\n\nfunction createStage(): Stage | null {\n  const canvas = document.createElement('canvas');\n  const context = canvas.getContext('2d');\n  if (!context) {\n    return null;\n  }\n  canvas.setAttribute('aria-hidden', 'true');\n  canvas.style.cssText =\n    'position:fixed;inset:0;width:100%;height:100%;pointer-events:none;z-index:100';\n  document.body.append(canvas);\n  const created: Stage = {\n    canvas,\n    context,\n    particles: [],\n    frame: 0,\n    lastTime: 0,\n    pixelRatio: 1,\n    resize: () => {\n      const viewport = document.documentElement;\n      const screenRatio = window.devicePixelRatio || 1;\n      created.pixelRatio = Math.min(screenRatio, MAX_PIXEL_RATIO);\n      canvas.width = viewport.clientWidth * created.pixelRatio;\n      canvas.height = viewport.clientHeight * created.pixelRatio;\n    },\n  };\n  created.resize();\n  window.addEventListener('resize', created.resize);\n  return created;\n}\n\nfunction removeStage(current: Stage) {\n  window.removeEventListener('resize', current.resize);\n  cancelAnimationFrame(current.frame);\n  current.canvas.remove();\n  stage = null;\n}\n\nfunction retire(particle: Particle) {\n  particle.burst.remaining -= 1;\n  if (particle.burst.remaining === 0) {\n    particle.burst.resolve();\n  }\n}\n\nfunction moveParticle(particle: Particle, steps: number) {\n  const decayOverSteps = particle.decay ** steps;\n  particle.speedX *= decayOverSteps;\n  particle.speedY *= decayOverSteps;\n  particle.wobble += particle.wobbleSpeed * steps;\n  particle.flip += particle.flipSpeed * steps;\n  particle.rotation += particle.spin * steps;\n  const sway = Math.sin(particle.wobble) * 0.6;\n  particle.x += (particle.speedX + particle.drift + sway) * steps;\n  particle.y += (particle.speedY + particle.fall) * steps;\n}\n\nfunction fadeOf(particle: Particle) {\n  const progress = particle.age / particle.lifetime;\n  if (particle.isStill) {\n    const arc = progress * Math.PI;\n    return Math.sin(arc);\n  }\n  const fadeStart = 1 - FADE_SHARE;\n  if (progress < fadeStart) {\n    return 1;\n  }\n  const fadeProgress = (progress - fadeStart) / FADE_SHARE;\n  return 1 - fadeProgress;\n}\n\nfunction drawShape(context: CanvasRenderingContext2D, particle: Particle) {\n  const size = particle.size;\n  const half = size / 2;\n  if (particle.shape === 'circle') {\n    context.beginPath();\n    context.arc(0, 0, half, 0, Math.PI * 2);\n    context.fill();\n    return;\n  }\n  if (particle.shape === 'ribbon') {\n    context.fillRect(-size, -half / 2, size * 2, half);\n    return;\n  }\n  context.fillRect(-half, -half, size, size);\n}\n\nfunction drawParticle(context: CanvasRenderingContext2D, particle: Particle) {\n  const fade = fadeOf(particle);\n  const turn = Math.cos(particle.flip);\n  context.save();\n  context.globalAlpha = particle.opacity * fade;\n  context.fillStyle = particle.color;\n  context.translate(particle.x, particle.y);\n  context.rotate(particle.rotation);\n  context.scale(1, turn);\n  drawShape(context, particle);\n  context.restore();\n}\n\nfunction tick(time: number) {\n  const current = stage;\n  if (!current) {\n    return;\n  }\n  const sinceLastFrame = time - current.lastTime;\n  const positiveGap = Math.max(sinceLastFrame, 0);\n  const elapsed = Math.min(positiveGap, MAX_STEP_MS);\n  const steps = elapsed / FRAME_MS;\n  current.lastTime = time;\n  const { context, canvas, pixelRatio } = current;\n  context.setTransform(1, 0, 0, 1, 0, 0);\n  context.clearRect(0, 0, canvas.width, canvas.height);\n  context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);\n  const alive: Particle[] = [];\n  for (const particle of current.particles) {\n    particle.age += elapsed;\n    const isDone = particle.age >= particle.lifetime;\n    if (isDone) {\n      retire(particle);\n      continue;\n    }\n    if (!particle.isStill) {\n      moveParticle(particle, steps);\n    }\n    drawParticle(context, particle);\n    alive.push(particle);\n  }\n  current.particles = alive;\n  if (alive.length === 0) {\n    removeStage(current);\n    return;\n  }\n  current.frame = requestAnimationFrame(tick);\n}\n\nfunction ensureRunning(current: Stage) {\n  if (current.frame) {\n    return;\n  }\n  current.lastTime = performance.now();\n  current.frame = requestAnimationFrame(tick);\n}\n\ntype Settings = typeof DEFAULTS & {\n  colors: string[];\n  shapes: ConfettiShape[];\n};\n\ntype Point = { x: number; y: number };\n\nfunction settingsOf(options: ConfettiOptions): Settings {\n  return {\n    particleCount: options.particleCount ?? DEFAULTS.particleCount,\n    angle: options.angle ?? DEFAULTS.angle,\n    spread: options.spread ?? DEFAULTS.spread,\n    velocity: options.velocity ?? DEFAULTS.velocity,\n    decay: options.decay ?? DEFAULTS.decay,\n    gravity: options.gravity ?? DEFAULTS.gravity,\n    drift: options.drift ?? DEFAULTS.drift,\n    duration: options.duration ?? DEFAULTS.duration,\n    scalar: options.scalar ?? DEFAULTS.scalar,\n    colors: options.colors ?? DEFAULT_COLORS,\n    shapes: options.shapes ?? DEFAULT_SHAPES,\n  };\n}\n\nfunction flyingParticle(\n  burst: Burst,\n  start: Point,\n  settings: Settings,\n  colors: string[]\n): Particle {\n  const halfSpread = settings.spread / 2;\n  const deviation = randomBetween(-halfSpread, halfSpread);\n  const launchDegrees = settings.angle + deviation;\n  const launch = (launchDegrees * Math.PI) / 180;\n  const speedShare = randomBetween(0.45, 1);\n  const speed = settings.velocity * speedShare;\n  const lifetimeJitter = randomBetween(0.8, 1.1);\n  const speedX = Math.cos(launch) * speed;\n  const speedY = -Math.sin(launch) * speed;\n  const baseSize = randomBetween(5, 9);\n  return {\n    burst,\n    x: start.x,\n    y: start.y,\n    speedX,\n    speedY,\n    fall: settings.gravity * 2.4,\n    drift: settings.drift,\n    decay: settings.decay,\n    rotation: randomBetween(0, Math.PI * 2),\n    spin: randomBetween(-0.12, 0.12),\n    flip: randomBetween(0, Math.PI * 2),\n    flipSpeed: randomBetween(0.08, 0.2),\n    wobble: randomBetween(0, Math.PI * 2),\n    wobbleSpeed: randomBetween(0.05, 0.12),\n    size: baseSize * settings.scalar,\n    color: pickFrom(colors),\n    shape: pickFrom(settings.shapes),\n    opacity: randomBetween(0.75, 1),\n    age: 0,\n    lifetime: settings.duration * lifetimeJitter,\n    isStill: false,\n  };\n}\n\nfunction stillParticle(\n  burst: Burst,\n  start: Point,\n  settings: Settings,\n  colors: string[]\n): Particle {\n  const direction = randomBetween(0, Math.PI * 2);\n  const distance = randomBetween(8, STILL_SCATTER);\n  const offsetX = Math.cos(direction) * distance;\n  const offsetY = Math.sin(direction) * distance;\n  const baseSize = randomBetween(5, 8);\n  return {\n    burst,\n    x: start.x + offsetX,\n    y: start.y + offsetY,\n    speedX: 0,\n    speedY: 0,\n    fall: 0,\n    drift: 0,\n    decay: 1,\n    rotation: randomBetween(0, Math.PI * 2),\n    spin: 0,\n    flip: 0,\n    flipSpeed: 0,\n    wobble: 0,\n    wobbleSpeed: 0,\n    size: baseSize * settings.scalar,\n    color: pickFrom(colors),\n    shape: pickFrom(settings.shapes),\n    opacity: randomBetween(0.6, 0.9),\n    age: 0,\n    lifetime: STILL_LIFETIME_MS,\n    isStill: true,\n  };\n}\n\nconst STILL_SHARE = 0.4;\n\nfunction createParticle(\n  isStill: boolean,\n  burst: Burst,\n  start: Point,\n  settings: Settings,\n  colors: string[]\n) {\n  if (isStill) {\n    return stillParticle(burst, start, settings, colors);\n  }\n  return flyingParticle(burst, start, settings, colors);\n}\n\nfunction particleCountOf(settings: Settings, isStill: boolean) {\n  if (isStill) {\n    const fewer = settings.particleCount * STILL_SHARE;\n    return Math.round(fewer);\n  }\n  return settings.particleCount;\n}\n\n/**\n * Throws one burst of confetti from a point of the viewport or from the centre of an element, and\n * resolves once its last piece has gone. With reduced motion, a few pieces fade in place instead.\n */\nexport function confetti(options: ConfettiOptions = {}) {\n  return new Promise<void>((resolve) => {\n    const isServer = typeof window === 'undefined';\n    if (isServer) {\n      resolve();\n      return;\n    }\n    const settings = settingsOf(options);\n    const isStill = prefersReducedMotion();\n    const count = particleCountOf(settings, isStill);\n    if (count <= 0) {\n      resolve();\n      return;\n    }\n    const current = stage ?? createStage();\n    if (!current) {\n      resolve();\n      return;\n    }\n    stage = current;\n    const colors = settings.colors.map(resolveColor);\n    const start = originPointOf(options.origin);\n    const burst: Burst = { remaining: count, resolve };\n    for (let index = 0; index < count; index += 1) {\n      const particle = createParticle(isStill, burst, start, settings, colors);\n      current.particles.push(particle);\n    }\n    ensureRunning(current);\n  });\n}\n\ntype ButtonClickEvent = Parameters<NonNullable<ButtonProps['onClick']>>[0];\n\nexport type ConfettiButtonProps = ButtonProps & {\n  options?: ConfettiOptions;\n};\n\nexport function ConfettiButton({\n  options,\n  onClick,\n  ...props\n}: ConfettiButtonProps) {\n  function celebrate(event: ButtonClickEvent) {\n    onClick?.(event);\n    void confetti({ origin: event.currentTarget, ...options });\n  }\n\n  return <Button onClick={celebrate} {...props} />;\n}\n",
      "type": "registry:component",
      "target": "components/core/confetti.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}