Bubble Background
animate-ui/components-backgrounds-bubbleFreeComponent
An interactive background featuring smoothly animated gradient bubbles, creating a playful, dynamic, and visually engaging backdrop.
Install it
npx shadcn@latest add https://animate-ui.com/r/components-backgrounds-bubble.jsonSource
1'use client';23import * as React from 'react';4import {5 motion,6 useMotionValue,7 useSpring,8 type SpringOptions,9} from 'motion/react';1011import { cn } from '@/lib/utils';1213type BubbleColors = {14 first: string;15 second: string;16 third: string;17 fourth: string;18 fifth: string;19 sixth: string;20};2122type BubbleBackgroundProps = React.ComponentProps<'div'> & {23 interactive?: boolean;24 transition?: SpringOptions;25 colors?: BubbleColors;26};2728function BubbleBackground({29 ref,30 className,31 children,32 interactive = false,33 transition = { stiffness: 100, damping: 20 },34 colors = {35 first: '18,113,255',36 second: '221,74,255',37 third: '0,220,255',38 fourth: '200,50,50',39 fifth: '180,180,50',40 sixth: '140,100,255',41 },42 ...props43}: BubbleBackgroundProps) {44 const containerRef = React.useRef<HTMLDivElement>(null);45 React.useImperativeHandle(ref, () => containerRef.current as HTMLDivElement);4647 const mouseX = useMotionValue(0);48 const mouseY = useMotionValue(0);49 const springX = useSpring(mouseX, transition);50 const springY = useSpring(mouseY, transition);5152 const rectRef = React.useRef<DOMRect | null>(null);53 const rafIdRef = React.useRef<number | null>(null);5455 React.useLayoutEffect(() => {56 const updateRect = () => {57 if (containerRef.current) {58 rectRef.current = containerRef.current.getBoundingClientRect();59 }60 };6162 updateRect();6364 const el = containerRef.current;65 const ro = new ResizeObserver(updateRect);66 if (el) ro.observe(el);6768 window.addEventListener('resize', updateRect);69 window.addEventListener('scroll', updateRect, { passive: true });7071 return () => {72 ro.disconnect();73 window.removeEventListener('resize', updateRect);74 window.removeEventListener('scroll', updateRect);75 };76 }, []);7778 React.useEffect(() => {79 if (!interactive) return;8081 const el = containerRef.current;82 if (!el) return;8384 const handleMouseMove = (e: MouseEvent) => {85 const rect = rectRef.current;86 if (!rect) return;87 const centerX = rect.left + rect.width / 2;88 const centerY = rect.top + rect.height / 2;8990 if (rafIdRef.current != null) cancelAnimationFrame(rafIdRef.current);91 rafIdRef.current = requestAnimationFrame(() => {92 mouseX.set(e.clientX - centerX);93 mouseY.set(e.clientY - centerY);94 });95 };9697 el.addEventListener('mousemove', handleMouseMove as EventListener, {98 passive: true,99 });100// … truncated
What it pulls in
npm packages
Files it writes
More from Animate UI
All 580 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Avatar Groupcomponents-animate-avatar-group | Animate UI | Components | Free | 1 dep | |
| Codecomponents-animate-code | Animate UI | Components | Free | 1 dep | |
| Code Tabscomponents-animate-code-tabs | Animate UI | Components | Free | 1 dep | |
| Cursorcomponents-animate-cursor | Animate UI | Components | Free | no deps | |
| GitHub Stars Wheelcomponents-animate-github-stars-wheel | Animate UI | Components | Free | 1 dep | |
| Tabscomponents-animate-tabs | Animate UI | Components | Free | no deps | |
| Tooltipcomponents-animate-tooltip | Animate UI | Components | Free | 1 dep | |
| Fireworks Backgroundcomponents-backgrounds-fireworks | Animate UI | Components | Free | no deps |
