Basic Number Ticker
fancy/basic-number-tickerFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/basic-number-ticker.jsonSource
1"use client"23import {4 forwardRef,5 useCallback,6 useEffect,7 useImperativeHandle,8 useState,9} from "react"10import {11 animate,12 AnimationPlaybackControls,13 motion,14 useMotionValue,15 useTransform,16 ValueAnimationTransition,17} from "motion/react"1819import { cn } from "@/lib/utils"2021interface NumberTickerProps {22 from: number // Starting value of the animation23 target: number // End value of the animation24 transition?: ValueAnimationTransition // Animation configuration, refer to motion docs for more details25 className?: string // additionl CSS classes for styling26 onStart?: () => void // Callback function when animation starts27 onComplete?: () => void // Callback function when animation completes28 autoStart?: boolean // Whether to start the animation automatically29}3031// Ref interface to allow external control of the animation32export interface NumberTickerRef {33 startAnimation: () => void34}3536const NumberTicker = forwardRef<NumberTickerRef, NumberTickerProps>(37 (38 {39 from = 0,40 target = 100,41 transition = {42 duration: 3,43 type: "tween",44 ease: "easeInOut",45 },46 className,47 onStart,48 onComplete,49 autoStart = true,50 ...props51 },52 ref53 ) => {54 const count = useMotionValue(from)55 const rounded = useTransform(count, (latest) => Math.round(latest))56 const [controls, setControls] = useState<AnimationPlaybackControls | null>(57 null58 )5960 // Function to start the animation61 const startAnimation = useCallback(() => {62 if (controls) controls.stop()63 onStart?.()6465 count.set(from)6667 const newControls = animate(count, target, {68 ...transition,69 onComplete: () => {70 onComplete?.()71 },72 })73 setControls(newControls)74 }, [])7576 // Expose the startAnimation function via ref77 useImperativeHandle(ref, () => ({78 startAnimation,79 }))8081 useEffect(() => {82 if (autoStart) {83 startAnimation()84 }85 return () => controls?.stop()86 }, [autoStart])8788 return (89 <motion.span className={cn(className)} {...props}>90 {rounded}91 </motion.span>92 )93 }94)9596NumberTicker.displayName = "NumberTicker"9798export default NumberTicker99100// Usage example:101// To start the animation from outside the component:102// 1. Create a ref:103// const tickerRef = useRef<NumberTickerRef>(null);104// 2. Pass the ref to the NumberTicker component:105// … truncated
What it pulls in
npm packages
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 | |
| 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 | |
| Elastic Lineelastic-line | fancy | Components | Free | 1 dep |
