opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Clocks
  4. Analog Clock — Numeric
Back to Clocks

Components / Clocks

Analog Clock — Numeric

Free Clocks React component — AnalogClockWidget. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Arabic numerals and a live second hand on a clean analog dial. Readable from across the room, still far from a default system clock.

Preview

Clock

--:--

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/clocks/analog-clock-widget.tsx in your project.

  4. 4

    Import and render:

    Example

    import { AnalogClockWidget } from "@/components/clocks/analog-clock-widget";

    <AnalogClockWidget variant="numeric" />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/clocks/analog-clock-widget.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use client";
 
import {
forwardRef,
useEffect,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
// roman = XII labels, minimal = ticks only, numeric = 12/3/6/9 labels.
export type AnalogClockFaceVariant = "roman" | "minimal" | "numeric";
 
const CLOCK_TICKS = Array.from({ length: 12 }, (_, hour) => {
const angle = (hour * 30 * Math.PI) / 180;
const inner = hour % 3 === 0 ? 38 : 40;
const outer = 44;
const f = (n: number) => n.toFixed(2);
return {
hour,
major: hour % 3 === 0,
x1: f(50 + inner * Math.sin(angle)),
y1: f(50 - inner * Math.cos(angle)),
x2: f(50 + outer * Math.sin(angle)),
y2: f(50 - outer * Math.cos(angle)),
};
});
 
const ROMAN_LABELS = [
{ deg: 0, label: "XII", x: "50.00", y: "18.00" },
{ deg: 90, label: "III", x: "82.00", y: "50.00" },
{ deg: 180, label: "VI", x: "50.00", y: "82.00" },
{ deg: 270, label: "IX", x: "18.00", y: "50.00" },
] as const;
 
const NUMERIC_LABELS = [
{ deg: 0, label: "12", x: "50.00", y: "18.00" },
{ deg: 90, label: "3", x: "82.00", y: "50.00" },
{ deg: 180, label: "6", x: "50.00", y: "82.00" },
{ deg: 270, label: "9", x: "18.00", y: "50.00" },
] as const;
 
type AnalogClockFaceProps = Readonly<{
hours: number;
minutes: number;
seconds: number;
variant: AnalogClockFaceVariant;
}>;
 
function AnalogClockFace({
hours,
minutes,
seconds,
variant,
}: AnalogClockFaceProps) {
const hourDeg = hours * 30 + minutes * 0.5;
const minuteDeg = minutes * 6 + seconds * 0.1;
const secondDeg = seconds * 6;
const labels = variant === "roman" ? ROMAN_LABELS : NUMERIC_LABELS;
 
return (
<svg viewBox="0 0 100 100" className="h-full w-full" aria-hidden>
<circle
cx="50"
cy="50"
r="46"
fill="none"
stroke="#f0f0f0"
strokeWidth="1"
/>
 
{CLOCK_TICKS.map((tick) => (
<line
key={tick.hour}
x1={tick.x1}
y1={tick.y1}
x2={tick.x2}
y2={tick.y2}
stroke="#d4d4d4"
strokeWidth={tick.major ? 1.5 : 1}
strokeLinecap="round"
/>
))}
 
{variant !== "minimal" &&
labels.map(({ deg, label, x, y }) => (
<text
key={deg}
x={x}
y={y}
textAnchor="middle"
dominantBaseline="middle"
className="fill-neutral-500 text-[6px] font-medium"
>
{label}
</text>
))}
 
<line
x1="50"
y1="50"
x2="50"
y2="28"
stroke="#171717"
strokeWidth="2.5"
strokeLinecap="round"
transform={`rotate(${hourDeg} 50 50)`}
/>
<line
x1="50"
y1="50"
x2="50"
y2="22"
stroke="#404040"
strokeWidth="1.5"
strokeLinecap="round"
transform={`rotate(${minuteDeg} 50 50)`}
/>
<line
x1="50"
y1="50"
x2="50"
y2="20"
stroke="#ef4444"
strokeWidth="1"
strokeLinecap="round"
transform={`rotate(${secondDeg} 50 50)`}
/>
<circle cx="50" cy="50" r="2" fill="#171717" />
</svg>
);
}
 
export type AnalogClockWidgetProps = Readonly<
{
// Face style — roman numerals, plain ticks, or numeric hour labels.
variant?: AnalogClockFaceVariant;
} & ComponentPropsWithoutRef<"div">
>;
 
// Compact iOS-style analog clock — live hands with three dial variants.
export const AnalogClockWidget = forwardRef<
HTMLDivElement,
AnalogClockWidgetProps
>(({ className, variant = "minimal", ...props }, ref) => {
const [now, setNow] = useState<Date | null>(null);
 
useEffect(() => {
const update = () => setNow(new Date());
update();
const timer = globalThis.setInterval(update, 1000);
return () => globalThis.clearInterval(timer);
}, []);
 
const hours = now ? now.getHours() % 12 : 0;
const minutes = now ? now.getMinutes() : 0;
const seconds = now ? now.getSeconds() : 0;
 
const digital = now
? now.toLocaleTimeString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: true,
})
: "--:--";
 
return (
<div
ref={ref}
data-slot="analog-clock-widget"
className={cn(
"flex h-44 w-44 flex-col items-center justify-between rounded-3xl border border-neutral-100 bg-white p-4 font-sans shadow-lg shadow-black/5 select-none",
className,
)}
{...props}
>
<p className="text-[10px] font-semibold tracking-widest text-neutral-400 uppercase">
Clock
</p>
 
<div className="relative h-24 w-24">
<div className="absolute inset-0 rounded-full border border-neutral-100" />
{now && (
<div className="absolute inset-1.5">
<AnalogClockFace
hours={hours}
minutes={minutes}
seconds={seconds}
variant={variant}
/>
</div>
)}
</div>
 
<p className="font-mono text-[9px] font-light text-neutral-900 tabular-nums">
{digital}
</p>
</div>
);
});
 
AnalogClockWidget.displayName = "AnalogClockWidget";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/clocks/analog-clock-widget.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.