2023-06-13 12:46:37 +03:00
|
|
|
const root = document.documentElement;
|
2023-06-14 06:15:40 +03:00
|
|
|
const style = getComputedStyle(root);
|
2023-06-13 12:46:37 +03:00
|
|
|
const defaultBoundingBoxPadding = 8;
|
2023-09-09 09:10:52 +03:00
|
|
|
|
|
|
|
let safeAreaInsets = [0, 0, 0, 0];
|
|
|
|
function getSafeAreaInsets() {
|
2023-06-13 12:46:37 +03:00
|
|
|
// Get safe area inset variables from root
|
|
|
|
const safeAreaInsetTop = style.getPropertyValue('--sai-top');
|
|
|
|
const safeAreaInsetRight = style.getPropertyValue('--sai-right');
|
|
|
|
const safeAreaInsetBottom = style.getPropertyValue('--sai-bottom');
|
|
|
|
const safeAreaInsetLeft = style.getPropertyValue('--sai-left');
|
2023-09-09 09:10:52 +03:00
|
|
|
safeAreaInsets = [
|
|
|
|
// top, right, bottom, left (clockwise)
|
|
|
|
Math.max(0, parseInt(safeAreaInsetTop, 10)),
|
|
|
|
Math.max(0, parseInt(safeAreaInsetRight, 10)),
|
|
|
|
Math.max(0, parseInt(safeAreaInsetBottom, 10)),
|
|
|
|
Math.max(0, parseInt(safeAreaInsetLeft, 10)),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
requestAnimationFrame(getSafeAreaInsets);
|
|
|
|
|
|
|
|
function safeBoundingBoxPadding(paddings = []) {
|
|
|
|
const str = safeAreaInsets
|
|
|
|
.map((v, i) => (v || defaultBoundingBoxPadding) + (paddings[i] || 0))
|
2023-06-13 12:46:37 +03:00
|
|
|
.join(' ');
|
|
|
|
// console.log(str);
|
|
|
|
return str;
|
|
|
|
}
|
2023-09-09 09:10:52 +03:00
|
|
|
|
|
|
|
// Update safe area insets when orientation or resize
|
|
|
|
if (CSS.supports('top: env(safe-area-inset-top)')) {
|
|
|
|
window.addEventListener('resize', getSafeAreaInsets, { passive: true });
|
|
|
|
}
|
2023-06-13 12:46:37 +03:00
|
|
|
|
|
|
|
export default safeBoundingBoxPadding;
|