@geophrase/core 1.3.10 → 1.3.12
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 +66 -0
package/package.json
CHANGED
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,6 +81,54 @@ class Geophrase {
|
|
|
78
81
|
`;
|
|
79
82
|
}
|
|
80
83
|
|
|
84
|
+
_getWidgetBg() {
|
|
85
|
+
const light = '#ffffff';
|
|
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
|
+
}
|
|
92
|
+
|
|
93
|
+
_applyHostBg() {
|
|
94
|
+
if (this._hostBgApplied) return;
|
|
95
|
+
const html = document.documentElement;
|
|
96
|
+
this._prevHtmlBg = html.style.backgroundColor;
|
|
97
|
+
this._prevBodyBg = document.body.style.backgroundColor;
|
|
98
|
+
this._prevHtmlColorScheme = html.style.colorScheme;
|
|
99
|
+
const bg = this._getWidgetBg();
|
|
100
|
+
const scheme = bg === '#121212' ? 'dark' : 'light';
|
|
101
|
+
html.style.backgroundColor = bg;
|
|
102
|
+
document.body.style.backgroundColor = bg;
|
|
103
|
+
html.style.colorScheme = scheme;
|
|
104
|
+
|
|
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);
|
|
108
|
+
if (!tc) {
|
|
109
|
+
tc = document.createElement('meta');
|
|
110
|
+
tc.id = tcId;
|
|
111
|
+
tc.name = 'theme-color';
|
|
112
|
+
document.head.appendChild(tc);
|
|
113
|
+
}
|
|
114
|
+
tc.content = bg;
|
|
115
|
+
|
|
116
|
+
this._hostBgApplied = true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_restoreHostBg() {
|
|
120
|
+
if (!this._hostBgApplied) return;
|
|
121
|
+
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;
|
|
130
|
+
}
|
|
131
|
+
|
|
81
132
|
_safeCall(fn, payload) {
|
|
82
133
|
if (typeof fn !== 'function') return;
|
|
83
134
|
try {
|
|
@@ -136,6 +187,15 @@ class Geophrase {
|
|
|
136
187
|
|
|
137
188
|
const iframe = document.getElementById(this.iframeId);
|
|
138
189
|
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 });
|
|
139
199
|
iframe.src = this.widgetUrl;
|
|
140
200
|
}
|
|
141
201
|
|
|
@@ -145,6 +205,8 @@ class Geophrase {
|
|
|
145
205
|
}
|
|
146
206
|
document.body.style.overflow = 'hidden';
|
|
147
207
|
|
|
208
|
+
this._applyHostBg();
|
|
209
|
+
|
|
148
210
|
document.getElementById(this.overlayId)?.classList.add('geophrase-active');
|
|
149
211
|
}
|
|
150
212
|
|
|
@@ -154,6 +216,8 @@ class Geophrase {
|
|
|
154
216
|
document.body.style.overflow = this._prevBodyOverflow ?? '';
|
|
155
217
|
this._prevBodyOverflow = null;
|
|
156
218
|
|
|
219
|
+
this._restoreHostBg();
|
|
220
|
+
|
|
157
221
|
document.getElementById(this.overlayId)?.classList.remove('geophrase-active');
|
|
158
222
|
|
|
159
223
|
const iframe = document.getElementById(this.iframeId);
|
|
@@ -169,6 +233,8 @@ class Geophrase {
|
|
|
169
233
|
document.body.style.overflow = this._prevBodyOverflow ?? '';
|
|
170
234
|
this._prevBodyOverflow = null;
|
|
171
235
|
|
|
236
|
+
this._restoreHostBg();
|
|
237
|
+
|
|
172
238
|
window.removeEventListener('message', this._boundHandleMessage);
|
|
173
239
|
|
|
174
240
|
this.onSuccess = null;
|