nexa-safe-area 0.7.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/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/useSafeArea.d.ts +14 -0
- package/dist/useSafeArea.js +61 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useSafeArea } from './useSafeArea.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface SafeAreaInsets {
|
|
2
|
+
top: number;
|
|
3
|
+
right: number;
|
|
4
|
+
bottom: number;
|
|
5
|
+
left: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function useSafeArea(): {
|
|
8
|
+
insets: import("nexa-reactivity").Signal<SafeAreaInsets>;
|
|
9
|
+
top: import("nexa-reactivity").Signal<number>;
|
|
10
|
+
right: import("nexa-reactivity").Signal<number>;
|
|
11
|
+
bottom: import("nexa-reactivity").Signal<number>;
|
|
12
|
+
left: import("nexa-reactivity").Signal<number>;
|
|
13
|
+
destroy(): void;
|
|
14
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { signal, computed } from 'nexa-reactivity';
|
|
2
|
+
// Parses 'env(safe-area-inset-top, 20px)' to 20
|
|
3
|
+
function parseEnvVal(val) {
|
|
4
|
+
const match = val.match(/(\d+)px/);
|
|
5
|
+
if (match)
|
|
6
|
+
return parseInt(match[1], 10);
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
export function useSafeArea() {
|
|
10
|
+
const insets = signal({ top: 0, right: 0, bottom: 0, left: 0 });
|
|
11
|
+
const update = () => {
|
|
12
|
+
if (typeof window === 'undefined' || typeof getComputedStyle === 'undefined')
|
|
13
|
+
return;
|
|
14
|
+
const root = document.documentElement;
|
|
15
|
+
const style = getComputedStyle(root);
|
|
16
|
+
const top = style.getPropertyValue('--sat') || '0px';
|
|
17
|
+
const right = style.getPropertyValue('--sar') || '0px';
|
|
18
|
+
const bottom = style.getPropertyValue('--sab') || '0px';
|
|
19
|
+
const left = style.getPropertyValue('--sal') || '0px';
|
|
20
|
+
insets.value = {
|
|
21
|
+
top: parseEnvVal(top),
|
|
22
|
+
right: parseEnvVal(right),
|
|
23
|
+
bottom: parseEnvVal(bottom),
|
|
24
|
+
left: parseEnvVal(left)
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
// We need to inject these CSS variables into the document for JS to read them reliably
|
|
28
|
+
if (typeof document !== 'undefined') {
|
|
29
|
+
const styleId = 'nexa-safe-area-vars';
|
|
30
|
+
if (!document.getElementById(styleId)) {
|
|
31
|
+
const styleEl = document.createElement('style');
|
|
32
|
+
styleEl.id = styleId;
|
|
33
|
+
styleEl.innerHTML = `
|
|
34
|
+
:root {
|
|
35
|
+
--sat: env(safe-area-inset-top, 0px);
|
|
36
|
+
--sar: env(safe-area-inset-right, 0px);
|
|
37
|
+
--sab: env(safe-area-inset-bottom, 0px);
|
|
38
|
+
--sal: env(safe-area-inset-left, 0px);
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
document.head.appendChild(styleEl);
|
|
42
|
+
}
|
|
43
|
+
update();
|
|
44
|
+
// Update on resize or orientation change since insets might change
|
|
45
|
+
window.addEventListener('resize', update);
|
|
46
|
+
window.addEventListener('orientationchange', update);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
insets,
|
|
50
|
+
top: computed(() => insets.value.top),
|
|
51
|
+
right: computed(() => insets.value.right),
|
|
52
|
+
bottom: computed(() => insets.value.bottom),
|
|
53
|
+
left: computed(() => insets.value.left),
|
|
54
|
+
destroy() {
|
|
55
|
+
if (typeof window !== 'undefined') {
|
|
56
|
+
window.removeEventListener('resize', update);
|
|
57
|
+
window.removeEventListener('orientationchange', update);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nexa-safe-area",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"nexa-reactivity": "0.7.0"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
}
|
|
28
|
+
}
|