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

Add debounce to useResizeObserver hook #104

Closed
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
8 changes: 7 additions & 1 deletion src/hooks/useResizeObserver/useResizeObserver.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import { Meta } from '@storybook/blocks';
This hook allows you to add a ResizeObserver for an element and remove it when the component
unmounts.

By default the callback is debounced at 200ms to improve performance.

## Reference

```ts
function useResizeObserver(ref: RefObject<Element>, callback: ResizeObserverCallback): void;
function useResizeObserver(
ref: RefObject<Element>,
callback: ResizeObserverCallback,
debounceTime?: number | null,
): void;
```

## Usage
Expand Down
32 changes: 26 additions & 6 deletions src/hooks/useResizeObserver/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import { debounce } from 'lodash-es';
import { type RefObject, useEffect } from 'react';

const resizeObserverInstanceCallbacks = new Map<Element, ResizeObserverCallback>();
let resizeObserverInstance: ResizeObserver | null = null;

/**
* This hook allows you to add a ResizeObserver for an element and remove it
* when the component unmounts.
*
* @param ref - The ref to observe
* @param callback - The callback to fire when the element resizes
* @param debounceTime - The number in milliseconds the callback is debounced
*/
export function useResizeObserver(ref: RefObject<Element>, callback: ResizeObserverCallback): void {
export function useResizeObserver(
ref: RefObject<Element>,
callback: ResizeObserverCallback,
debounceTime?: number | null,
): void {
useEffect(() => {
const resizeObserverInstance = new ResizeObserver(callback);

if (ref.current === null) {
throw new Error('`ref.current` is undefined');
}

resizeObserverInstance.observe(ref.current);
if (!resizeObserverInstance) {
resizeObserverInstance = new ResizeObserver((entries, observer) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the previous approach is fine, so I'd like to keep the implementation as simple as possible. Yeah, the old implementation is creating multiple resize observers which may not be necessary, but from a performance point of view it's not likely that we're creating thousands of resize observers on a single page.

for (const entry of entries) {
resizeObserverInstanceCallbacks.get(entry.target)?.(entries, observer);
}
});
}

const element = ref.current;
const callbackFunction =
debounceTime === null ? callback : debounce(callback, debounceTime ?? 200);
resizeObserverInstanceCallbacks.set(element, callbackFunction);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will overwrite callbacks that are already added to an element, that could lead to bugs. If we want to keep this implementation we need to support multiple callbacks for a single element.

resizeObserverInstance.observe(element);

return () => {
resizeObserverInstance.disconnect();
resizeObserverInstance?.unobserve(element);
resizeObserverInstanceCallbacks.delete(element);
};
}, [ref, callback]);
}, [ref, callback, debounceTime]);
}