Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug Fix

- `Popover`: make sure that `ownerDocument` is always defined ([#42886](https://github.com/WordPress/gutenberg/pull/42886)).
- `ExternalLink`: Check if the link is an internal anchor link and prevent anchor links from being opened. ([#42259](https://github.com/WordPress/gutenberg/pull/42259)).
- `BorderControl`: Ensure box-sizing is reset for the control ([#42754](https://github.com/WordPress/gutenberg/pull/42754)).
- `InputControl`: Fix acceptance of falsy values in controlled updates ([#42484](https://github.com/WordPress/gutenberg/pull/42484/)).
Expand Down
21 changes: 11 additions & 10 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,26 @@ const Popover = (
: placement;

const ownerDocument = useMemo( () => {
let documentToReturn;

if ( anchorRef?.top ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised anchorRef is not even documented in the readme, when it's the prop that takes the highest priority. Looks like we have a lot of work to do (docs, stories, tests) as we work out the kinks in this component 💪

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! This component definitely needs a lot of work

return anchorRef?.top.ownerDocument;
documentToReturn = anchorRef?.top.ownerDocument;
} else if ( anchorRef?.startContainer ) {
return anchorRef.startContainer.ownerDocument;
documentToReturn = anchorRef.startContainer.ownerDocument;
} else if ( anchorRef?.current ) {
return anchorRef.current.ownerDocument;
documentToReturn = anchorRef.current.ownerDocument;
} else if ( anchorRef ) {
// This one should be deprecated.
return anchorRef.ownerDocument;
documentToReturn = anchorRef.ownerDocument;
} else if ( anchorRect && anchorRect?.ownerDocument ) {
return anchorRect.ownerDocument;
documentToReturn = anchorRect.ownerDocument;
} else if ( getAnchorRect ) {
return (
getAnchorRect( anchorRefFallback.current )?.ownerDocument ??
document
);
documentToReturn = getAnchorRect(
anchorRefFallback.current
)?.ownerDocument;
}

return document;
return documentToReturn ?? document;
}, [ anchorRef, anchorRect, getAnchorRect ] );

/**
Expand Down
24 changes: 23 additions & 1 deletion packages/components/src/popover/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* External dependencies
*/
import { act, render } from '@testing-library/react';
import { act, render, screen } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -44,4 +49,21 @@ describe( 'Popover', () => {

expect( result.container.querySelector( 'span' ) ).toMatchSnapshot();
} );

it( 'should render correctly when anchorRef is provided', () => {
const PopoverWithAnchor = ( args ) => {
const anchorRef = useRef( null );

return (
<div>
<p ref={ anchorRef }>Anchor</p>
<Popover { ...args } anchorRef={ anchorRef } />
</div>
);
};

render( <PopoverWithAnchor>Popover content</PopoverWithAnchor> );

expect( screen.getByText( 'Popover content' ) ).toBeInTheDocument();
} );
} );