Skip to content

fix: do not delay auto-adding Popover by beforeClientResponse #7563

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
merged 5 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<groupId>com.vaadin</groupId>
<artifactId>vaadin-dev-server</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-dialog-flow</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-flow-components-test-util</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vaadin.flow.component.popover.tests;

import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.popover.Popover;
Expand Down Expand Up @@ -54,7 +55,22 @@ public PopoverView() {
event -> popover.setCloseOnOutsideClick(false));
disableCloseOnOutsideClick.setId("disable-close-on-outside-click");

NativeButton openDialog = new NativeButton("Open dialog and set target",
event -> {
var dialog = new Dialog();
var close = new NativeButton("Close", e -> dialog.close());
close.setId("close-dialog");
dialog.add(close);
dialog.open();
popover.setTarget(target);
});
openDialog.setId("open-dialog");

NativeButton removePopover = new NativeButton("Remove popover",
event -> remove(popover));
removePopover.setId("remove-popover");

add(popover, clearTarget, detachTarget, attachTarget, disableCloseOnEsc,
disableCloseOnOutsideClick, target);
disableCloseOnOutsideClick, removePopover, openDialog, target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ public void disableCloseOnEsc_pressEsc_popoverDoesNotClose() {
Assert.assertTrue(popover.isOpen());
}

@Test
public void openDialogAndSetTarget_closeDialog_popoverOpens() {
// Clear the target and remove the popover first to ensure the popover
// is auto-added when the dialog opens
clickElementWithJs("clear-target");
clickElementWithJs("remove-popover");

clickElementWithJs("open-dialog");
clickElementWithJs("close-dialog");

clickTarget();
checkPopoverIsOpened();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to avoid adding an IT and use a unit test instead, but with no luck. I tried something like:

    @Test
    public void targetAddedToPage_modalElementOpenedAndClosed_popoverAutoAddedToPage() {
        Popover popover = new Popover();
        Div target = new Div();
        popover.setTarget(target);
        ui.add(target);

        Div modalElement = new Div();
        ui.setChildComponentModal(modalElement, true);
        fakeClientResponse();

        Assert.assertEquals(modalElement.getElement(),
                popover.getElement().getParent());

        ui.remove(modalElement);
        modalElement.getElement().removeAllChildren();
        modalElement.getElement().removeFromTree();
        // popover.getElement().removeFromTree();
        fakeClientResponse();

        Assert.assertEquals(ui.getElement(), popover.getElement().getParent());
    }

But the detach listener in the Popover class wasn't being called.


private void clickTarget() {
clickElementWithJs("popover-target");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class Popover extends Component implements HasAriaLabel, HasComponents,
*/
public Popover() {
getElement().getNode().addAttachListener(this::attachComponentRenderer);
getElement().getNode().addDetachListener(this::dettachListener);

// Workaround for: https://github.com/vaadin/flow/issues/3496
getElement().setProperty("opened", false);
Expand Down Expand Up @@ -696,10 +697,7 @@ public void setTarget(Component target) {
targetDetachRegistration.remove();
}

if (autoAddedToTheUi) {
getElement().removeFromParent();
autoAddedToTheUi = false;
}
removeFromUiIfAutoAdded();

this.target = target;

Expand All @@ -714,13 +712,17 @@ public void setTarget(Component target) {
targetAttachRegistration = target
.addAttachListener(e -> onTargetAttach(e.getUI()));
targetDetachRegistration = target.addDetachListener(e -> {
if (autoAddedToTheUi) {
getElement().removeFromParent();
autoAddedToTheUi = false;
}
removeFromUiIfAutoAdded();
});
}

private void removeFromUiIfAutoAdded() {
if (autoAddedToTheUi) {
autoAddedToTheUi = false;
getElement().removeFromParent();
}
}

private void onTargetAttach(UI ui) {
if (target != null) {
ui.beforeClientResponse(ui, context -> {
Expand Down Expand Up @@ -828,6 +830,14 @@ private void attachComponentRenderer() {
appId);
}

private void dettachListener() {
if (autoAddedToTheUi && target != null && target.isAttached()
&& target.getUI().isPresent()) {
removeFromUiIfAutoAdded();
onTargetAttach(target.getUI().get());
}
}

private Map<Element, Registration> childDetachListenerMap = new HashMap<>();

// Must not use lambda here as that would break serialization. See
Expand Down