Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set options to observed element in useResizeObserver #162 #283

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ?? {})]);
}
Loading