UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: number ticker startvalue prop added (#578)
Arghya Das5ad708e471e4
diff --git a/__registry__/index.tsx b/__registry__/index.tsxindex 50ba5fac..c21d2fe2 100644--- a/__registry__/index.tsx+++ b/__registry__/index.tsx@@ -2453,6 +2453,29 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "number-ticker-demo-2": {+ name: "number-ticker-demo-2",+ description: "Example showing animated counting numbers.",+ type: "registry:example",+ registryDependencies: ["https://magicui.design/r/number-ticker"],+ files: [+ {+ path: "registry/example/number-ticker-demo-2.tsx",+ type: "registry:example",+ target: "components/number-ticker-demo-2.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/example/number-ticker-demo-2.tsx");+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },"number-ticker-decimal-demo": {name: "number-ticker-decimal-demo",description: "Example showing animated counting decimal numbers.",diff --git a/content/docs/components/number-ticker.mdx b/content/docs/components/number-ticker.mdxindex 96b8b06c..85261a9e 100644--- a/content/docs/components/number-ticker.mdx+++ b/content/docs/components/number-ticker.mdx@@ -47,6 +47,10 @@ npx shadcn@latest add "https://magicui.design/r/number-ticker"<ComponentPreview name="number-ticker-decimal-demo" />+### Start Value++<ComponentPreview name="number-ticker-demo-2" />+## Props| Prop | Type | Default | Description |@@ -55,3 +59,4 @@ npx shadcn@latest add "https://magicui.design/r/number-ticker"| `direction` | `up \| down` | `"up"` | The direction to count in || `delay` | `number` | `0` | The delay before counting || `decimalPlaces` | `number` | `0` | The number of decimal places to show |+| `startValue` | `number` | `0` | The value to start counting from |diff --git a/public/r/number-ticker-demo-2.json b/public/r/number-ticker-demo-2.jsonnew file mode 100644index 00000000..f83b7421--- /dev/null+++ b/public/r/number-ticker-demo-2.json@@ -0,0 +1,18 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "number-ticker-demo-2",+ "type": "registry:example",+ "title": "Number Ticker Demo 2",+ "description": "Example showing animated counting numbers.",+ "registryDependencies": [+ "https://magicui.design/r/number-ticker"+ ],+ "files": [+ {+ "path": "registry/example/number-ticker-demo-2.tsx",+ "content": "import { NumberTicker } from \"@/components/magicui/number-ticker\";\r\n\r\nexport default function NumberTickerDemo() {\r\n return (\r\n <NumberTicker\r\n value={100}\r\n startValue={80}\r\n className=\"whitespace-pre-wrap text-8xl font-medium tracking-tighter text-black dark:text-white\"\r\n />\r\n );\r\n}\r\n",+ "type": "registry:example",+ "target": "components/number-ticker-demo-2.tsx"+ }+ ]+}\ No newline at end of filediff --git a/public/r/number-ticker.json b/public/r/number-ticker.jsonindex 957ead94..62c62e23 100644--- a/public/r/number-ticker.json+++ b/public/r/number-ticker.json@@ -10,7 +10,7 @@"files": [{"path": "registry/magicui/number-ticker.tsx",- "content": "\"use client\";\n\nimport { useInView, useMotionValue, useSpring } from \"motion/react\";\nimport { ComponentPropsWithoutRef, useEffect, useRef } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\ninterface NumberTickerProps extends ComponentPropsWithoutRef<\"span\"> {\n value: number;\n direction?: \"up\" | \"down\";\n delay?: number; // delay in s\n decimalPlaces?: number;\n}\n\nexport function NumberTicker({\n value,\n direction = \"up\",\n delay = 0,\n className,\n decimalPlaces = 0,\n ...props\n}: NumberTickerProps) {\n const ref = useRef<HTMLSpanElement>(null);\n const motionValue = useMotionValue(direction === \"down\" ? value : 0);\n const springValue = useSpring(motionValue, {\n damping: 60,\n stiffness: 100,\n });\n const isInView = useInView(ref, { once: true, margin: \"0px\" });\n\n useEffect(() => {\n if (isInView) {\n const timer = setTimeout(() => {\n motionValue.set(direction === \"down\" ? 0 : value);\n }, delay * 1000);\n return () => clearTimeout(timer);\n }\n }, [motionValue, isInView, delay, value, direction]);\n\n useEffect(\n () =>\n springValue.on(\"change\", (latest) => {\n if (ref.current) {\n ref.current.textContent = Intl.NumberFormat(\"en-US\", {\n minimumFractionDigits: decimalPlaces,\n maximumFractionDigits: decimalPlaces,\n }).format(Number(latest.toFixed(decimalPlaces)));\n }\n }),\n [springValue, decimalPlaces],\n );\n\n return (\n <span\n ref={ref}\n className={cn(\n \"inline-block tabular-nums tracking-wider text-black dark:text-white\",\n className,\n )}\n {...props}\n />\n );\n}\n",+ "content": "\"use client\";\n\nimport { useInView, useMotionValue, useSpring } from \"motion/react\";\nimport { ComponentPropsWithoutRef, useEffect, useRef } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\ninterface NumberTickerProps extends ComponentPropsWithoutRef<\"span\"> {\n value: number;\n startValue?: number;\n direction?: \"up\" | \"down\";\n delay?: number;\n decimalPlaces?: number;\n}\n\nexport function NumberTicker({\n value,\n startValue = 0,\n direction = \"up\",\n delay = 0,\n className,\n decimalPlaces = 0,\n ...props\n}: NumberTickerProps) {\n const ref = useRef<HTMLSpanElement>(null);\n const motionValue = useMotionValue(direction === \"down\" ? value : startValue);\n const springValue = useSpring(motionValue, {\n damping: 60,\n stiffness: 100,\n });\n const isInView = useInView(ref, { once: true, margin: \"0px\" });\n\n useEffect(() => {\n if (isInView) {\n const timer = setTimeout(() => {\n motionValue.set(direction === \"down\" ? startValue : value);\n }, delay * 1000);\n return () => clearTimeout(timer);\n }\n }, [motionValue, isInView, delay, value, direction, startValue]);\n\n useEffect(\n () =>\n springValue.on(\"change\", (latest) => {\n if (ref.current) {\n ref.current.textContent = Intl.NumberFormat(\"en-US\", {\n minimumFractionDigits: decimalPlaces,\n maximumFractionDigits: decimalPlaces,\n }).format(Number(latest.toFixed(decimalPlaces)));\n }\n }),\n [springValue, decimalPlaces]\n );\n\n return (\n <span\n ref={ref}\n className={cn(\n \"inline-block tabular-nums tracking-wider text-black dark:text-white\",\n className\n )}\n {...props}\n >\n {startValue}\n </span>\n );\n}\n","type": "registry:ui","target": "components/magicui/number-ticker.tsx"}diff --git a/registry.json b/registry.jsonindex 78373032..748640dc 100644--- a/registry.json+++ b/registry.json@@ -2036,6 +2036,22 @@}]},+ {+ "name": "number-ticker-demo-2",+ "type": "registry:example",+ "title": "Number Ticker Demo 2",+ "description": "Example showing animated counting numbers.",+ "registryDependencies": [+ "https://magicui.design/r/number-ticker"+ ],+ "files": [+ {+ "path": "registry/example/number-ticker-demo-2.tsx",+ "type": "registry:example",+ "target": "components/number-ticker-demo-2.tsx"+ }+ ]+ },{"name": "number-ticker-decimal-demo","type": "registry:example",diff --git a/registry/example/number-ticker-demo-2.tsx b/registry/example/number-ticker-demo-2.tsxnew file mode 100644index 00000000..295f2e7d--- /dev/null+++ b/registry/example/number-ticker-demo-2.tsx@@ -0,0 +1,11 @@+import { NumberTicker } from "@/registry/magicui/number-ticker";++export default function NumberTickerDemo() {+ return (+ <NumberTicker+ value={100}+ startValue={80}+ className="whitespace-pre-wrap text-8xl font-medium tracking-tighter text-black dark:text-white"+ />+ );+}diff --git a/registry/magicui/number-ticker.tsx b/registry/magicui/number-ticker.tsxindex 2836fdff..1b9bf39b 100644--- a/registry/magicui/number-ticker.tsx+++ b/registry/magicui/number-ticker.tsx@@ -7,13 +7,15 @@ import { cn } from "@/lib/utils";interface NumberTickerProps extends ComponentPropsWithoutRef<"span"> {value: number;+ startValue?: number;direction?: "up" | "down";- delay?: number; // delay in s+ delay?: number;decimalPlaces?: number;}export function NumberTicker({value,+ startValue = 0,direction = "up",delay = 0,className,@@ -21,7 +23,7 @@ export function NumberTicker({...props}: NumberTickerProps) {const ref = useRef<HTMLSpanElement>(null);- const motionValue = useMotionValue(direction === "down" ? value : 0);+ const motionValue = useMotionValue(direction === "down" ? value : startValue);const springValue = useSpring(motionValue, {damping: 60,stiffness: 100,@@ -31,11 +33,11 @@ export function NumberTicker({useEffect(() => {if (isInView) {const timer = setTimeout(() => {- motionValue.set(direction === "down" ? 0 : value);+ motionValue.set(direction === "down" ? startValue : value);}, delay * 1000);return () => clearTimeout(timer);}- }, [motionValue, isInView, delay, value, direction]);+ }, [motionValue, isInView, delay, value, direction, startValue]);useEffect(() =>@@ -58,6 +60,8 @@ export function NumberTicker({className,)}{...props}- />+ >+ {startValue}+ </span>);}diff --git a/registry/registry-examples.ts b/registry/registry-examples.tsindex ebbb139c..8037416a 100644--- a/registry/registry-examples.ts+++ b/registry/registry-examples.ts@@ -561,6 +561,20 @@ export const examples: Registry["items"] = [},],},+ {+ name: "number-ticker-demo-2",+ type: "registry:example",+ title: "Number Ticker Demo 2",+ description: "Example showing animated counting numbers.",+ registryDependencies: ["https://magicui.design/r/number-ticker"],+ files: [+ {+ path: "registry/example/number-ticker-demo-2.tsx",+ type: "registry:example",+ target: "components/number-ticker-demo-2.tsx",+ },+ ],+ },{name: "number-ticker-decimal-demo",type: "registry:example",