-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathviewport.js
More file actions
64 lines (52 loc) · 1.86 KB
/
Copy pathviewport.js
File metadata and controls
64 lines (52 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function(root, factory) {
const api = factory();
if (typeof module === 'object' && module.exports) {
module.exports = api;
}
root.ViewportHeight = api;
})(typeof globalThis !== 'undefined' ? globalThis : this, function() {
function getVisibleViewportHeight(win) {
const visualHeight = win?.visualViewport?.height;
if (typeof visualHeight === 'number' && visualHeight > 0) {
return visualHeight;
}
const innerHeight = win?.innerHeight;
if (typeof innerHeight === 'number' && innerHeight > 0) {
return innerHeight;
}
return 0;
}
function syncViewportHeight(win, doc) {
const height = Math.round(getVisibleViewportHeight(win));
if (!height || !doc?.documentElement?.style?.setProperty) {
return height;
}
doc.documentElement.style.setProperty('--app-viewport-height', `${height}px`);
return height;
}
function initViewportHeightSync(win, doc) {
if (!win || !doc) {
return function noop() {};
}
const sync = () => syncViewportHeight(win, doc);
const removeListeners = [];
const register = (target, eventName) => {
if (!target?.addEventListener) return;
target.addEventListener(eventName, sync, { passive: true });
removeListeners.push(() => target.removeEventListener(eventName, sync));
};
sync();
register(win, 'resize');
register(win, 'orientationchange');
register(win.visualViewport, 'resize');
register(win.visualViewport, 'scroll');
return function cleanup() {
removeListeners.forEach(removeListener => removeListener());
};
}
return {
getVisibleViewportHeight,
syncViewportHeight,
initViewportHeightSync
};
});