From 746a9b6a01e0d0c406874d8c6afded48c6c9766c Mon Sep 17 00:00:00 2001 From: Nelson Liu Date: Mon, 9 Aug 2021 14:28:01 +0800 Subject: [PATCH] feat: add useResizeObserver --- docs/demo/hook.md | 3 + examples/basic.tsx | 5 +- examples/debug.tsx | 4 +- examples/hook.tsx | 53 ++++++++++++ src/OnResizeHandler.d.ts | 3 + src/{index.tsx => ResizeObserver.tsx} | 21 ++--- src/Size.d.ts | 6 ++ src/index.ts | 7 ++ src/useResizeObserver.tsx | 111 ++++++++++++++++++++++++++ 9 files changed, 193 insertions(+), 20 deletions(-) create mode 100644 docs/demo/hook.md create mode 100644 examples/hook.tsx create mode 100644 src/OnResizeHandler.d.ts rename src/{index.tsx => ResizeObserver.tsx} (91%) create mode 100644 src/Size.d.ts create mode 100644 src/index.ts create mode 100644 src/useResizeObserver.tsx diff --git a/docs/demo/hook.md b/docs/demo/hook.md new file mode 100644 index 0000000..67bf6ad --- /dev/null +++ b/docs/demo/hook.md @@ -0,0 +1,3 @@ +## hook + + diff --git a/examples/basic.tsx b/examples/basic.tsx index b5f3aad..2ac4e9c 100644 --- a/examples/basic.tsx +++ b/examples/basic.tsx @@ -1,6 +1,7 @@ import '../assets/index.less'; import React from 'react'; -import ResizeObserver, { ResizeObserverProps } from '../src'; +import type { ResizeObserverProps } from '../src/ResizeObserver'; +import ResizeObserver from '../src/ResizeObserver'; export default function App() { const [times, setTimes] = React.useState(0); @@ -17,7 +18,7 @@ export default function App() { offsetHeight, offsetWidth, }) => { - setTimes(prevTimes => prevTimes + 1); + setTimes((prevTimes) => prevTimes + 1); console.log( 'Resize:', '\n', diff --git a/examples/debug.tsx b/examples/debug.tsx index 20c3e1f..11a8463 100644 --- a/examples/debug.tsx +++ b/examples/debug.tsx @@ -1,6 +1,6 @@ import '../assets/index.less'; import React from 'react'; -import ResizeObserver from '../src'; +import ResizeObserver from '../src/ResizeObserver'; export default function App() { const [times, setTimes] = React.useState(0); @@ -9,7 +9,7 @@ export default function App() { const [disabled, setDisabled] = React.useState(false); const onResize = (size: { width: number; height: number }) => { - setTimes(prevTimes => prevTimes + 1); + setTimes((prevTimes) => prevTimes + 1); setWidth(size.width); setHeight(size.height); }; diff --git a/examples/hook.tsx b/examples/hook.tsx new file mode 100644 index 0000000..d7eb3fd --- /dev/null +++ b/examples/hook.tsx @@ -0,0 +1,53 @@ +import '../assets/index.less'; +import React from 'react'; +import type { OnResizeHandler } from '../src/OnResizeHandler'; +import { useResizeObserver } from '../src/useResizeObserver'; + +export default function App() { + const [times, setTimes] = React.useState(0); + const [disabled, setDisabled] = React.useState(false); + const textareaRef = React.useRef(null); + + const onResize: OnResizeHandler = ({ width, height, offsetWidth, offsetHeight }) => { + setTimes((prevTimes) => prevTimes + 1); + console.log( + 'Resize:', + '\n', + 'BoundingBox', + width, + height, + '\n', + 'Offset', + offsetWidth, + offsetHeight, + ); + }; + + useResizeObserver({ ref: textareaRef, onResize, disabled }); + + React.useEffect(() => { + console.log('Ref:', textareaRef.current); + }, []); + + return ( + +
+
+ + {' >>> '} + Resize times: {times} +
+