Parallax Floating
fancy/parallax-floatingFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/parallax-floating.jsonSource
1"use client"23import {4 createContext,5 ReactNode,6 useCallback,7 useContext,8 useEffect,9 useRef,10} from "react"11import { useAnimationFrame } from "motion/react"1213import { cn } from "@/lib/utils"14import { useMousePositionRef } from "@/hooks/use-mouse-position-ref"1516interface FloatingContextType {17 registerElement: (id: string, element: HTMLDivElement, depth: number) => void18 unregisterElement: (id: string) => void19}2021const FloatingContext = createContext<FloatingContextType | null>(null)2223interface FloatingProps {24 children: ReactNode25 className?: string26 sensitivity?: number27 easingFactor?: number28}2930const Floating = ({31 children,32 className,33 sensitivity = 1,34 easingFactor = 0.05,35 ...props36}: FloatingProps) => {37 const containerRef = useRef<HTMLDivElement>(null)38 const elementsMap = useRef(39 new Map<40 string,41 {42 element: HTMLDivElement43 depth: number44 currentPosition: { x: number; y: number }45 }46 >()47 )48 const mousePositionRef = useMousePositionRef(containerRef)4950 const registerElement = useCallback(51 (id: string, element: HTMLDivElement, depth: number) => {52 elementsMap.current.set(id, {53 element,54 depth,55 currentPosition: { x: 0, y: 0 },56 })57 },58 []59 )6061 const unregisterElement = useCallback((id: string) => {62 elementsMap.current.delete(id)63 }, [])6465 useAnimationFrame(() => {66 if (!containerRef.current) return6768 elementsMap.current.forEach((data) => {69 const strength = (data.depth * sensitivity) / 207071 // Calculate new target position72 const newTargetX = mousePositionRef.current.x * strength73 const newTargetY = mousePositionRef.current.y * strength7475 // Check if we need to update76 const dx = newTargetX - data.currentPosition.x77 const dy = newTargetY - data.currentPosition.y7879 // Update position only if we're still moving80 data.currentPosition.x += dx * easingFactor81 data.currentPosition.y += dy * easingFactor8283 data.element.style.transform = `translate3d(${data.currentPosition.x}px, ${data.currentPosition.y}px, 0)`84 })85 })8687 return (88 <FloatingContext.Provider value={{ registerElement, unregisterElement }}>89 <div90 ref={containerRef}91 className={cn("absolute top-0 left-0 w-full h-full", className)}92 {...props}93 >94 {children}95 </div>96 </FloatingContext.Provider>97 )98}99100export default Floating101102interface FloatingElementProps {103// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from fancy
All 158 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Gradient With Svganimated-gradient-with-svg | fancy | Components | Free | no deps | |
| Basic Number Tickerbasic-number-ticker | fancy | Components | Free | 1 dep | |
| Box Carouselbox-carousel | fancy | Components | Free | 1 dep | |
| Breathing Textbreathing-text | fancy | Components | Free | 1 dep | |
| Circling Elementscircling-elements | fancy | Components | Free | 1 dep | |
| Css Boxcss-box | fancy | Components | Free | 1 dep | |
| Cursor Attractor And Gravitycursor-attractor-and-gravity | fancy | Components | Free | 5 deps | |
| Drag Elementsdrag-elements | fancy | Components | Free | 1 dep |
