opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Stopwatch
  4. Stopwatch
Back to Stopwatch

Components / Stopwatch

Stopwatch

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

Millisecond-precision stopwatch with reset, play-pause, and stop in a compact white widget. Running state shown with a small indicator dot.

Preview

00:00.00

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/stopwatch/stopwatch-widget.tsx in your project.

  4. 4

    Import and render:

    Example

    import { StopwatchWidget } from "@/components/stopwatch/stopwatch-widget";

    <StopwatchWidget />

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/stopwatch/stopwatch-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
"use client";
 
import {
forwardRef,
useEffect,
useRef,
useState,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
import { Play, Pause, Repeat } from "lucide-react";
 
// Stopwatch widget — start, pause, lap, and reset with centisecond precision.
// Splits elapsed milliseconds into MM:SS and centiseconds for the display
function formatStopwatch(ms: number) {
const mins = Math.floor(ms / 60_000);
const secs = Math.floor((ms % 60_000) / 1000);
const centis = Math.floor((ms % 1000) / 10);
return {
main: `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`,
centis: String(centis).padStart(2, "0"),
};
}
 
type StopSquareProps = Readonly<{
size?: number;
}>;
 
// Small square icon used on the stop button
function StopSquare({ size = 8 }: StopSquareProps) {
return (
<svg width={size} height={size} viewBox="0 0 8 8" fill="none" aria-hidden>
<rect width="8" height="8" rx="1.5" fill="currentColor" />
</svg>
);
}
 
export type StopwatchWidgetProps = Readonly<ComponentPropsWithoutRef<"div">>;
 
export const StopwatchWidget = forwardRef<HTMLDivElement, StopwatchWidgetProps>(
({ className, ...props }, ref) => {
const [elapsed, setElapsed] = useState(0);
const [running, setRunning] = useState(false);
// startRef = timestamp when the current run began; baseRef = ms already counted before pause
const startRef = useRef<number | null>(null);
const baseRef = useRef(0);
 
// requestAnimationFrame loop keeps the centisecond readout smooth while running
useEffect(() => {
if (!running) return;
 
startRef.current = performance.now();
 
const tick = () => {
setElapsed(
baseRef.current + (performance.now() - (startRef.current ?? 0)),
);
frame = globalThis.requestAnimationFrame(tick);
};
 
let frame = globalThis.requestAnimationFrame(tick);
return () => globalThis.cancelAnimationFrame(frame);
}, [running]);
 
const handlePlay = () => {
baseRef.current = elapsed;
setRunning(true);
};
 
const handlePause = () => {
if (running && startRef.current !== null) {
baseRef.current += performance.now() - startRef.current;
setElapsed(baseRef.current);
}
setRunning(false);
};
 
// Reset clears elapsed time and stops the timer
const clearTimer = () => {
baseRef.current = 0;
startRef.current = null;
setElapsed(0);
setRunning(false);
};
 
const { main, centis } = formatStopwatch(elapsed);
const canReset = elapsed > 0 && !running;
const canStop = running;
 
// Green status dot (top-right), MM:SS.centis readout, reset · play/pause · stop controls
return (
<div
ref={ref}
data-slot="minimal-stopwatch-widget"
className={cn(
"relative flex h-44 w-44 max-w-full flex-col items-center justify-between overflow-hidden rounded-3xl border border-neutral-100 bg-white px-4 py-5 font-sans shadow-lg shadow-black/5 select-none",
className,
)}
{...props}
>
<span
aria-hidden
className={cn(
"absolute top-4 right-4 h-2 w-2 rounded-full transition-colors duration-300",
running
? "bg-[#34C759]/55 ring-1 ring-[#34C759]/20"
: "bg-[#34C759]/18 ring-1 ring-[#34C759]/10",
)}
/>
 
<div className="flex flex-1 flex-col items-center justify-center">
<p className="flex items-baseline leading-none tabular-nums">
<span className="text-[32px] font-extralight tracking-[-0.04em] text-neutral-900">
{main}
</span>
<span className="ml-px text-[15px] font-light text-neutral-400">
.{centis}
</span>
</p>
</div>
 
<div className="flex items-center gap-2 rounded-full bg-neutral-50 px-2 py-1.5 ring-1 ring-neutral-100">
<button
type="button"
aria-label="Reset stopwatch"
onClick={clearTimer}
disabled={!canReset}
className={cn(
"flex h-7 w-7 cursor-pointer items-center justify-center rounded-full bg-white text-neutral-500 ring-1 ring-neutral-200/80 transition-all active:scale-95 disabled:cursor-default disabled:opacity-30",
)}
>
<Repeat size={11} color="currentColor" />
</button>
 
{running ? (
<button
type="button"
aria-label="Pause stopwatch"
onClick={handlePause}
className="flex h-8 w-8 cursor-pointer items-center justify-center rounded-full bg-[#FF9500] text-white shadow-[0_2px_8px_rgba(255,149,0,0.28)] transition-transform active:scale-95"
>
<Pause size={14} color="#FFFFFF" fill="#FFFFFF" />
</button>
) : (
<button
type="button"
aria-label="Start stopwatch"
onClick={handlePlay}
className="flex h-8 w-8 cursor-pointer items-center justify-center rounded-full bg-[#34C759] text-white shadow-[0_2px_8px_rgba(52,199,89,0.28)] transition-transform active:scale-95"
>
<Play size={14} color="#FFFFFF" fill="#FFFFFF" />
</button>
)}
 
<button
type="button"
aria-label="Stop stopwatch"
onClick={clearTimer}
disabled={!canStop}
className={cn(
"flex h-7 w-7 cursor-pointer items-center justify-center rounded-full transition-all active:scale-95 disabled:cursor-default disabled:opacity-30",
canStop
? "bg-[#FF3B30] text-white shadow-[0_2px_8px_rgba(255,59,48,0.25)]"
: "bg-white text-neutral-300 ring-1 ring-neutral-200/80",
)}
>
<StopSquare size={8} />
</button>
</div>
</div>
);
},
);
 
StopwatchWidget.displayName = "StopwatchWidget";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/stopwatch/stopwatch-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.