@geophrase/core 1.3.9 → 1.3.11

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 +28 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geophrase/core",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
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
@@ -50,6 +50,9 @@ class Geophrase {
50
50
 
51
51
  this._boundHandleMessage = this._handleMessage.bind(this);
52
52
  this._prevBodyOverflow = null;
53
+ this._prevHtmlBg = null;
54
+ this._prevBodyBg = null;
55
+ this._hostBgApplied = false;
53
56
 
54
57
  // 6. Pre-compute the secure widget URL
55
58
  const url = new URL(this.widgetOrigin);
@@ -78,35 +81,32 @@ class Geophrase {
78
81
  `;
79
82
  }
80
83
 
81
- _applyHostThemeColor() {
84
+ _getWidgetBg() {
82
85
  const light = '#ffffff';
83
86
  const dark = '#121212';
87
+ if (this.theme === 'light') return light;
88
+ if (this.theme === 'dark') return dark;
89
+ // 'system' or unspecified → resolve at open time
90
+ return window.matchMedia?.('(prefers-color-scheme: dark)').matches ? dark : light;
91
+ }
84
92
 
85
- const ensure = (id, media, color) => {
86
- let el = document.getElementById(id);
87
- if (!el) {
88
- el = document.createElement('meta');
89
- el.id = id;
90
- el.name = 'theme-color';
91
- if (media) el.media = media;
92
- document.head.appendChild(el);
93
- }
94
- el.content = color;
95
- };
96
-
97
- if (this.theme === 'light') {
98
- ensure(`${this.styleId}-tc`, '', light);
99
- } else if (this.theme === 'dark') {
100
- ensure(`${this.styleId}-tc`, '', dark);
101
- } else {
102
- ensure(`${this.styleId}-tc-light`, '(prefers-color-scheme: light)', light);
103
- ensure(`${this.styleId}-tc-dark`, '(prefers-color-scheme: dark)', dark);
104
- }
93
+ _applyHostBg() {
94
+ if (this._hostBgApplied) return;
95
+ this._prevHtmlBg = document.documentElement.style.backgroundColor;
96
+ this._prevBodyBg = document.body.style.backgroundColor;
97
+ const bg = this._getWidgetBg();
98
+ document.documentElement.style.backgroundColor = bg;
99
+ document.body.style.backgroundColor = bg;
100
+ this._hostBgApplied = true;
105
101
  }
106
102
 
107
- _removeHostThemeColor() {
108
- [`${this.styleId}-tc`, `${this.styleId}-tc-light`, `${this.styleId}-tc-dark`]
109
- .forEach(id => document.getElementById(id)?.remove());
103
+ _restoreHostBg() {
104
+ if (!this._hostBgApplied) return;
105
+ document.documentElement.style.backgroundColor = this._prevHtmlBg ?? '';
106
+ document.body.style.backgroundColor = this._prevBodyBg ?? '';
107
+ this._prevHtmlBg = null;
108
+ this._prevBodyBg = null;
109
+ this._hostBgApplied = false;
110
110
  }
111
111
 
112
112
  _safeCall(fn, payload) {
@@ -176,7 +176,7 @@ class Geophrase {
176
176
  }
177
177
  document.body.style.overflow = 'hidden';
178
178
 
179
- this._applyHostThemeColor();
179
+ this._applyHostBg();
180
180
 
181
181
  document.getElementById(this.overlayId)?.classList.add('geophrase-active');
182
182
  }
@@ -187,7 +187,7 @@ class Geophrase {
187
187
  document.body.style.overflow = this._prevBodyOverflow ?? '';
188
188
  this._prevBodyOverflow = null;
189
189
 
190
- this._removeHostThemeColor();
190
+ this._restoreHostBg();
191
191
 
192
192
  document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
193
193
 
@@ -200,11 +200,12 @@ class Geophrase {
200
200
 
201
201
  document.getElementById(this.overlayId)?.remove();
202
202
  document.getElementById(this.styleId)?.remove();
203
- this._removeHostThemeColor();
204
203
 
205
204
  document.body.style.overflow = this._prevBodyOverflow ?? '';
206
205
  this._prevBodyOverflow = null;
207
206
 
207
+ this._restoreHostBg();
208
+
208
209
  window.removeEventListener('message', this._boundHandleMessage);
209
210
 
210
211
  this.onSuccess = null;