@mmerlone/react-tz-globepicker 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marcio Merlone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,416 @@
1
+ # react-tz-globepicker
2
+
3
+ Interactive 3D globe component for timezone selection with React.
4
+
5
+ https://miro.medium.com/v2/resize:fit:4800/format:webp/0*AuW8APtlc27iI-5D
6
+
7
+ ## Features
8
+
9
+ - Interactive 3D globe visualization with drag-to-rotate and zoom
10
+ - Clickable timezone markers with hover tooltips
11
+ - Animated fly-to transitions when selecting timezones
12
+ - Multiple visualization modes (nautic bands, etc-gmt boundaries, iana country-level)
13
+ - Bundled timezone data for easy setup
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ pnpm add react-tz-globepicker
19
+ # or
20
+ npm install react-tz-globepicker
21
+ # or
22
+ yarn add react-tz-globepicker
23
+ ```
24
+
25
+ ## Peer Dependencies
26
+
27
+ This package requires the following peer dependencies:
28
+
29
+ - `react` ^18.0.0
30
+ - `react-dom` ^18.0.0
31
+
32
+ ## Complete Example
33
+
34
+ Here's a complete example showing how to use the utilities together, similar to how they're used in production applications:
35
+
36
+ ```tsx
37
+ import React, { useState, useEffect } from "react";
38
+ import {
39
+ TzGlobePicker,
40
+ buildMarkerList,
41
+ } from "react-tz-globepicker";
42
+
43
+ interface TimezoneOption {
44
+ value: string;
45
+ label: string;
46
+ }
47
+
48
+ // You would typically get this from your own API or timezone utilities
49
+ const mockTimezones: TimezoneOption[] = [
50
+ { value: "America/New_York", label: "America/New York (UTC-5)" },
51
+ { value: "Europe/London", label: "Europe/London (UTC+0)" },
52
+ { value: "Asia/Tokyo", label: "Asia/Tokyo (UTC+9)" },
53
+ { value: "Australia/Sydney", label: "Australia/Sydney (UTC+10)" },
54
+ ];
55
+
56
+ function TimezoneSelector() {
57
+ const [selectedTimezone, setSelectedTimezone] = useState<string | null>(
58
+ "America/New_York",
59
+ );
60
+ const [timezones, setTimezones] = useState<TimezoneOption[]>([]);
61
+ const [isLoading, setIsLoading] = useState(true);
62
+
63
+ useEffect(() => {
64
+ // Load available timezones (you'd typically get this from your own API)
65
+ setTimeout(() => {
66
+ setTimezones(mockTimezones);
67
+ setIsLoading(false);
68
+ }, 500);
69
+ }, []);
70
+
71
+ // Generate markers for the globe from available timezones
72
+ const markers = buildMarkerList(timezones.map((tz) => tz.value));
73
+
74
+ return (
75
+ <div style={{ display: "flex", gap: "2rem", padding: "2rem" }}>
76
+ {/* Globe Component */}
77
+ <div>
78
+ <h3>Select Timezone on Globe</h3>
79
+ <TzGlobePicker
80
+ timezone={selectedTimezone}
81
+ size={350}
82
+ onSelect={(tz) => setSelectedTimezone(tz)}
83
+ markers={markers}
84
+ showTooltips={true}
85
+ zoomMarkers={true}
86
+ showCountryBorders={true}
87
+ showTZBoundaries="etc-gmt"
88
+ />
89
+ </div>
90
+
91
+ {/* Dropdown Selector */}
92
+ <div style={{ minWidth: "300px" }}>
93
+ <h3>Or Select from List</h3>
94
+ <label htmlFor="timezone-select">Timezone</label>
95
+ <select
96
+ id="timezone-select"
97
+ style={{ display: "block", marginTop: "0.5rem", minWidth: "100%" }}
98
+ value={selectedTimezone ?? ""}
99
+ onChange={(event) => {
100
+ setSelectedTimezone(event.target.value || null);
101
+ }}
102
+ disabled={isLoading}
103
+ >
104
+ <option value="">Select your timezone...</option>
105
+ {timezones.map((tz) => (
106
+ <option key={tz.value} value={tz.value}>
107
+ {tz.label}
108
+ </option>
109
+ ))}
110
+ </select>
111
+
112
+ {selectedTimezone && (
113
+ <div
114
+ style={{
115
+ marginTop: "1rem",
116
+ padding: "1rem",
117
+ backgroundColor: "#f5f5f5",
118
+ borderRadius: "4px",
119
+ }}
120
+ >
121
+ <strong>Selected:</strong> {selectedTimezone}
122
+ </div>
123
+ )}
124
+ </div>
125
+ </div>
126
+ );
127
+ }
128
+
129
+ function App() {
130
+ return <TimezoneSelector />;
131
+ }
132
+ ```
133
+
134
+ ## Basic Usage
135
+
136
+ ```tsx
137
+ import { TzGlobePicker } from "react-tz-globepicker";
138
+
139
+ function App() {
140
+ const [timezone, setTimezone] = React.useState<string | null>(
141
+ "America/New_York",
142
+ );
143
+
144
+ return (
145
+ <TzGlobePicker
146
+ timezone={timezone}
147
+ size={400}
148
+ onSelect={(tz) => setTimezone(tz)}
149
+ showMarkers={true}
150
+ showTooltips={true}
151
+ />
152
+ );
153
+ }
154
+ ```
155
+
156
+ ## Space Background
157
+
158
+ The package includes a small `SpaceBackground` React component you can pass as the `background` prop to get a starfield-style backdrop.
159
+
160
+ ```tsx
161
+ import { TzGlobePicker, SpaceBackground } from "react-tz-globepicker";
162
+
163
+ function App() {
164
+ return (
165
+ <TzGlobePicker
166
+ timezone="America/New_York"
167
+ size={400}
168
+ onSelect={(tz) => console.log(tz)}
169
+ showMarkers={true}
170
+ background={<SpaceBackground />}
171
+ />
172
+ );
173
+ }
174
+ ```
175
+
176
+
177
+ ### Boundary Mode Constants
178
+
179
+ Use `TZ_BOUNDARY_MODES` to avoid string literals when setting `showTZBoundaries`:
180
+
181
+ ```tsx
182
+ import {
183
+ TzGlobePicker,
184
+ TZ_BOUNDARY_MODES,
185
+ type TzBoundaryMode,
186
+ } from "react-tz-globepicker";
187
+
188
+ function App() {
189
+ const [mode, setMode] = React.useState<TzBoundaryMode>(
190
+ TZ_BOUNDARY_MODES.ETCGMT,
191
+ );
192
+
193
+ return (
194
+ <TzGlobePicker
195
+ timezone="Europe/London"
196
+ showTZBoundaries={mode}
197
+ onSelect={(tz) => setMode(TZ_BOUNDARY_MODES.IANA)}
198
+ />
199
+ );
200
+ }
201
+ ```
202
+
203
+ ## Data Loading
204
+
205
+ The component uses bundled timezone data and requires no additional configuration for data loading.
206
+
207
+ ### Updating Globe Data
208
+
209
+ To update the bundled globe data artifacts, run:
210
+
211
+ ```bash
212
+ pnpm gen:globe
213
+ ```
214
+
215
+ This will fetch the latest data from Natural Earth and regenerate the files in `src/data/`, including the split IANA and ETC/GMT geometry artifacts.
216
+
217
+ ## API Reference
218
+
219
+ ### TzGlobePicker Props
220
+
221
+ | Prop | Type | Default | Description |
222
+ | -------------------- | --------------------------------------------------- | ----------- | -------------------------------------------------- |
223
+ | `timezone` | `string \| null` | `null` | IANA timezone identifier (e.g. "America/New_York") |
224
+ | `size` | `number` | `250` | Globe diameter in pixels |
225
+ | `onSelect` | `(timezone: string) => void` | - | Called when a timezone marker is clicked |
226
+ | `showMarkers` | `boolean` | `false` | Whether to render timezone markers |
227
+ | `showTooltips` | `boolean` | `false` | Whether to show hover tooltips on markers |
228
+ | `zoomMarkers` | `boolean` | `false` | When true, markers scale with zoom level |
229
+ | `showTZBoundaries` | `'nautic' \| 'etc-gmt' \| 'iana' \| 'none'` | `'none'` | Timezone boundary visualization mode |
230
+ | `showCountryBorders` | `boolean` | `false` | Whether to render country borders |
231
+ | `markers` | `MarkerEntry[]` | - | Optional explicit marker list |
232
+ | `background` | `string \| React.ReactElement \| null \| undefined` | `undefined` | Background styling (color, JSX, or transparent) |
233
+ | `colors` | `Partial<GlobePalette>` | - | Custom color palette override |
234
+ | `minZoom` | `number` | `MIN_ZOOM` | Minimum zoom level |
235
+ | `maxZoom` | `number` | `MAX_ZOOM` | Maximum zoom level |
236
+ | `initialZoom` | `number` | `1` | Initial zoom level |
237
+ | `style` | `React.CSSProperties` | - | Optional inline styles for the outer container |
238
+ | `className` | `string` | - | Optional CSS class name for the outer container |
239
+
240
+ ## Exported Components
241
+
242
+ ```typescript
243
+ import { TzGlobePicker, SpaceBackground } from "react-tz-globepicker";
244
+ ```
245
+
246
+ - `TzGlobePicker`: Main interactive globe component
247
+ - `SpaceBackground`: Optional starfield-style background component
248
+
249
+ ## Exported Types
250
+
251
+ ```typescript
252
+ import type {
253
+ TzGlobePickerProps,
254
+ TzBoundaryMode,
255
+ GlobeState,
256
+ MarkerEntry,
257
+ Coordinate,
258
+ LatLng,
259
+ Rotation,
260
+ GeoData,
261
+ RenderFn,
262
+ } from "react-tz-globepicker";
263
+ ```
264
+
265
+ ## Exported Constants
266
+
267
+ ```typescript
268
+ import {
269
+ TZ_BOUNDARY_MODES,
270
+ COLORS,
271
+ TILT,
272
+ GRATICULE_STEP,
273
+ MAX_BOUNDARY_AREA,
274
+ HIT_RADIUS,
275
+ CLICK_THRESHOLD,
276
+ FLY_DURATION,
277
+ DRAG_SENSITIVITY,
278
+ INERTIA_FRICTION,
279
+ INERTIA_MIN_VELOCITY,
280
+ MIN_ZOOM,
281
+ MAX_ZOOM,
282
+ ZOOM_SENSITIVITY,
283
+ MAX_LATITUDE,
284
+ } from "react-tz-globepicker";
285
+ ```
286
+
287
+ ## Exported Utils
288
+
289
+ ```typescript
290
+ import {
291
+ formatUtcOffset,
292
+ getSubsolarPoint,
293
+ getTimezoneCenter,
294
+ getUtcOffsetMinutes,
295
+ getUtcOffsetHour,
296
+ buildMarkerList,
297
+ CANONICAL_MARKERS,
298
+ TIMEZONE_COORDINATES,
299
+ mapToCanonicalTz,
300
+ utcOffsetToLongitude,
301
+ useGlobeState,
302
+ } from "react-tz-globepicker";
303
+ ```
304
+
305
+ ### Utility Functions
306
+
307
+ #### `buildMarkerList(allowed?)`
308
+
309
+ Builds a list of timezone markers from coordinate data. Used to generate markers for the globe.
310
+
311
+ ```tsx
312
+ import { buildMarkerList } from "react-tz-globepicker";
313
+
314
+ // Get markers for all timezones
315
+ const allMarkers = buildMarkerList();
316
+
317
+ // Get markers for specific timezones only
318
+ const timezones = ["America/New_York", "Europe/London", "Asia/Tokyo"];
319
+ const filteredMarkers = buildMarkerList(timezones);
320
+
321
+ // Example: Generate markers from timezone objects with value property
322
+ const timezoneObjects = [
323
+ { value: "America/New_York", label: "New York" },
324
+ { value: "Europe/London", label: "London" },
325
+ ];
326
+ const markers = buildMarkerList(timezoneObjects.map((tz) => tz.value));
327
+ ```
328
+
329
+ #### `CANONICAL_MARKERS`
330
+
331
+ Pre-built subset of markers filtered to canonical timezone regions (~64 entries).
332
+
333
+ ```tsx
334
+ import { CANONICAL_MARKERS } from "react-tz-globepicker";
335
+
336
+ // Use canonical markers for better performance
337
+ <TzGlobePicker markers={CANONICAL_MARKERS} />;
338
+ ```
339
+
340
+ #### `TIMEZONE_COORDINATES`
341
+
342
+ Lookup map from IANA timezone ID to approximate [lat, lng] centroid coordinates.
343
+
344
+ ```tsx
345
+ import { TIMEZONE_COORDINATES } from "react-tz-globepicker";
346
+
347
+ const [lat, lng] = TIMEZONE_COORDINATES["America/New_York"] ?? [0, 0];
348
+ console.log(`New York is at ${lat}°, ${lng}°`);
349
+ ```
350
+
351
+ #### `getUtcOffsetMinutes(timezone)` & `getUtcOffsetHour(timezone)`
352
+
353
+ Get current UTC offset for a timezone.
354
+
355
+ ```tsx
356
+ import { getUtcOffsetMinutes, getUtcOffsetHour } from "react-tz-globepicker";
357
+
358
+ const offsetMinutes = getUtcOffsetMinutes("America/New_York"); // -300 or -240 (DST)
359
+ const offsetHours = getUtcOffsetHour("Asia/Kolkata"); // 5.5
360
+ ```
361
+
362
+ #### `mapToCanonicalTz(timezone)`
363
+
364
+ Map any IANA timezone to its canonical timezone-boundary-builder region.
365
+
366
+ ```tsx
367
+ import { mapToCanonicalTz } from "react-tz-globepicker";
368
+
369
+ const canonical = mapToCanonicalTz("America/Indiana/Indianapolis");
370
+ // Returns: 'America/New_York'
371
+ ```
372
+
373
+ #### `utcOffsetToLongitude(offset)`
374
+
375
+ Convert UTC offset to longitude center.
376
+
377
+ ```tsx
378
+ import { utcOffsetToLongitude } from "react-tz-globepicker";
379
+
380
+ const longitude = utcOffsetToLongitude(-5); // -75 (Eastern US)
381
+ ```
382
+
383
+ ## Development
384
+
385
+ ```bash
386
+ # Install dependencies
387
+ pnpm install
388
+
389
+ # Run linting
390
+ pnpm lint
391
+
392
+ # Run type checking
393
+ pnpm type-check
394
+
395
+ # Format code
396
+ pnpm format
397
+
398
+ # Format check
399
+ pnpm format:check
400
+
401
+ # Update globe data
402
+ pnpm gen:globe
403
+ ```
404
+
405
+ ## License
406
+
407
+ MIT
408
+
409
+ ## Data Sources
410
+
411
+ This package uses high-quality, open geodata from:
412
+
413
+ - [Natural Earth 10m Time Zones](https://github.com/nvkelso/natural-earth-vector): authoritative, closed-polygon timezone boundaries. See [ne_10m_time_zones.geojson](https://github.com/nvkelso/natural-earth-vector/blob/master/geojson/ne_10m_time_zones.geojson).
414
+ - [visionscarto-world-atlas](https://github.com/visionscarto/world-atlas): simplified world country boundaries (110m resolution).
415
+
416
+ Data is downloaded and processed automatically by the `pnpm gen:globe` script. See `scripts/update-globe-data.ts` for details.
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@mmerlone/react-tz-globepicker",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "workspaces": [
6
+ "demo"
7
+ ],
8
+ "description": "Interactive 3D globe component for timezone selection with React",
9
+ "homepage": "https://ywybase.vercel.app/",
10
+ "author": "Marcio Merlone <mmerlone@gmail.com> (https://mmerlone.dev.br)",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/mmerlone/react-tz-globepicker.git"
14
+ },
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "scripts"
27
+ ],
28
+ "peerDependencies": {
29
+ "react": "^18.0.0",
30
+ "react-dom": "^18.0.0"
31
+ },
32
+ "dependencies": {
33
+ "d3-drag": "^3.0.0",
34
+ "d3-geo": "^3.1.1",
35
+ "d3-selection": "^3.0.0",
36
+ "hex-rgb": "^5.0.0",
37
+ "topojson-client": "^3.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@eslint/js": "^9.39.1",
41
+ "@types/d3-drag": "^3.0.7",
42
+ "@types/d3-geo": "^3.1.0",
43
+ "@types/d3-selection": "^3.0.11",
44
+ "@types/geojson": "^7946.0.13",
45
+ "@types/node": "^22",
46
+ "@types/react": "^18",
47
+ "@types/react-dom": "^18",
48
+ "@types/topojson-client": "^3.1.5",
49
+ "@types/topojson-server": "^3.0.4",
50
+ "@types/topojson-simplify": "^3.0.3",
51
+ "@types/topojson-specification": "^1.0.5",
52
+ "@types/yauzl": "^2.10.3",
53
+ "@typescript-eslint/eslint-plugin": "^8.48.0",
54
+ "@typescript-eslint/parser": "^8.48.0",
55
+ "eslint": "^9.39.1",
56
+ "eslint-config-prettier": "^10.1.8",
57
+ "eslint-plugin-react": "^7.37.5",
58
+ "eslint-plugin-react-hooks": "^7.0.1",
59
+ "globals": "^17.3.0",
60
+ "husky": "^8.0.0",
61
+ "prettier": "^3.3.3",
62
+ "topojson-server": "^3.0.1",
63
+ "topojson-simplify": "^3.0.3",
64
+ "tsx": "^4.7.1",
65
+ "typescript": "^5.3.0",
66
+ "visionscarto-world-atlas": "^1.0.0",
67
+ "yauzl": "^3.2.1"
68
+ },
69
+ "keywords": [
70
+ "react",
71
+ "timezone",
72
+ "globe",
73
+ "3d",
74
+ "interactive",
75
+ "d3",
76
+ "geojson"
77
+ ],
78
+ "license": "MIT",
79
+ "scripts": {
80
+ "lint": "eslint .",
81
+ "lint:fix": "eslint . --fix",
82
+ "type-check": "tsc --noEmit",
83
+ "build": "tsc --project tsconfig.build.json",
84
+ "clean": "rm -rf dist",
85
+ "prebuild": "pnpm clean",
86
+ "format": "prettier --write .",
87
+ "format:check": "prettier --check .",
88
+ "gen:globe": "tsx scripts/update-globe-data.ts",
89
+ "test:unit": "tsx scripts/test-iana-to-etc.ts",
90
+ "demo": "pnpm --filter ./demo dev"
91
+ }
92
+ }
@@ -0,0 +1,52 @@
1
+ import { ianaToEtc, offsetKeyFromEtc } from "../src/utils/timezoneMapping";
2
+
3
+ const cases: Array<[string, string]> = [
4
+ ["America/New_York", "UTC-05:00"],
5
+ ["America/Los_Angeles", "UTC-08:00"],
6
+ ["Asia/Kolkata", "UTC+05:30"],
7
+ ["Pacific/Chatham", "UTC+12:45"],
8
+ ["UTC", "UTC+00:00"],
9
+ ];
10
+
11
+ let failed = 0;
12
+ for (const [input, expected] of cases) {
13
+ try {
14
+ const got = ianaToEtc(input);
15
+ if (got !== expected) {
16
+ console.error(`FAIL: ${input} -> expected ${expected}, got ${got}`);
17
+ failed++;
18
+ } else {
19
+ console.log(`ok: ${input} -> ${got}`);
20
+ }
21
+ } catch (e) {
22
+ console.error(`ERROR: ${input} threw`, e);
23
+ failed++;
24
+ }
25
+ }
26
+
27
+ // Basic etc parsing test
28
+ const etcCases: Array<[string, string]> = [
29
+ ["Etc/GMT+5", "UTC-05:00"],
30
+ ["Etc/GMT-3", "UTC+03:00"],
31
+ ["GMT+05:30", "UTC+05:30"],
32
+ ];
33
+ for (const [input, expected] of etcCases) {
34
+ try {
35
+ const got = offsetKeyFromEtc(input);
36
+ if (got !== expected) {
37
+ console.error(`FAIL: ${input} -> expected ${expected}, got ${got}`);
38
+ failed++;
39
+ } else {
40
+ console.log(`ok: ${input} -> ${got}`);
41
+ }
42
+ } catch (e) {
43
+ console.error(`ERROR: ${input} threw`, e);
44
+ failed++;
45
+ }
46
+ }
47
+
48
+ if (failed > 0) {
49
+ console.error(`${failed} tests failed`);
50
+ process.exit(1);
51
+ }
52
+ console.log("All tests passed");