@geophrase/core 1.3.11 → 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 +84 -40
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,31 +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
|
-
|
|
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
|
+
|
|
106
|
+
const html = document.documentElement;
|
|
107
|
+
const body = document.body;
|
|
97
108
|
const bg = this._getWidgetBg();
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this.
|
|
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';
|
|
119
|
+
html.style.backgroundColor = bg;
|
|
120
|
+
body.style.backgroundColor = bg;
|
|
121
|
+
html.style.colorScheme = scheme;
|
|
122
|
+
|
|
123
|
+
let tc = document.getElementById(this._themeColorId());
|
|
124
|
+
if (!tc) {
|
|
125
|
+
tc = document.createElement('meta');
|
|
126
|
+
tc.id = this._themeColorId();
|
|
127
|
+
tc.name = 'theme-color';
|
|
128
|
+
document.head.appendChild(tc);
|
|
129
|
+
}
|
|
130
|
+
tc.content = bg;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
_restoreHostOverrides() {
|
|
134
|
+
if (!this._savedHostState) return;
|
|
135
|
+
|
|
136
|
+
const html = document.documentElement;
|
|
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`;
|
|
101
152
|
}
|
|
102
153
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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 });
|
|
110
165
|
}
|
|
111
166
|
|
|
112
167
|
_safeCall(fn, payload) {
|
|
@@ -167,16 +222,11 @@ class Geophrase {
|
|
|
167
222
|
|
|
168
223
|
const iframe = document.getElementById(this.iframeId);
|
|
169
224
|
if (iframe && !iframe.hasAttribute('src')) {
|
|
225
|
+
this._hideIframeUntilLoaded(iframe);
|
|
170
226
|
iframe.src = this.widgetUrl;
|
|
171
227
|
}
|
|
172
228
|
|
|
173
|
-
|
|
174
|
-
if (this._prevBodyOverflow === null) {
|
|
175
|
-
this._prevBodyOverflow = document.body.style.overflow;
|
|
176
|
-
}
|
|
177
|
-
document.body.style.overflow = 'hidden';
|
|
178
|
-
|
|
179
|
-
this._applyHostBg();
|
|
229
|
+
this._applyHostOverrides();
|
|
180
230
|
|
|
181
231
|
document.getElementById(this.overlayId)?.classList.add('geophrase-active');
|
|
182
232
|
}
|
|
@@ -184,10 +234,7 @@ class Geophrase {
|
|
|
184
234
|
close() {
|
|
185
235
|
if (this._isSSR) return;
|
|
186
236
|
|
|
187
|
-
|
|
188
|
-
this._prevBodyOverflow = null;
|
|
189
|
-
|
|
190
|
-
this._restoreHostBg();
|
|
237
|
+
this._restoreHostOverrides();
|
|
191
238
|
|
|
192
239
|
document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
|
|
193
240
|
|
|
@@ -201,10 +248,7 @@ class Geophrase {
|
|
|
201
248
|
document.getElementById(this.overlayId)?.remove();
|
|
202
249
|
document.getElementById(this.styleId)?.remove();
|
|
203
250
|
|
|
204
|
-
|
|
205
|
-
this._prevBodyOverflow = null;
|
|
206
|
-
|
|
207
|
-
this._restoreHostBg();
|
|
251
|
+
this._restoreHostOverrides();
|
|
208
252
|
|
|
209
253
|
window.removeEventListener('message', this._boundHandleMessage);
|
|
210
254
|
|