Animated Number
Animated Number é um componente para React e Tailwind CSS, da biblioteca Cult UI, com licença MIT. Copia e cola no teu projeto.
Cult UI
MIT
other
O que é
Animated Number é um componente para React e Tailwind CSS, da biblioteca Cult UI, com licença MIT. Copia e cola no teu projeto.
Como usar
Abre o componente no catálogo, carrega em copiar, e cola no teu projeto. O prompt copiado leva o código e as regras para o teu agente o reproduzir sem alterar nada.
Cinco cópias grátis por mês. Sem cartão.
Precisa de
npm i motion
Licença
Este componente vem de Cult UI e é redistribuído sob a licença MIT, que permite uso comercial. O aviso de copyright viaja com o código.
Animated Number
"use client"
import { useEffect } from "react"
import { motion, MotionValue, useSpring, useTransform } from "motion/react"
interface AnimatedNumberProps {
value: number
mass?: number
stiffness?: number
damping?: number
precision?: number
format?: (value: number) => string
onAnimationStart?: () => void
onAnimationComplete?: () => void
}
export function AnimatedNumber({
value,
mass = 0.8,
stiffness = 75,
damping = 15,
precision = 0,
format = (num) => num.toLocaleString(),
onAnimationStart,
onAnimationComplete,
}: AnimatedNumberProps) {
const spring = useSpring(value, { mass, stiffness, damping })
const display: MotionValue<string> = useTransform(spring, (current) =>
format(parseFloat(current.toFixed(precision)))
)
useEffect(() => {
spring.set(value)
if (onAnimationStart) onAnimationStart()
const unsubscribe = spring.on("change", () => {
if (spring.get() === value && onAnimationComplete) onAnimationComplete()
})
return () => unsubscribe()
}, [spring, value, onAnimationStart, onAnimationComplete])
return <motion.span>{display}</motion.span>
}