opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Clocks
  4. Ios Digital Clock
Back to Clocks

Components / Clocks

Ios Digital Clock

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

Live HH:MM in an iOS squircle with subtle tick marks around the edge. The kind of clock tile you'd find on a lock screen.

Preview

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/ios-digital-clock-widget.tsx in your project.

  4. 4

    Import and render:

    Example

    import { IosDigitalClockWidget } from "@/components/clocks/ios-digital-clock-widget";

    <IosDigitalClockWidget />

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/ios-digital-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
"use client";
 
import {
forwardRef,
useEffect,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
type PointOnPath = Readonly<{
x: number;
y: number;
tx: number;
ty: number;
}>;
 
// Walks a rounded rectangle path (0–1) and returns a point plus its inward normal.
function pointOnRoundedRect(
t: number,
left: number,
top: number,
width: number,
height: number,
radius: number,
): PointOnPath {
const sw = width - 2 * radius;
const sh = height - 2 * radius;
const arc = (Math.PI / 2) * radius;
const perim = 2 * sw + 2 * sh + 4 * arc;
let d = (((t % 1) + 1) % 1) * perim;
 
if (d <= sw) {
return { x: left + radius + d, y: top, tx: 1, ty: 0 };
}
d -= sw;
 
if (d <= arc) {
const angle = -Math.PI / 2 + (d / arc) * (Math.PI / 2);
const ox = left + width - radius;
const oy = top + radius;
return {
x: ox + radius * Math.cos(angle),
y: oy + radius * Math.sin(angle),
tx: -Math.sin(angle),
ty: Math.cos(angle),
};
}
d -= arc;
 
if (d <= sh) {
return { x: left + width, y: top + radius + d, tx: 0, ty: 1 };
}
d -= sh;
 
if (d <= arc) {
const angle = (d / arc) * (Math.PI / 2);
const ox = left + width - radius;
const oy = top + height - radius;
return {
x: ox + radius * Math.cos(angle),
y: oy + radius * Math.sin(angle),
tx: -Math.sin(angle),
ty: Math.cos(angle),
};
}
d -= arc;
 
if (d <= sw) {
return { x: left + width - radius - d, y: top + height, tx: -1, ty: 0 };
}
d -= sw;
 
if (d <= arc) {
const angle = Math.PI / 2 + (d / arc) * (Math.PI / 2);
const ox = left + radius;
const oy = top + height - radius;
return {
x: ox + radius * Math.cos(angle),
y: oy + radius * Math.sin(angle),
tx: -Math.sin(angle),
ty: Math.cos(angle),
};
}
d -= arc;
 
if (d <= sh) {
return { x: left, y: top + height - radius - d, tx: 0, ty: -1 };
}
d -= sh;
 
const angle = Math.PI + (d / arc) * (Math.PI / 2);
const ox = left + radius;
const oy = top + radius;
return {
x: ox + radius * Math.cos(angle),
y: oy + radius * Math.sin(angle),
tx: -Math.sin(angle),
ty: Math.cos(angle),
};
}
 
// 60 tick marks spaced evenly around the squircle border.
const BORDER_TICKS = Array.from({ length: 60 }, (_, tick) => {
const { x, y, tx, ty } = pointOnRoundedRect(tick / 60, 2, 2, 96, 96, 18);
const len = 4.2;
const f = (n: number) => n.toFixed(2);
return {
tick,
x1: f(x),
y1: f(y),
x2: f(x - ty * len),
y2: f(y + tx * len),
};
});
 
type SquircleDigitalFaceProps = Readonly<{
hours: string;
minutes: string;
}>;
 
function SquircleDigitalFace({ hours, minutes }: SquircleDigitalFaceProps) {
return (
<div className="relative h-full w-full">
{/* Tick ring sits behind the centered HH:MM readout. */}
<svg viewBox="0 0 100 100" className="h-full w-full" aria-hidden>
{BORDER_TICKS.map((tick) => (
<line
key={tick.tick}
x1={tick.x1}
y1={tick.y1}
x2={tick.x2}
y2={tick.y2}
stroke="#d4d4d4"
strokeWidth="1.1"
strokeLinecap="round"
/>
))}
</svg>
 
<div className="absolute inset-0 flex items-center justify-center">
<p className="text-5xl leading-none font-bold tracking-tight text-neutral-900 tabular-nums">
{hours}:{minutes}
</p>
</div>
</div>
);
}
 
export type IosDigitalClockWidgetProps = Readonly<
ComponentPropsWithoutRef<"div">
>;
 
export const IosDigitalClockWidget = forwardRef<
HTMLDivElement,
IosDigitalClockWidgetProps
>(({ className, ...props }, ref) => {
// Start as null so the first paint matches SSR; then refresh every second on the client.
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 ? String(now.getHours()).padStart(2, "0") : "00";
const minutes = now ? String(now.getMinutes()).padStart(2, "0") : "00";
 
return (
<div
ref={ref}
data-slot="ios-digital-clock-widget"
className={cn(
"flex h-44 w-44 items-center justify-center overflow-hidden rounded-3xl border border-neutral-100 bg-white p-1 font-sans shadow-lg select-none",
className,
)}
{...props}
>
{now ? (
<SquircleDigitalFace hours={hours} minutes={minutes} />
) : (
// Gray bar matches the size of the HH:MM text while time loads.
<div className="h-9 w-28 rounded-lg bg-neutral-100" aria-hidden />
)}
</div>
);
});
 
IosDigitalClockWidget.displayName = "IosDigitalClockWidget";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/clocks/ios-digital-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.