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
Patterns
Live Time
Minutes counting up or down from a time, sharing one clock.
import { CountDown, CountUp } from "@/registry/vcare/ui/live-time"
const since = new Date(Date.now() - 23 * 60_000)
const until = new Date(Date.now() + 12 * 60_000)
export default function LiveTimeDemo() {
return (
<div className="grid gap-2 text-sm">
<p>Waiting for <CountUp from={since}>{(m) => <b className="tabular-nums">{m} min</b>}</CountUp></p>
<p>Next slot in <CountDown to={until} expired="now">{(m) => <b className="tabular-nums">{m} min</b>}</CountDown></p>
</div>
)
}Installation
npx shadcn@latest add @vcare/live-timeOr install directly from the URL: https://ui.vcarehms.com/r/live-time.json
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/live-time.tsx
import { useSyncExternalStore, type ReactNode } from 'react';
const listeners = new Set<() => void>();
let timer: ReturnType<typeof setInterval> | undefined;
let now = Date.now();
function subscribe(listener: () => void) {
now = Date.now();
listeners.add(listener);
if (!timer) {
timer = setInterval(() => {
now = Date.now();
listeners.forEach((notify) => notify());
}, 30_000);
}
return () => {
listeners.delete(listener);
if (!listeners.size && timer) {
clearInterval(timer);
timer = undefined;
}
};
}
const getSnapshot = () => now;
const getServerSnapshot = () => now;
function useLiveNow() {
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}
/** Live elapsed whole minutes from a timestamp. All instances share one clock. */
export function CountUp({ from, children }: { from: Date | string; children: (minutes: number) => ReactNode }) {
const current = useLiveNow();
const minutes = Math.max(0, Math.floor((current - new Date(from).getTime()) / 60_000));
return <time dateTime={new Date(from).toISOString()}>{children(minutes)}</time>;
}
/** Live remaining whole minutes to a timestamp. All instances share one clock. */
export function CountDown({
to,
children,
expired = null,
}: {
to: Date | string | null;
children: (minutes: number) => ReactNode;
expired?: ReactNode;
}) {
const current = useLiveNow();
if (!to) return <>{expired}</>;
const target = new Date(to);
const minutes = Math.max(0, Math.ceil((target.getTime() - current) / 60_000));
if (target.getTime() <= current) return <>{expired}</>;
return <time dateTime={target.toISOString()}>{children(minutes)}</time>;
}Usage
import { CountUp, CountDown } from "@/components/ui/live-time"Exports
CountUp, CountDown
Dependencies
- None. This component only needs React and Tailwind.
Raw item: /r/live-time.json