diff --git a/src/hooks/useResizeObserver/useResizeObserver.stories.tsx b/src/hooks/useResizeObserver/useResizeObserver.stories.tsx index ab258cc..56cd839 100644 --- a/src/hooks/useResizeObserver/useResizeObserver.stories.tsx +++ b/src/hooks/useResizeObserver/useResizeObserver.stories.tsx @@ -1,5 +1,7 @@ +/* eslint-disable react/jsx-no-literals, react-hooks/rules-of-hooks */ import type { Meta, StoryObj } from '@storybook/react'; -import { type ReactElement, useRef, useState } from 'react'; +import { useCallback, useRef, useState } from 'react'; +import { useToggle } from '../useToggle/useToggle.js'; import { useResizeObserver } from './useResizeObserver.js'; const meta = { @@ -10,30 +12,48 @@ export default meta; type Story = StoryObj; -function DemoComponent(): ReactElement { - const elementRef = useRef(null); - const [element, setElement] = useState(null); - - useResizeObserver(elementRef, () => { - // eslint-disable-next-line no-console - console.log('Element from RefObject resized'); - }); - - useResizeObserver(element, () => { - // eslint-disable-next-line no-console - console.log('Element from state resized'); - }); - - return ( - <> -
-
- - ); -} - export const Demo: Story = { render() { - return ; + const [enabled, onClick] = useToggle(true); + + const elementRef = useRef(null); + const [elementRefWidth, setElementRefWidth] = useState(Number.NaN); + + const [element, setElement] = useState(null); + const [elementWidth, setElementWidth] = useState(Number.NaN); + + const onResizeElementRef = useCallback(() => { + setElementRefWidth(elementRef.current?.clientWidth ?? Number.NaN); + }, []); + + const onResizeElement = useCallback(() => { + setElementWidth(element?.clientWidth ?? Number.NaN); + }, [element?.clientWidth]); + + useResizeObserver(elementRef, enabled ? onResizeElementRef : undefined); + useResizeObserver(element, enabled ? onResizeElement : undefined); + + return ( + <> +

Resize the window to update the width of the elements

+
+ Enabled: {enabled.toString()};
+ Width: {elementRefWidth}; +
+
+ Enabled: {enabled.toString()};
+ Width: {elementWidth}; +
+ + + ); }, }; diff --git a/src/hooks/useResizeObserver/useResizeObserver.ts b/src/hooks/useResizeObserver/useResizeObserver.ts index e1278de..f20b672 100644 --- a/src/hooks/useResizeObserver/useResizeObserver.ts +++ b/src/hooks/useResizeObserver/useResizeObserver.ts @@ -10,20 +10,20 @@ import { unref, type Unreffable } from '../../utils/unref/unref.js'; */ export function useResizeObserver( target: Unreffable, - callback: ResizeObserverCallback, + callback?: ResizeObserverCallback | undefined, ): void { useEffect(() => { const element = unref(target); - if (element === null) { + if (element === null || callback === undefined) { return; } - const resizeObserverInstance = new ResizeObserver(callback); - resizeObserverInstance.observe(element); + const resizeObserver = new ResizeObserver(callback); + resizeObserver.observe(element); return () => { - resizeObserverInstance.disconnect(); + resizeObserver.disconnect(); }; }, [target, callback]); }