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

Components / Compass

Compass

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

A compass dial with labeled directions and a needle that follows device tilt. Pass a heading prop or let the browser handle orientation.

Preview

NESW

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

  4. 4

    Import and render:

    Example

    import { CompassWidget } from "@/components/compass/compass-widget";

    <CompassWidget heading={45} />

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/compass/compass-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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use client";
 
import {
forwardRef,
useCallback,
useEffect,
useRef,
useState,
useSyncExternalStore,
type ComponentPropsWithoutRef,
} from "react";
 
import { cn } from "@/lib/cn";
 
const CX = 88;
const CY = 84;
const R = 56;
const TICK_COUNT = 60;
 
const CARDINAL_LABELS = [
{ text: "N", deg: 0 },
{ text: "E", deg: 90 },
{ text: "S", deg: 180 },
{ text: "W", deg: 270 },
] as const;
 
// Rounds SVG numbers so server and client render identical coordinates
function svgCoord(value: number) {
return Number(value.toFixed(2));
}
 
type DeviceOrientationWithCompass = DeviceOrientationEvent & {
webkitCompassHeading?: number;
};
 
// Keeps heading within 0–359°
function normalizeHeading(degrees: number) {
return ((degrees % 360) + 360) % 360;
}
 
// Reads compass heading from iOS webkit or standard deviceorientation APIs
function readHeading(event: DeviceOrientationEvent): number | null {
const e = event as DeviceOrientationWithCompass;
 
if (
typeof e.webkitCompassHeading === "number" &&
!Number.isNaN(e.webkitCompassHeading)
) {
return normalizeHeading(e.webkitCompassHeading);
}
 
if (event.absolute && event.alpha != null && !Number.isNaN(event.alpha)) {
return normalizeHeading(360 - event.alpha);
}
 
if (event.alpha != null && !Number.isNaN(event.alpha)) {
return normalizeHeading(360 - event.alpha);
}
 
return null;
}
 
// iOS Safari requires an explicit permission prompt before orientation events fire
function needsOrientationPermission() {
return (
globalThis.DeviceOrientationEvent !== undefined &&
"requestPermission" in globalThis.DeviceOrientationEvent
);
}
 
// Client-only snapshot — false on the server to avoid hydration mismatches
function useRequiresOrientationPrompt() {
return useSyncExternalStore(
() => () => {},
() => needsOrientationPermission(),
() => false,
);
}
 
// Violet mascot peeking from the bottom of the dial
function PeekingMascot() {
return (
<g aria-hidden>
<circle cx="88" cy="142" r="58" fill="#7C6CF0" />
<circle cx="68" cy="122" r="5" fill="white" />
<circle cx="108" cy="122" r="5" fill="white" />
<circle cx="69" cy="123" r="2.2" fill="#1C1C1E" />
<circle cx="109" cy="123" r="2.2" fill="#1C1C1E" />
<path
d="M76 138 Q88 146 100 138"
stroke="white"
strokeWidth="2.2"
fill="none"
strokeLinecap="round"
/>
</g>
);
}
 
// Tick ring and N/E/S/W labels — rotated as a group to match device heading
function CompassFace() {
return (
<g aria-hidden>
{Array.from({ length: TICK_COUNT }, (_, tick) => {
const deg = tick * (360 / TICK_COUNT);
const rad = ((deg - 90) * Math.PI) / 180;
const major = deg % 90 === 0;
const inner = R - (major ? 8 : 5);
const outer = R - 1;
return (
<line
key={deg}
x1={svgCoord(CX + inner * Math.cos(rad))}
y1={svgCoord(CY + inner * Math.sin(rad))}
x2={svgCoord(CX + outer * Math.cos(rad))}
y2={svgCoord(CY + outer * Math.sin(rad))}
stroke="white"
strokeWidth={major ? 1.6 : 1}
strokeLinecap="round"
opacity={major ? 0.95 : 0.45}
/>
);
})}
 
{CARDINAL_LABELS.map(({ text, deg }) => {
const rad = ((deg - 90) * Math.PI) / 180;
const lr = R + 10;
return (
<text
key={text}
x={svgCoord(CX + lr * Math.cos(rad))}
y={svgCoord(CY + lr * Math.sin(rad))}
textAnchor="middle"
dominantBaseline="middle"
fill="white"
fontSize="11"
fontWeight="600"
fontFamily='-apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif'
>
{text}
</text>
);
})}
</g>
);
}
 
// heading — fallback degrees when device sensors are unavailable
export type CompassWidgetProps = Readonly<
{
heading?: number;
} & ComponentPropsWithoutRef<"div">
>;
 
export const CompassWidget = forwardRef<HTMLDivElement, CompassWidgetProps>(
({ className, heading: fallbackHeading = 0, ...props }, ref) => {
const requiresPrompt = useRequiresOrientationPrompt();
const [heading, setHeading] = useState(normalizeHeading(fallbackHeading));
const [active, setActive] = useState(false);
// True once the user grants iOS permission or when no prompt is required
const [promptDismissed, setPromptDismissed] = useState(false);
const [permissionDenied, setPermissionDenied] = useState(false);
const listeningRef = useRef(false);
 
const onOrientation = useCallback((event: DeviceOrientationEvent) => {
const next = readHeading(event);
if (next != null) {
setHeading(next);
setActive(true);
}
}, []);
 
const startListening = useCallback(() => {
if (listeningRef.current) return;
globalThis.addEventListener(
"deviceorientationabsolute",
onOrientation,
true,
);
globalThis.addEventListener("deviceorientation", onOrientation, true);
listeningRef.current = true;
}, [onOrientation]);
 
const stopListening = useCallback(() => {
if (listeningRef.current) {
globalThis.removeEventListener(
"deviceorientationabsolute",
onOrientation,
true,
);
globalThis.removeEventListener(
"deviceorientation",
onOrientation,
true,
);
listeningRef.current = false;
}
}, [onOrientation]);
 
const enableCompass = useCallback(async () => {
if (requiresPrompt) {
try {
const requestPermission = (
DeviceOrientationEvent as typeof DeviceOrientationEvent & {
requestPermission?: () => Promise<PermissionState>;
}
).requestPermission;
 
if (!requestPermission) return;
 
const result = await requestPermission();
if (result === "granted") {
setPromptDismissed(true);
setPermissionDenied(false);
return;
}
 
setPermissionDenied(true);
return;
} catch {
setPermissionDenied(true);
return;
}
}
 
setPromptDismissed(true);
setPermissionDenied(false);
}, [requiresPrompt]);
 
// Subscribe to orientation events once permission is not blocking
useEffect(() => {
if (requiresPrompt && !promptDismissed) {
return stopListening;
}
 
startListening();
return stopListening;
}, [requiresPrompt, promptDismissed, startListening, stopListening]);
 
const showTapHint = requiresPrompt && !promptDismissed && !permissionDenied;
 
return (
<div
ref={ref}
data-slot="minimal-compass-widget"
className={cn(
"relative h-44 w-44 max-w-full overflow-hidden rounded-[1.75rem] bg-black font-sans shadow-lg shadow-black/5 select-none",
className,
)}
{...props}
>
<svg
viewBox="0 0 176 176"
className="absolute inset-0 h-full w-full"
aria-hidden
>
<rect width="176" height="176" fill="#000000" />
<PeekingMascot />
<g transform={`rotate(${-heading} ${CX} ${CY})`}>
<CompassFace />
</g>
</svg>
 
{active && (
<span
aria-hidden
className="absolute top-3.5 right-3.5 h-1.5 w-1.5 rounded-full bg-[#34C759]/50"
/>
)}
 
{showTapHint && (
<button
type="button"
aria-label="Enable compass"
onClick={() => void enableCompass()}
className="absolute inset-0 flex cursor-pointer items-end justify-center border-0 bg-black/50 p-0 pb-3 font-sans"
>
<span className="text-[10px] font-medium text-white/70">
Tap to enable
</span>
</button>
)}
 
{permissionDenied && (
<output className="absolute inset-0 flex items-end justify-center border-0 bg-black/50 pb-3 font-sans">
<span className="text-[10px] font-medium text-white/40">
Access denied
</span>
</output>
)}
</div>
);
},
);
 
CompassWidget.displayName = "CompassWidget";
PreviousNext

On this

page

Jump to a section on this page.

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