From 5ab0ea1b592b059c71b688b6795a92bd8745c2d2 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Tue, 25 Jun 2024 10:45:52 +0800 Subject: [PATCH] Remove usehooks dep In the end, only used one hook out of so many hooks --- package-lock.json | 14 -------------- package.json | 1 - src/components/menu2.jsx | 2 +- src/utils/useWindowSize.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 src/utils/useWindowSize.js diff --git a/package-lock.json b/package-lock.json index 376051c0..37c34121 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,6 @@ "@iconify-icons/mingcute": "~1.2.9", "@justinribeiro/lite-youtube": "~1.5.0", "@szhsin/react-menu": "~4.1.0", - "@uidotdev/usehooks": "~2.4.1", "compare-versions": "~6.1.0", "dayjs": "~1.11.11", "dayjs-twitter": "~0.5.0", @@ -3886,19 +3885,6 @@ "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", "dev": true }, - "node_modules/@uidotdev/usehooks": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/@uidotdev/usehooks/-/usehooks-2.4.1.tgz", - "integrity": "sha512-1I+RwWyS+kdv3Mv0Vmc+p0dPYH0DTRAo04HLyXReYBL9AeseDWUJyi4THuksBJcu9F0Pih69Ak150VDnqbVnXg==", - "license": "MIT", - "engines": { - "node": ">=16" - }, - "peerDependencies": { - "react": ">=18.0.0", - "react-dom": ">=18.0.0" - } - }, "node_modules/@vue/compiler-core": { "version": "3.2.45", "resolved": "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.2.45.tgz", diff --git a/package.json b/package.json index 21feda6f..3c87980e 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "@iconify-icons/mingcute": "~1.2.9", "@justinribeiro/lite-youtube": "~1.5.0", "@szhsin/react-menu": "~4.1.0", - "@uidotdev/usehooks": "~2.4.1", "compare-versions": "~6.1.0", "dayjs": "~1.11.11", "dayjs-twitter": "~0.5.0", diff --git a/src/components/menu2.jsx b/src/components/menu2.jsx index 6ca68320..3d83a72d 100644 --- a/src/components/menu2.jsx +++ b/src/components/menu2.jsx @@ -1,8 +1,8 @@ import { Menu } from '@szhsin/react-menu'; -import { useWindowSize } from '@uidotdev/usehooks'; import { useRef } from 'preact/hooks'; import safeBoundingBoxPadding from '../utils/safe-bounding-box-padding'; +import useWindowSize from '../utils/useWindowSize'; // It's like Menu but with sensible defaults, bug fixes and improvements. function Menu2(props) { diff --git a/src/utils/useWindowSize.js b/src/utils/useWindowSize.js new file mode 100644 index 00000000..0907cee7 --- /dev/null +++ b/src/utils/useWindowSize.js @@ -0,0 +1,28 @@ +import { useLayoutEffect, useState } from 'preact/hooks'; + +export default function useWindowSize() { + const [size, setSize] = useState({ + width: null, + height: null, + }); + + useLayoutEffect(() => { + const handleResize = () => { + setSize({ + width: window.innerWidth, + height: window.innerHeight, + }); + }; + + handleResize(); + window.addEventListener('resize', handleResize, { + passive: true, + }); + + return () => { + window.removeEventListener('resize', handleResize); + }; + }, []); + + return size; +}