-
Notifications
You must be signed in to change notification settings - Fork 2
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
firstchair
wants to merge
6
commits into
mediamonks:main
from
firstchair:feature/use-resize-observer-debounced
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e0113a
Add default debounce to useResizeObserver hook
firstchair 7cc7467
Fix typo
firstchair 68d9ad1
Add parameter optional to story
firstchair d28c670
Update hook to use one ResizeObserver instead of multiple
firstchair e5893ed
Cleanup
firstchair 6e17407
Remove unnecessary condition
firstchair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.