Skip to content

Commit

Permalink
Popover: fix exhaustive-deps warning (#45656)
Browse files Browse the repository at this point in the history
* Extract hook dependencies to separate variables

* Add refs.flaoting to deps list

* CHANGELOG
  • Loading branch information
ciampo authored Nov 10, 2022
1 parent e112e26 commit 6ab84d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Internal

- `PaletteEditListView`: Update to ignore `exhaustive-deps` eslint rule ([#45467](https://github.com/WordPress/gutenberg/pull/45467)).
- `Popover`: Update to pass `exhaustive-deps` eslint rule ([#45656](https://github.com/WordPress/gutenberg/pull/45656)).
- `Flex`: Update to pass `exhaustive-deps` eslint rule ([#45528](https://github.com/WordPress/gutenberg/pull/45528)).
- `withNotices`: Update to pass `exhaustive-deps` eslint rule ([#45530](https://github.com/WordPress/gutenberg/pull/45530)).
- `ItemGroup`: Update to pass `exhaustive-deps` eslint rule ([#45531](https://github.com/WordPress/gutenberg/pull/45531)).
Expand Down
25 changes: 18 additions & 7 deletions packages/components/src/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ const UnforwardedPopover = (

// When any of the possible anchor "sources" change,
// recompute the reference element (real or virtual) and its owner document.

const anchorRefTop = ( anchorRef as PopoverAnchorRefTopBottom | undefined )
?.top;
const anchorRefBottom = (
anchorRef as PopoverAnchorRefTopBottom | undefined
)?.bottom;
const anchorRefStartContainer = ( anchorRef as Range | undefined )
?.startContainer;
const anchorRefCurrent = ( anchorRef as PopoverAnchorRefReference )
?.current;

useLayoutEffect( () => {
const resultingReferenceOwnerDoc = getReferenceOwnerDocument( {
anchor,
Expand All @@ -401,11 +412,11 @@ const UnforwardedPopover = (
setReferenceOwnerDocument( resultingReferenceOwnerDoc );
}, [
anchor,
anchorRef as Element | undefined,
( anchorRef as PopoverAnchorRefTopBottom | undefined )?.top,
( anchorRef as PopoverAnchorRefTopBottom | undefined )?.bottom,
( anchorRef as Range | undefined )?.startContainer,
( anchorRef as PopoverAnchorRefReference )?.current,
anchorRef,
anchorRefTop,
anchorRefBottom,
anchorRefStartContainer,
anchorRefCurrent,
anchorRect,
getAnchorRect,
fallbackReferenceElement,
Expand All @@ -420,7 +431,7 @@ const UnforwardedPopover = (
// Reference and root documents are the same.
referenceOwnerDocument === document ||
// Reference and floating are in the same document.
referenceOwnerDocument === refs?.floating?.current?.ownerDocument ||
referenceOwnerDocument === refs.floating.current?.ownerDocument ||
// The reference's document has no view (i.e. window)
// or frame element (ie. it's not an iframe).
! referenceOwnerDocument?.defaultView?.frameElement
Expand All @@ -442,7 +453,7 @@ const UnforwardedPopover = (
return () => {
defaultView.removeEventListener( 'resize', updateFrameOffset );
};
}, [ referenceOwnerDocument, update ] );
}, [ referenceOwnerDocument, update, refs.floating ] );

const mergedFloatingRef = useMergeRefs( [
floating,
Expand Down

0 comments on commit 6ab84d5

Please sign in to comment.