@os-design/use-size 1.0.14 → 1.0.16
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 +11 -4
- package/src/index.ts +42 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/use-size",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -14,7 +14,14 @@
|
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"!**/*.test.ts",
|
|
20
|
+
"!**/*.test.tsx",
|
|
21
|
+
"!**/__tests__",
|
|
22
|
+
"!**/*.stories.tsx",
|
|
23
|
+
"!**/*.stories.mdx",
|
|
24
|
+
"!**/*.example.tsx"
|
|
18
25
|
],
|
|
19
26
|
"sideEffects": false,
|
|
20
27
|
"scripts": {
|
|
@@ -29,10 +36,10 @@
|
|
|
29
36
|
"access": "public"
|
|
30
37
|
},
|
|
31
38
|
"dependencies": {
|
|
32
|
-
"@os-design/use-event": "^1.0.
|
|
39
|
+
"@os-design/use-event": "^1.0.15"
|
|
33
40
|
},
|
|
34
41
|
"peerDependencies": {
|
|
35
42
|
"react": ">=18"
|
|
36
43
|
},
|
|
37
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "e5d8409760608145d2c738aa5789d0465ae5416f"
|
|
38
45
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import useEvent from '@os-design/use-event';
|
|
2
|
+
import { RefObject, useCallback, useEffect, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
interface Size {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const useSize = (container?: Element | RefObject<Element>): Size => {
|
|
10
|
+
const [size, setSize] = useState<Size>({ width: 0, height: 0 });
|
|
11
|
+
|
|
12
|
+
const updateSize = useCallback(() => {
|
|
13
|
+
if (!container) {
|
|
14
|
+
setSize({ width: window.innerWidth, height: window.innerHeight });
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const containerElement =
|
|
19
|
+
container instanceof Element ? container : container.current;
|
|
20
|
+
if (!containerElement) return;
|
|
21
|
+
|
|
22
|
+
setSize({
|
|
23
|
+
width: containerElement.clientWidth,
|
|
24
|
+
height: containerElement.clientHeight,
|
|
25
|
+
});
|
|
26
|
+
}, [container]);
|
|
27
|
+
|
|
28
|
+
// Init the size of the container
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
updateSize();
|
|
31
|
+
}, [updateSize]);
|
|
32
|
+
|
|
33
|
+
useEvent(
|
|
34
|
+
(typeof window !== 'undefined' ? window : undefined) as never,
|
|
35
|
+
'resize',
|
|
36
|
+
updateSize
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
return size;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default useSize;
|