-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename "_legacy" dir to "legacy" * Fix circular dependency issue * Fix bug that caused elements to not be unobserved. * Add changelog * Remove changelog entry Co-authored-by: DaniGuardiola <[email protected]> Co-authored-by: jsnajdr <[email protected]> Co-authored-by: tyxla <[email protected]>
- Loading branch information
1 parent
fe25bf1
commit a90e35b
Showing
5 changed files
with
47 additions
and
43 deletions.
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
File renamed without changes.
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
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
packages/compose/src/hooks/use-resize-observer/use-resize-observer.ts
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useEvent from '../use-event'; | ||
|
||
// This is the current implementation of `useResizeObserver`. | ||
// | ||
// The legacy implementation is still supported for backwards compatibility. | ||
// This is achieved by overloading the exported function with both signatures, | ||
// and detecting which API is being used at runtime. | ||
export function useResizeObserver< T extends HTMLElement >( | ||
callback: ResizeObserverCallback, | ||
resizeObserverOptions: ResizeObserverOptions = {} | ||
): ( element?: T | null ) => void { | ||
const callbackEvent = useEvent( callback ); | ||
|
||
const observedElementRef = useRef< T | null >(); | ||
const resizeObserverRef = useRef< ResizeObserver >(); | ||
return useEvent( ( element?: T | null ) => { | ||
if ( element === observedElementRef.current ) { | ||
return; | ||
} | ||
|
||
// Set up `ResizeObserver`. | ||
resizeObserverRef.current ??= new ResizeObserver( callbackEvent ); | ||
const { current: resizeObserver } = resizeObserverRef; | ||
|
||
// Unobserve previous element. | ||
if ( observedElementRef.current ) { | ||
resizeObserver.unobserve( observedElementRef.current ); | ||
} | ||
|
||
// Observe new element. | ||
observedElementRef.current = element; | ||
if ( element ) { | ||
resizeObserver.observe( element, resizeObserverOptions ); | ||
} | ||
} ); | ||
} |