-
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
Don't recreate ResizeObserver on callback reference change #158
Closed
leroykorterink
wants to merge
1
commit into
main
from
feature/dont-recreate-resize-observer-on-callback-reference-change
Closed
Changes from all commits
Commits
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
32 changes: 21 additions & 11 deletions
32
src/hooks/useResizeObserver/useResizeObserver.stories.tsx
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,34 @@ | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
import type { StoryObj } from '@storybook/react'; | ||
import { type ReactElement, useRef } from 'react'; | ||
import { useRef, useState } from 'react'; | ||
import { useResizeObserver } from './useResizeObserver.js'; | ||
|
||
export default { | ||
title: 'hooks/useResizeObserver', | ||
}; | ||
|
||
function DemoComponent(): ReactElement { | ||
const ref = useRef<HTMLDivElement>(null); | ||
export const UsingRefObject: StoryObj = { | ||
render() { | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
useResizeObserver(ref, () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Element resized'); | ||
}); | ||
useResizeObserver(ref, () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Element resized'); | ||
}); | ||
|
||
return <div ref={ref}></div>; | ||
} | ||
return <div ref={ref}></div>; | ||
}, | ||
}; | ||
|
||
export const Demo: StoryObj = { | ||
export const UsingState: StoryObj = { | ||
render() { | ||
return <DemoComponent />; | ||
const [element, setElement] = useState<HTMLDivElement | null>(null); | ||
|
||
useResizeObserver(element, () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Element resized'); | ||
}); | ||
|
||
return <div ref={setElement}></div>; | ||
}, | ||
}; |
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,37 @@ | ||
import { type RefObject, useEffect } from 'react'; | ||
import { useEffect } from 'react'; | ||
import { unref, type Unreffable } from '../../index.js'; | ||
import { useRefValue } from '../useRefValue/useRefValue.js'; | ||
|
||
/** | ||
* This hook allows you to add a ResizeObserver for an element and remove it | ||
* This hook allows you to add a ResizeObserver to 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 | ||
*/ | ||
export function useResizeObserver(ref: RefObject<Element>, callback: ResizeObserverCallback): void { | ||
export function useResizeObserver( | ||
target: Unreffable<Element | null>, | ||
callback: ResizeObserverCallback, | ||
): void { | ||
const callbackRef = useRefValue(callback); | ||
|
||
useEffect(() => { | ||
const resizeObserverInstance = new ResizeObserver(callback); | ||
const element = unref(target); | ||
|
||
if (ref.current === null) { | ||
throw new Error('`ref.current` is undefined'); | ||
if (element === null) { | ||
return; | ||
} | ||
|
||
resizeObserverInstance.observe(ref.current); | ||
const resizeObserverInstance = new ResizeObserver( | ||
(entries: Array<ResizeObserverEntry>, observer: ResizeObserver) => { | ||
callbackRef.current?.(entries, observer); | ||
}, | ||
); | ||
|
||
resizeObserverInstance.observe(element); | ||
leroykorterink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return () => { | ||
resizeObserverInstance.disconnect(); | ||
}; | ||
}, [ref, callback]); | ||
}, [callbackRef, target]); | ||
} |
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.
Do we want a separate ticket to support
unobserve
, e.g. having a boolean arg that defaults totrue
, but can be set tofalse
to unobserve ?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.
Implementation will be similar to this: