Documentation menu
Getting Started
Components
Primitives
- Agent Thinking
- Alert
- Alert Dialog
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Checkbox
- Command
- Command Palette
- Context Menu
- Data Table
- Dialog
- Dropdown Menu
- Empty
- Field
- Glow Cloud
- Input
- Label
- Medication Schedule
- Period Tabs
- Popover
- Rainbow Button
- Scroll Area
- Select
- Selectable Card
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Tag
- Textarea
- Timeline
- Tooltip
- Use Count Up
Charts and metrics
Patterns
- Confirm Button
- Date Picker
- Embedded
- Empty State
- Entity Drawer
- Error Boundary
- Field Row
- File Dropzone
- Form Dialog
- Kv
- List Editor
- List Page
- Live Time
- Mini Table
- Money
- Note Field
- Note Text
- Page Card
- Page Header
- Page Tabs
- Page Toolbar
- Person Cell
- Picture Field
- Print Card
- Range Switcher
- Row Actions
- Search Input
- Search Select
- Selected Box
- Signature Pad
- Status Badge
- Sub Nav Layout
- Suggest Input
- Tab Icons
- Table Footer
- Theme Accent Picker
- Time Picker
- View All Link
Dashboard cards
Advanced
Device frames
Website widgets
Website screens
Website panels
Primitives
Use Count Up
A hook that counts a number up to its value when it appears.
import { useState } from "react"
import { Button } from "@/registry/vcare/ui/button"
import { useCountUp } from "@/registry/vcare/ui/use-count-up"
export default function UseCountUpDemo() {
const [target, setTarget] = useState(1280)
const value = useCountUp(target)
return (
<div className="grid justify-items-center gap-3">
<span className="text-4xl font-semibold tabular-nums">{Math.round(value).toLocaleString()}</span>
<Button variant="outline" size="sm" onClick={() => setTarget(Math.round(Math.random() * 5000))}>New number</Button>
</div>
)
}Installation
npx shadcn@latest add @vcare/use-count-upOr install directly from the URL: https://ui.vcarehms.com/r/use-count-up.json
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/use-count-up.ts
import { useEffect, useRef, useState } from 'react';
/** Eases a number toward `target` so a headline counts up when its data changes. Skips the animation for reduced motion. */
export function useCountUp(target: number, duration = 600) {
const [value, setValue] = useState(target);
const from = useRef(target);
useEffect(() => {
if (typeof window === 'undefined' || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
setValue(target);
from.current = target;
return;
}
const start = performance.now();
const origin = from.current;
let frame = 0;
const tick = (now: number) => {
const t = Math.min(1, (now - start) / duration);
const eased = 1 - Math.pow(1 - t, 3);
const next = origin + (target - origin) * eased;
from.current = next;
setValue(next);
if (t < 1) frame = requestAnimationFrame(tick);
};
frame = requestAnimationFrame(tick);
return () => cancelAnimationFrame(frame);
}, [target, duration]);
return Math.round(value);
}Usage
import { useCountUp } from "@/components/ui/use-count-up"Exports
useCountUp
Dependencies
- None. This component only needs React and Tailwind.
Raw item: /r/use-count-up.json