number-counter
jolyui-ui/number-counterFreeComponent
jolyui/ui publishes no description for this item.
Install it
npx shadcn@latest add https://jolyui.dev/r/number-counter.jsonSource
1import { motion, useInView, useSpring, useTransform } from "motion/react";2import * as React from "react";3import { cn } from "@/lib/utils";45type EasingType =6 | "linear"7 | "easeOut"8 | "easeIn"9 | "easeInOut"10 | "spring"11 | "bounce";1213interface NumberCounterProps {14 value: number;15 from?: number;16 duration?: number;17 delay?: number;18 decimals?: number;19 separator?: string;20 decimalSeparator?: string;21 prefix?: string;22 suffix?: string;23 easing?: EasingType;24 className?: string;25 once?: boolean;26 formatFn?: (value: number) => string;27}2829// Easing functions for CSS-based animation30const easingFunctions: Record<EasingType, number[]> = {31 linear: [0, 0, 1, 1],32 easeOut: [0.16, 1, 0.3, 1],33 easeIn: [0.7, 0, 0.84, 0],34 easeInOut: [0.65, 0, 0.35, 1],35 spring: [0.34, 1.56, 0.64, 1],36 bounce: [0.68, -0.55, 0.27, 1.55],37};3839// Format number with separators40const formatNumber = (41 value: number,42 decimals: number,43 separator: string,44 decimalSeparator: string,45): string => {46 const fixed = value.toFixed(decimals);47 const [intPart, decPart] = fixed.split(".");4849 const formattedInt = (intPart || "").replace(50 /\B(?=(\d{3})+(?!\d))/g,51 separator,52 );5354 return decPart55 ? `${formattedInt}${decimalSeparator}${decPart}`56 : formattedInt;57};5859// Animated digit component for rolling effect60const AnimatedDigit = ({61 digit,62 delay = 0,63}: {64 digit: string;65 delay?: number;66}) => {67 const isNumber = /\d/.test(digit);6869 if (!isNumber) {70 return <span className="inline-block">{digit}</span>;71 }7273 const num = parseInt(digit, 10);7475 return (76 <span className="relative inline-block h-[1em] w-[0.6em] overflow-hidden">77 <motion.span78 className="absolute top-0 left-0 flex w-full flex-col items-center"79 initial={{ y: "-100%" }}80 animate={{ y: `${-num * 10}%` }}81 transition={{82 duration: 0.5,83 delay,84 ease: [0.16, 1, 0.3, 1],85 }}86 >87 {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => (88 <span key={n} className="h-[1em] leading-none">89 {n}90 </span>91 ))}92 </motion.span>93 </span>94 );95};9697// Spring-based counter using useSpring98const SpringCounter = ({99 value,100 from = 0,101 decimals = 0,102 separator = ",",103 decimalSeparator = ".",104 prefix = "",105 suffix = "",106 className,107 once = true,108 formatFn,109 duration = 2,110 delay = 0,111}: NumberCounterProps) => {112 const ref = React.useRef(null);113// … truncated
What it pulls in
npm packages
Files it writes
More from jolyui/ui
All 199 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| ai-prompt-boxai-prompt-box | jolyui/ui | Components | Free | 4 deps | |
| animated-beamanimated-beam | jolyui/ui | Components | Free | 1 dep | |
| animated-tableanimated-table | jolyui/ui | Components | Free | 2 deps | |
| animated-theme-toggleanimated-theme-toggle | jolyui/ui | Components | Free | 2 deps | |
| animated-toastanimated-toast | jolyui/ui | Components | Free | 2 deps | |
| animated-tooltipanimated-tooltip | jolyui/ui | Components | Free | 1 dep | |
| bento-gridbento-grid | jolyui/ui | Components | Free | no deps | |
| buttonbutton | jolyui/ui | Components | Free | 3 deps |
