Skip to content

Commit

Permalink
Set options to observed element in useResizeObserver #162
Browse files Browse the repository at this point in the history
  • Loading branch information
leroykorterink committed Dec 4, 2023
1 parent c705fdf commit dc3de5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
44 changes: 43 additions & 1 deletion src/hooks/useResizeObserver/useResizeObserver.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const Demo: Story = {

return (
<>
<h4>Resize the window to update the width of the elements</h4>
<h4>Resize the window to update</h4>
<div ref={elementRef} style={{ outline: '1px solid red', marginBlock: 20 }}>
Enabled: {enabled.toString()};<br />
Width: {elementRefWidth};
Expand All @@ -57,3 +57,45 @@ export const Demo: Story = {
);
},
};

export const Options: Story = {
render() {
// Element 1
const elementRef1 = useRef<HTMLDivElement>(null);
const [elementWidth1, setElementWidth1] = useState(Number.NaN);

const onResizeElement1 = useCallback((entries: Array<ResizeObserverEntry>) => {
setElementWidth1(entries.at(0)?.borderBoxSize.at(0)?.inlineSize ?? Number.NaN);
}, []);

useResizeObserver(elementRef1, onResizeElement1, {
box: 'border-box',
});

// Element 2
const elementRef2 = useRef<HTMLDivElement>(null);
const [elementWidth2, setElementWidth2] = useState<number>(Number.NaN);

const onResizeElement2 = useCallback((entries: Array<ResizeObserverEntry>) => {
setElementWidth2(entries.at(0)?.contentBoxSize.at(0)?.inlineSize ?? Number.NaN);
}, []);

useResizeObserver(elementRef2, onResizeElement2, {
box: 'content-box',
});

return (
<>
<h4>Resize the window to update</h4>
<div ref={elementRef1} style={{ outline: '1px solid red', marginBlock: 20, padding: 20 }}>
<pre>border-box</pre>
Width: {elementWidth1};
</div>
<div ref={elementRef2} style={{ outline: '1px solid red', marginBlock: 20, padding: 20 }}>
<pre>content-box</pre>
Width: {elementWidth2};
</div>
</>
);
},
};
7 changes: 5 additions & 2 deletions src/hooks/useResizeObserver/useResizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { unref, type Unreffable } from '../../utils/unref/unref.js';
*
* @param target - The target to observe
* @param callback - The callback to fire when the element resizes
* @param options - The ResizeObserverOptions for the observed element
*/
export function useResizeObserver(
target: Unreffable<Element | null>,
callback?: ResizeObserverCallback | undefined,
options?: ResizeObserverOptions,
): void {
useEffect(() => {
const element = unref(target);
Expand All @@ -20,10 +22,11 @@ export function useResizeObserver(
}

const resizeObserver = new ResizeObserver(callback);
resizeObserver.observe(element);
resizeObserver.observe(element, options);

return () => {
resizeObserver.disconnect();
};
}, [target, callback]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target, callback, ...Object.values(options ?? {})]);
}

0 comments on commit dc3de5d

Please sign in to comment.