@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.
- package/package.json +1 -1
- package/src/index.js +73 -58
package/package.json
CHANGED
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
|
-
|
|
53
|
-
|
|
54
|
-
this.
|
|
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 =
|
|
67
|
-
const dark =
|
|
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
|
-
|
|
86
|
-
|
|
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
|
-
|
|
91
|
+
const prefersDark = window.matchMedia?.('(prefers-color-scheme: dark)').matches;
|
|
92
|
+
return prefersDark ? Geophrase._BG_DARK : Geophrase._BG_LIGHT;
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
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 ===
|
|
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
|
-
|
|
120
|
+
body.style.backgroundColor = bg;
|
|
103
121
|
html.style.colorScheme = scheme;
|
|
104
122
|
|
|
105
|
-
|
|
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 =
|
|
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
|
-
|
|
120
|
-
if (!this.
|
|
133
|
+
_restoreHostOverrides() {
|
|
134
|
+
if (!this._savedHostState) return;
|
|
135
|
+
|
|
121
136
|
const html = document.documentElement;
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
234
|
-
this._prevBodyOverflow = null;
|
|
235
|
-
|
|
236
|
-
this._restoreHostBg();
|
|
251
|
+
this._restoreHostOverrides();
|
|
237
252
|
|
|
238
253
|
window.removeEventListener('message', this._boundHandleMessage);
|
|
239
254
|
|