{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dot-pattern-background","type":"registry:block","title":"Dot pattern background","description":"A canvas of dots that lean away from the cursor and ease back into place, with children on top and a still grid under reduced motion.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/dot-pattern-background/dot-pattern-background.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useEffect,\n  useRef,\n  type ReactNode,\n} from \"react\";\nimport { useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type DotPatternBackgroundProps = {\n  className?: string;\n  /** Content rendered above the dots. */\n  children?: ReactNode;\n  /** Distance between dot centres, in pixels. Defaults to 24. */\n  spacing?: number;\n  /** Radius of the pointer's influence, in pixels. Defaults to 150. */\n  radius?: number;\n  /** Maximum push away from the pointer, in pixels. Defaults to 20. */\n  strength?: number;\n  /** Base dot radius, in pixels. Defaults to 1.5. */\n  dotSize?: number;\n};\n\ntype Dot = {\n  x: number;\n  y: number;\n  ox: number;\n  oy: number;\n};\n\ntype Point = { x: number; y: number };\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function DotPatternBackground({\n  className,\n  children,\n  spacing = 24,\n  radius = 150,\n  strength = 20,\n  dotSize = 1.5,\n}: DotPatternBackgroundProps) {\n  const reduce = Boolean(useReducedMotion());\n  const rootRef = useRef<HTMLDivElement | null>(null);\n  const canvasRef = useRef<HTMLCanvasElement | null>(null);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n\n    const context = canvas.getContext(\"2d\");\n    if (!context) return;\n\n    const gap = Math.max(18, spacing);\n    const influence = Math.max(48, radius);\n    const push = Math.max(0, strength);\n    const baseRadius = Math.max(0.75, dotSize);\n\n    let dots: Dot[] = [];\n    let width = 0;\n    let height = 0;\n    let color: string | null = null;\n    let pointer: Point | null = null;\n    let rect: DOMRect | null = null;\n    let frame: number | null = null;\n    let running = false;\n    let dirty = true;\n\n    const buildDots = () => {\n      dots = [];\n      if (width < 1 || height < 1) return;\n\n      const columns = Math.max(1, Math.floor(width / gap) + 1);\n      const rows = Math.max(1, Math.floor(height / gap) + 1);\n      const originX = (width - (columns - 1) * gap) / 2;\n      const originY = (height - (rows - 1) * gap) / 2;\n\n      for (let row = 0; row < rows; row += 1) {\n        for (let column = 0; column < columns; column += 1) {\n          dots.push({\n            x: originX + column * gap,\n            y: originY + row * gap,\n            ox: 0,\n            oy: 0,\n          });\n        }\n      }\n    };\n\n    const paint = () => {\n      const ctx = context;\n      if (dirty || !color) {\n        color = getComputedStyle(canvas).color || null;\n        dirty = false;\n      }\n\n      ctx.clearRect(0, 0, width, height);\n      if (!color) return;\n      ctx.fillStyle = color;\n\n      for (const dot of dots) {\n        const distance = Math.hypot(dot.ox, dot.oy);\n        const energy = push > 0 ? Math.min(1, distance / push) : 0;\n\n        ctx.globalAlpha = 0.22 + energy * 0.5;\n        ctx.beginPath();\n        ctx.arc(\n          dot.x + dot.ox,\n          dot.y + dot.oy,\n          baseRadius + energy * 1.35,\n          0,\n          Math.PI * 2\n        );\n        ctx.fill();\n      }\n\n      ctx.globalAlpha = 1;\n    };\n\n    const step = () => {\n      frame = null;\n      let moving = false;\n\n      if (!pointer) {\n        for (const dot of dots) {\n          const nextX = dot.ox + (0 - dot.ox) * 0.16;\n          const nextY = dot.oy + (0 - dot.oy) * 0.16;\n          if (Math.abs(nextX - dot.ox) > 0.01 || Math.abs(nextY - dot.oy) > 0.01) {\n            moving = true;\n          }\n          dot.ox = nextX;\n          dot.oy = nextY;\n        }\n      } else {\n        const target = pointer;\n\n        for (const dot of dots) {\n          const dx = dot.x - target.x;\n          const dy = dot.y - target.y;\n          const distance = Math.hypot(dx, dy);\n\n          let tx = 0;\n          let ty = 0;\n\n          if (distance < influence && distance > 0.5) {\n            const force = (1 - distance / influence) * push;\n            tx = (dx / distance) * force;\n            ty = (dy / distance) * force;\n          }\n\n          const nextX = dot.ox + (tx - dot.ox) * 0.16;\n          const nextY = dot.oy + (ty - dot.oy) * 0.16;\n          if (Math.abs(nextX - dot.ox) > 0.01 || Math.abs(nextY - dot.oy) > 0.01) {\n            moving = true;\n          }\n          dot.ox = nextX;\n          dot.oy = nextY;\n        }\n      }\n\n      paint();\n\n      if (moving || dirty) {\n        frame = window.requestAnimationFrame(step);\n      } else {\n        running = false;\n      }\n    };\n\n    const ensureLoop = () => {\n      if (reduce || running) return;\n      running = true;\n      frame = window.requestAnimationFrame(step);\n    };\n\n    const resize = () => {\n      const bounds = root.getBoundingClientRect();\n      const dpr = window.devicePixelRatio || 1;\n      width = Math.max(0, bounds.width);\n      height = Math.max(0, bounds.height);\n      rect = bounds;\n\n      canvas.width = Math.max(1, Math.round(width * dpr));\n      canvas.height = Math.max(1, Math.round(height * dpr));\n      context.setTransform(dpr, 0, 0, dpr, 0, 0);\n\n      buildDots();\n      dirty = true;\n      paint();\n      if (!reduce) ensureLoop();\n    };\n\n    const movePointer = (event: PointerEvent) => {\n      if (reduce) return;\n      if (!rect) rect = root.getBoundingClientRect();\n      const x = event.clientX - rect.left;\n      const y = event.clientY - rect.top;\n\n      if (!pointer) pointer = { x, y };\n      else {\n        pointer.x = x;\n        pointer.y = y;\n      }\n      ensureLoop();\n    };\n\n    const enterPointer = (event: PointerEvent) => {\n      rect = root.getBoundingClientRect();\n      movePointer(event);\n    };\n\n    const leavePointer = () => {\n      pointer = null;\n      ensureLoop();\n    };\n\n    const upPointer = (event: PointerEvent) => {\n      if (event.pointerType === \"touch\") {\n        pointer = null;\n        ensureLoop();\n      }\n    };\n\n    if (typeof ResizeObserver !== \"undefined\") {\n      const observer = new ResizeObserver(() => resize());\n      observer.observe(root);\n\n      resize();\n\n      /* Watch the root and its ancestors only: a theme switch changes the\n         tokens on one of them, while unrelated animations stay invisible. */\n      const themeObserver = new MutationObserver(() => {\n        dirty = true;\n        if (reduce) paint();\n        else ensureLoop();\n      });\n\n      for (\n        let node: Element | null = root;\n        node;\n        node = node.parentElement\n      ) {\n        themeObserver.observe(node, {\n          attributes: true,\n          attributeFilter: [\"class\", \"style\", \"data-theme\"],\n        });\n      }\n\n      root.addEventListener(\"pointerenter\", enterPointer);\n      root.addEventListener(\"pointermove\", movePointer);\n      root.addEventListener(\"pointerdown\", enterPointer);\n      root.addEventListener(\"pointerleave\", leavePointer);\n      root.addEventListener(\"pointercancel\", leavePointer);\n      root.addEventListener(\"pointerup\", upPointer);\n\n      return () => {\n        observer.disconnect();\n        themeObserver.disconnect();\n        root.removeEventListener(\"pointerenter\", enterPointer);\n        root.removeEventListener(\"pointermove\", movePointer);\n        root.removeEventListener(\"pointerdown\", enterPointer);\n        root.removeEventListener(\"pointerleave\", leavePointer);\n        root.removeEventListener(\"pointercancel\", leavePointer);\n        root.removeEventListener(\"pointerup\", upPointer);\n        if (frame !== null) window.cancelAnimationFrame(frame);\n        running = false;\n      };\n    }\n\n    resize();\n\n    root.addEventListener(\"pointerenter\", enterPointer);\n    root.addEventListener(\"pointermove\", movePointer);\n    root.addEventListener(\"pointerdown\", enterPointer);\n    root.addEventListener(\"pointerleave\", leavePointer);\n    root.addEventListener(\"pointercancel\", leavePointer);\n    root.addEventListener(\"pointerup\", upPointer);\n\n    return () => {\n      root.removeEventListener(\"pointerenter\", enterPointer);\n      root.removeEventListener(\"pointermove\", movePointer);\n      root.removeEventListener(\"pointerdown\", enterPointer);\n      root.removeEventListener(\"pointerleave\", leavePointer);\n      root.removeEventListener(\"pointercancel\", leavePointer);\n      root.removeEventListener(\"pointerup\", upPointer);\n      if (frame !== null) window.cancelAnimationFrame(frame);\n      running = false;\n    };\n  }, [reduce, spacing, radius, strength, dotSize]);\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\n        \"relative isolate min-h-[320px] overflow-hidden rounded-xl border border-border bg-card text-card-foreground\",\n        className\n      )}\n    >\n      <canvas\n        ref={canvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n      />\n\n      <div className=\"relative\">\n        {children ?? (\n          <div className=\"mx-auto flex max-w-2xl flex-col gap-3 px-6 py-16 sm:px-10\">\n            <span className=\"text-[12.5px] font-medium text-muted-foreground\">\n              Build metrics\n            </span>\n            <h2 className=\"text-balance text-2xl font-semibold tracking-tight sm:text-3xl\">\n              Deploys finish 18% faster this month.\n            </h2>\n            <p className=\"max-w-md text-[14px] leading-relaxed text-muted-foreground\">\n              Median build time is 94 seconds across 2,847 runs, and the slowest\n              queue is down to six jobs.\n            </p>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n\n/* Ready-made instance for previews and docs. */\nexport function DotPatternBackgroundDemo() {\n  return <DotPatternBackground />;\n}\n\nexport default DotPatternBackground;\n"}]}