@geophrase/core 1.3.12 → 1.4.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +123 -71
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.3.12",
3
+ "version": "1.4.1",
4
4
  "description": "Drop-in address picker widget for Indian e-commerce checkouts. Captures structured addresses with coordinates to reduce RTO.",
5
5
  "license": "MIT",
6
6
  "author": "Geophrase",
package/src/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  class Geophrase {
2
2
  static _instanceCounter = 0;
3
3
 
4
+ // Widget background colors, kept in sync with the connect app's theme.
5
+ static _BG_LIGHT = '#ffffff';
6
+ static _BG_DARK = '#121212';
7
+
4
8
  constructor(options = {}) {
5
9
  if (typeof document === 'undefined') {
6
10
  this._isSSR = true;
@@ -49,10 +53,9 @@ class Geophrase {
49
53
  this.apiBase = endpoints.apiBase || 'https://api.geophrase.com';
50
54
 
51
55
  this._boundHandleMessage = this._handleMessage.bind(this);
52
- this._prevBodyOverflow = null;
53
- this._prevHtmlBg = null;
54
- this._prevBodyBg = null;
55
- this._hostBgApplied = false;
56
+ // Snapshot of host state we mutate on open(), restored on close()/destroy().
57
+ // null when not applied; an object of saved values otherwise.
58
+ this._savedHostState = null;
56
59
 
57
60
  // 6. Pre-compute the secure widget URL
58
61
  const url = new URL(this.widgetOrigin);
@@ -63,8 +66,8 @@ class Geophrase {
63
66
  }
64
67
 
65
68
  _getIframeBgCSS() {
66
- const light = '#ffffff';
67
- const dark = '#121212';
69
+ const light = Geophrase._BG_LIGHT;
70
+ const dark = Geophrase._BG_DARK;
68
71
 
69
72
  if (this.theme === 'light') {
70
73
  return `#${this.iframeId} { background-color: ${light}; }`;
@@ -82,51 +85,91 @@ class Geophrase {
82
85
  }
83
86
 
84
87
  _getWidgetBg() {
85
- const light = '#ffffff';
86
- const dark = '#121212';
87
- if (this.theme === 'light') return light;
88
- if (this.theme === 'dark') return dark;
88
+ if (this.theme === 'light') return Geophrase._BG_LIGHT;
89
+ if (this.theme === 'dark') return Geophrase._BG_DARK;
89
90
  // 'system' or unspecified → resolve at open time
90
- return window.matchMedia?.('(prefers-color-scheme: dark)').matches ? dark : light;
91
+ const prefersDark = window.matchMedia?.('(prefers-color-scheme: dark)').matches;
92
+ return prefersDark ? Geophrase._BG_DARK : Geophrase._BG_LIGHT;
91
93
  }
92
94
 
93
- _applyHostBg() {
94
- if (this._hostBgApplied) return;
95
+ /**
96
+ * Mutates host-document state needed while the modal is open:
97
+ * - body overflow: prevent background scroll
98
+ * - html/body background: paint iOS safe-area zones to match the widget
99
+ * - html color-scheme: tell the browser the page is currently light/dark
100
+ * - theme-color meta: explicit hint for iOS Safari chrome
101
+ * All previous values are snapshotted and restored by _restoreHostOverrides().
102
+ */
103
+ _applyHostOverrides() {
104
+ if (this._savedHostState) return;
105
+
95
106
  const html = document.documentElement;
96
- this._prevHtmlBg = html.style.backgroundColor;
97
- this._prevBodyBg = document.body.style.backgroundColor;
98
- this._prevHtmlColorScheme = html.style.colorScheme;
107
+ const body = document.body;
99
108
  const bg = this._getWidgetBg();
100
- const scheme = bg === '#121212' ? 'dark' : 'light';
109
+ const scheme = bg === Geophrase._BG_DARK ? 'dark' : 'light';
110
+
111
+ this._savedHostState = {
112
+ bodyOverflow: body.style.overflow,
113
+ htmlBg: html.style.backgroundColor,
114
+ bodyBg: body.style.backgroundColor,
115
+ htmlColorScheme: html.style.colorScheme,
116
+ };
117
+
118
+ body.style.overflow = 'hidden';
101
119
  html.style.backgroundColor = bg;
102
- document.body.style.backgroundColor = bg;
120
+ body.style.backgroundColor = bg;
103
121
  html.style.colorScheme = scheme;
104
122
 
105
- // Add a theme-color meta as a final lever for iOS Safari chrome.
106
- const tcId = `${this.styleId}-tc`;
107
- let tc = document.getElementById(tcId);
123
+ let tc = document.getElementById(this._themeColorId());
108
124
  if (!tc) {
109
125
  tc = document.createElement('meta');
110
- tc.id = tcId;
126
+ tc.id = this._themeColorId();
111
127
  tc.name = 'theme-color';
112
128
  document.head.appendChild(tc);
113
129
  }
114
130
  tc.content = bg;
115
-
116
- this._hostBgApplied = true;
117
131
  }
118
132
 
119
- _restoreHostBg() {
120
- if (!this._hostBgApplied) return;
133
+ _restoreHostOverrides() {
134
+ if (!this._savedHostState) return;
135
+
121
136
  const html = document.documentElement;
122
- html.style.backgroundColor = this._prevHtmlBg ?? '';
123
- document.body.style.backgroundColor = this._prevBodyBg ?? '';
124
- html.style.colorScheme = this._prevHtmlColorScheme ?? '';
125
- document.getElementById(`${this.styleId}-tc`)?.remove();
126
- this._prevHtmlBg = null;
127
- this._prevBodyBg = null;
128
- this._prevHtmlColorScheme = null;
129
- this._hostBgApplied = false;
137
+ const body = document.body;
138
+ const saved = this._savedHostState;
139
+
140
+ body.style.overflow = saved.bodyOverflow;
141
+ html.style.backgroundColor = saved.htmlBg;
142
+ body.style.backgroundColor = saved.bodyBg;
143
+ html.style.colorScheme = saved.htmlColorScheme;
144
+
145
+ document.getElementById(this._themeColorId())?.remove();
146
+
147
+ this._savedHostState = null;
148
+ }
149
+
150
+ _themeColorId() {
151
+ return `${this.styleId}-tc`;
152
+ }
153
+
154
+ _loaderId() {
155
+ return `${this.styleId}-loader`;
156
+ }
157
+
158
+ _spinKeyframesName() {
159
+ return `${this.styleId}-spin`;
160
+ }
161
+
162
+ /**
163
+ * Hide the iframe until its document has actually painted. Browsers render
164
+ * an unloaded iframe as a white rectangle, which (a) flashes visibly and
165
+ * (b) on iOS Safari can lock the chrome appearance to light because Safari
166
+ * samples the page at the moment the modal appears.
167
+ */
168
+ _hideIframeUntilLoaded(iframe) {
169
+ iframe.style.opacity = '0';
170
+ iframe.addEventListener('load', () => {
171
+ iframe.style.opacity = '';
172
+ }, { once: true });
130
173
  }
131
174
 
132
175
  _safeCall(fn, payload) {
@@ -153,7 +196,22 @@ class Geophrase {
153
196
  }
154
197
  #${this.overlayId}.geophrase-active { opacity: 1; visibility: visible; }
155
198
 
156
- #${this.iframeId} { width: 100%; height: 100%; border: none; }
199
+ #${this._loaderId()} {
200
+ position: absolute;
201
+ top: 50%; left: 50%;
202
+ width: 40px; height: 40px;
203
+ margin: -20px 0 0 -20px;
204
+ border: 3px solid rgba(255, 255, 255, 0.25);
205
+ border-top-color: rgba(255, 255, 255, 0.9);
206
+ border-radius: 50%;
207
+ animation: ${this._spinKeyframesName()} 0.8s linear infinite;
208
+ }
209
+ @keyframes ${this._spinKeyframesName()} { to { transform: rotate(360deg); } }
210
+
211
+ #${this.iframeId} {
212
+ width: 100%; height: 100%; border: none;
213
+ position: relative; z-index: 1;
214
+ }
157
215
  ${this._getIframeBgCSS()}
158
216
 
159
217
  @media (min-width: 768px) {
@@ -169,11 +227,17 @@ class Geophrase {
169
227
  const overlay = document.createElement('div');
170
228
  overlay.id = this.overlayId;
171
229
 
230
+ // Spinner shown while the iframe is loading. The iframe stacks above
231
+ // it (z-index: 1) and covers it once paint completes — no state to toggle.
232
+ const loader = document.createElement('div');
233
+ loader.id = this._loaderId();
234
+ overlay.appendChild(loader);
235
+
172
236
  const iframe = document.createElement('iframe');
173
237
  iframe.id = this.iframeId;
174
238
  iframe.allow = 'geolocation';
175
-
176
239
  overlay.appendChild(iframe);
240
+
177
241
  document.body.appendChild(overlay);
178
242
 
179
243
  window.removeEventListener('message', this._boundHandleMessage);
@@ -187,25 +251,11 @@ class Geophrase {
187
251
 
188
252
  const iframe = document.getElementById(this.iframeId);
189
253
  if (iframe && !iframe.hasAttribute('src')) {
190
- // Hide the iframe until its document has actually painted.
191
- // Browsers render an unloaded iframe as a white rectangle, which
192
- // (a) causes a visible white flash and (b) on iOS Safari can lock
193
- // the chrome appearance to light because Safari samples the page
194
- // at the moment the modal appears.
195
- iframe.style.opacity = '0';
196
- iframe.addEventListener('load', () => {
197
- iframe.style.opacity = '';
198
- }, { once: true });
254
+ this._hideIframeUntilLoaded(iframe);
199
255
  iframe.src = this.widgetUrl;
200
256
  }
201
257
 
202
- // Preserve merchant's existing overflow; idempotent under repeated open()
203
- if (this._prevBodyOverflow === null) {
204
- this._prevBodyOverflow = document.body.style.overflow;
205
- }
206
- document.body.style.overflow = 'hidden';
207
-
208
- this._applyHostBg();
258
+ this._applyHostOverrides();
209
259
 
210
260
  document.getElementById(this.overlayId)?.classList.add('geophrase-active');
211
261
  }
@@ -213,10 +263,7 @@ class Geophrase {
213
263
  close() {
214
264
  if (this._isSSR) return;
215
265
 
216
- document.body.style.overflow = this._prevBodyOverflow ?? '';
217
- this._prevBodyOverflow = null;
218
-
219
- this._restoreHostBg();
266
+ this._restoreHostOverrides();
220
267
 
221
268
  document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
222
269
 
@@ -230,10 +277,7 @@ class Geophrase {
230
277
  document.getElementById(this.overlayId)?.remove();
231
278
  document.getElementById(this.styleId)?.remove();
232
279
 
233
- document.body.style.overflow = this._prevBodyOverflow ?? '';
234
- this._prevBodyOverflow = null;
235
-
236
- this._restoreHostBg();
280
+ this._restoreHostOverrides();
237
281
 
238
282
  window.removeEventListener('message', this._boundHandleMessage);
239
283
 
@@ -261,18 +305,26 @@ class Geophrase {
261
305
  }
262
306
 
263
307
  if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
264
- this.close();
265
-
266
- // Server mode: hand the raw token to the merchant for backend exchange
308
+ // Server mode: nothing to wait for — close immediately and hand
309
+ // the raw token to the merchant for backend exchange.
267
310
  if (this.mode === 'server') {
311
+ this.close();
268
312
  this._safeCall(this.onSuccess, { token: data.token });
269
313
  return;
270
314
  }
271
315
 
272
- // Client mode: resolve the token via the public API
316
+ // Client mode: keep the overlay + spinner visible while we exchange
317
+ // the token for the resolved address. Closing now would leave the
318
+ // merchant's page blank for the duration of the API round-trip.
319
+ iframe.style.opacity = '0';
320
+
273
321
  const controller = new AbortController();
274
322
  const timeoutId = setTimeout(() => controller.abort(), 15000);
275
323
 
324
+ // Build the callback to fire after we close the overlay, so the
325
+ // merchant sees the modal disappear before their handler runs.
326
+ let callback = null;
327
+
276
328
  try {
277
329
  const response = await fetch(`${this.apiBase}/business/resolve/`, {
278
330
  method: 'POST',
@@ -286,20 +338,18 @@ class Geophrase {
286
338
 
287
339
  if (!response.ok) {
288
340
  const errorData = await response.json().catch(() => ({}));
289
- this._safeCall(this.onError, {
341
+ callback = () => this._safeCall(this.onError, {
290
342
  type: 'API_ERROR',
291
343
  status: response.status,
292
344
  message: errorData.message || 'Geophrase API resolution failed'
293
345
  });
294
- return;
346
+ } else {
347
+ const responseData = await response.json();
348
+ callback = () => this._safeCall(this.onSuccess, responseData);
295
349
  }
296
-
297
- const responseData = await response.json();
298
- this._safeCall(this.onSuccess, responseData);
299
-
300
350
  } catch (error) {
301
351
  console.error('Geophrase Resolution Error:', error);
302
- this._safeCall(this.onError, {
352
+ callback = () => this._safeCall(this.onError, {
303
353
  type: 'NETWORK_ERROR',
304
354
  message: error.name === 'AbortError'
305
355
  ? 'Geophrase API request timed out'
@@ -307,6 +357,8 @@ class Geophrase {
307
357
  });
308
358
  } finally {
309
359
  clearTimeout(timeoutId);
360
+ this.close();
361
+ callback?.();
310
362
  }
311
363
  }
312
364
  }