Skip to content
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

Fix issue with popovers not working in embedded app modals when put inside of a details component #12534

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .changeset/funny-feet-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Fix issue with popovers not working in embedded app modals when put inside of a details component
10 changes: 10 additions & 0 deletions polaris-react/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ const PopoverComponent = forwardRef<PopoverPublicAPI, PopoverProps>(
const observer = new ResizeObserver(setDisplayState);
observer.observe(activatorContainer.current);

/**
* We want to observe the parent element when possible because it is the one controlling the display state of the popover.
* The parent element could be collapsed initially and then opened later on like in the case
* of using a `<details>` component.
*/

observer.observe(
activatorContainer.current.parentElement || activatorContainer.current,

Choose a reason for hiding this comment

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

I think a more robust approach would be to recursively get the parent element until you find one where offsetParent !== null:

let elementToObserve = activatorContainer.current;

while (elementToObserve.offsetParent !== null) {
  elementToObserve = elementToObserve.parentElement;
}

);

setDisplayState();

return () => {
Expand Down
6 changes: 4 additions & 2 deletions polaris-react/src/components/Popover/tests/Popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ describe('<Popover />', () => {
expect(popover).not.toContainReactComponent(PositionedOverlay);
});

it('observes the resize event for the activator wrapper', () => {
it("observes the resize event for the activator wrapper's parent element", () => {
const observe = jest.fn();

// eslint-disable-next-line jest/prefer-spy-on
Expand All @@ -558,7 +558,9 @@ describe('<Popover />', () => {
/>,
);

expect(observe).toHaveBeenCalledWith(popover.find('span')?.domNode);
expect(observe).toHaveBeenCalledWith(
popover.find('span')?.domNode?.parentElement,
);
});

it('disconnects the resize observer when component unmounts', () => {
Expand Down
Loading