@geophrase/core 1.3.12 → 1.4.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +73 -58
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.3.12",
3
+ "version": "1.4.0",
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,83 @@ 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
+ /**
155
+ * Hide the iframe until its document has actually painted. Browsers render
156
+ * an unloaded iframe as a white rectangle, which (a) flashes visibly and
157
+ * (b) on iOS Safari can lock the chrome appearance to light because Safari
158
+ * samples the page at the moment the modal appears.
159
+ */
160
+ _hideIframeUntilLoaded(iframe) {
161
+ iframe.style.opacity = '0';
162
+ iframe.addEventListener('load', () => {
163
+ iframe.style.opacity = '';
164
+ }, { once: true });
130
165
  }
131
166
 
132
167
  _safeCall(fn, payload) {
@@ -187,25 +222,11 @@ class Geophrase {
187
222
 
188
223
  const iframe = document.getElementById(this.iframeId);
189
224
  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 });
225
+ this._hideIframeUntilLoaded(iframe);
199
226
  iframe.src = this.widgetUrl;
200
227
  }
201
228
 
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();
229
+ this._applyHostOverrides();
209
230
 
210
231
  document.getElementById(this.overlayId)?.classList.add('geophrase-active');
211
232
  }
@@ -213,10 +234,7 @@ class Geophrase {
213
234
  close() {
214
235
  if (this._isSSR) return;
215
236
 
216
- document.body.style.overflow = this._prevBodyOverflow ?? '';
217
- this._prevBodyOverflow = null;
218
-
219
- this._restoreHostBg();
237
+ this._restoreHostOverrides();
220
238
 
221
239
  document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
222
240
 
@@ -230,10 +248,7 @@ class Geophrase {
230
248
  document.getElementById(this.overlayId)?.remove();
231
249
  document.getElementById(this.styleId)?.remove();
232
250
 
233
- document.body.style.overflow = this._prevBodyOverflow ?? '';
234
- this._prevBodyOverflow = null;
235
-
236
- this._restoreHostBg();
251
+ this._restoreHostOverrides();
237
252
 
238
253
  window.removeEventListener('message', this._boundHandleMessage);
239
254