-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add useEvent and revamped useResizeObserver to @wordpress/compose
#64943
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
Merged
DaniGuardiola
merged 32 commits into
trunk
from
feat/add-use-event-and-use-observe-element-size
Sep 9, 2024
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
95881cc
Simplify useResizeObserver
jsnajdr 1e305a9
Loop through all resize entries
jsnajdr 55172e3
Add `useEvent` util.
DaniGuardiola 0b91189
Add `useObserveElementSize` util.
DaniGuardiola c4ee26c
Simplify `useResizeObserver` by using `useEvent` and `useObserveEleme…
DaniGuardiola 575423c
Merge branch 'trunk' of https://github.com/WordPress/gutenberg into f…
DaniGuardiola 93a36e1
Switch to layout effect and accept refs too.
DaniGuardiola fc33849
Prevent initial re-render in ResizeElement.
DaniGuardiola b422ccc
Better error message.
DaniGuardiola cff6661
Improved example of useEvent.
DaniGuardiola 9531c10
Update packages/compose/src/hooks/use-event/index.ts
DaniGuardiola c58e10b
Sync docs.
DaniGuardiola 0da2846
Avoid redundant resize listener calls.
DaniGuardiola bc77100
Switch to structural check.
DaniGuardiola 638f551
Improve example.
DaniGuardiola 654bac5
Fix docs.
DaniGuardiola 00f53f7
Make `useObserveElementSize` generic.
DaniGuardiola b6dc4f4
New API that returns a ref.
DaniGuardiola 74c4a00
Make utility private for now.
DaniGuardiola 4fb82b9
Mark legacy `useResizeObserver` as such.
DaniGuardiola a381140
Rename `useObserveElementSize` to `useResizeObserver`.
DaniGuardiola 7b4ecce
Add return type.
DaniGuardiola f42603c
Add signature as overload.
DaniGuardiola 63deb16
Add support for legacy API.
DaniGuardiola 30a07f0
Move into subdirectory.
DaniGuardiola e57cc0e
Minor import fix.
DaniGuardiola 15eaebc
Fix docgen to support overloads (will pick up the first function sign…
DaniGuardiola 3387a0a
Replace legacy utility with the new one.
DaniGuardiola 8c2094f
Apply feedback.
DaniGuardiola 6f3abb7
Clean up and document.
DaniGuardiola 4aa01b3
Added changelog entries.
DaniGuardiola 55946ef
Merge branch 'trunk' of https://github.com/WordPress/gutenberg into f…
DaniGuardiola 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
Add
useObserveElementSize util.
- Loading branch information
commit 0b91189f8c7feac117b5249681168a796e79b73c
There are no files selected for viewing
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
packages/compose/src/hooks/use-observe-element-size/index.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useRef, useEffect } from '@wordpress/element'; | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useEvent from '../use-event'; | ||
|
|
||
| /** | ||
| * Tracks a given element's size and calls `onUpdate` for all of its discrete | ||
| * values using a `ResizeObserver`. The element can change dynamically and **it | ||
| * must not be stored in a ref**. Instead, it should be stored in a React | ||
| * state or equivalent. | ||
| * | ||
| * @param targetElement The target element to observe. It can be changed dynamically. | ||
| * @param onUpdate Callback that will be called when the element is resized. It is passed the list of [`ResizeObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry) objects passed to the `ResizeObserver.observe` callback internally, and the element being tracked at the time of this update. | ||
| * @param resizeObserverOptions Options to pass to `ResizeObserver.observe` when called internally. Updating this option will not cause the observer to be re-created, and it will only take effect if a new element is observed. | ||
| * | ||
| * @example | ||
| * | ||
| * ```tsx | ||
| * const [ targetElement, setTargetElement ] = useState< HTMLElement | null >(); | ||
| * useObserveElementSize( | ||
| * targetElement, | ||
| * ( resizeObserverEntries, element ) => { | ||
| * console.log( 'Resize observer entries:', resizeObserverEntries ); | ||
| * console.log( 'Element that was resized:', element ); | ||
| * }, | ||
| * { box: 'border-box' } | ||
| * ); | ||
| * <div ref={ setTargetElement } />; | ||
| * ``` | ||
| */ | ||
| export default function useObserveElementSize( | ||
| /** | ||
| * The target element to observe. It can be changed dynamically. | ||
| */ | ||
| targetElement: HTMLElement | undefined | null, | ||
| /** | ||
| * Callback that will be called when the element is resized. | ||
| */ | ||
| onUpdate: ( | ||
| /** | ||
| * The list of | ||
| * [`ResizeObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry) | ||
| * objects passed to the `ResizeObserver.observe` callback internally. | ||
| */ | ||
| resizeObserverEntries: ResizeObserverEntry[], | ||
| /** | ||
| * The element being tracked at the time of this update. | ||
| */ | ||
| element: HTMLElement | ||
| ) => void, | ||
| /** | ||
| * Options to pass to `ResizeObserver.observe` when called internally. | ||
| * | ||
| * Updating this option will not cause the observer to be re-created, and it | ||
| * will only take effect if a new element is observed. | ||
| */ | ||
| resizeObserverOptions?: ResizeObserverOptions | ||
| ) { | ||
| const onUpdateEvent = useEvent( onUpdate ); | ||
|
|
||
| const observedElementRef = useRef< HTMLElement | null >(); | ||
| const resizeObserverRef = useRef< ResizeObserver >(); | ||
|
|
||
| // Options are passed on `.observe` once and never updated, so we store them | ||
| // in an up-to-date ref to avoid unnecessary cycles of the effect due to | ||
| // unstable option objects such as inlined literals. | ||
| const resizeObserverOptionsRef = useRef( resizeObserverOptions ); | ||
| useEffect( () => { | ||
| resizeObserverOptionsRef.current = resizeObserverOptions; | ||
| }, [ resizeObserverOptions ] ); | ||
|
|
||
| // TODO: could/should this be a layout effect? | ||
| useEffect( () => { | ||
DaniGuardiola marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( targetElement === observedElementRef.current ) { | ||
| return; | ||
| } | ||
|
|
||
| observedElementRef.current = targetElement; | ||
|
|
||
| // Set up a ResizeObserver. | ||
| if ( ! resizeObserverRef.current ) { | ||
| resizeObserverRef.current = new ResizeObserver( ( entries ) => { | ||
| if ( observedElementRef.current ) { | ||
| onUpdateEvent( entries, observedElementRef.current ); | ||
| } | ||
| } ); | ||
| } | ||
| const { current: resizeObserver } = resizeObserverRef; | ||
|
|
||
| // Observe new element. | ||
| if ( targetElement ) { | ||
| resizeObserver.observe( | ||
| targetElement, | ||
| resizeObserverOptionsRef.current | ||
| ); | ||
| } | ||
|
|
||
| return () => { | ||
| // Unobserve previous element. | ||
| if ( observedElementRef.current ) { | ||
| resizeObserver.unobserve( observedElementRef.current ); | ||
| } | ||
| }; | ||
| }, [ onUpdateEvent, targetElement ] ); | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.