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 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
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,22 @@ 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");

waitForElementNotPresent(By.tagName("vaadin-dialog-overlay"));

clickTarget();
checkPopoverIsOpened();
}

private void clickTarget() {
clickElementWithJs("popover-target");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,7 @@ public void setTarget(Component target) {
targetDetachRegistration.remove();
}

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

this.target = target;

Expand All @@ -710,27 +707,31 @@ public void setTarget(Component target) {

// Target's JavaScript needs to be executed on each attach,
// because Flow creates a new client-side element
target.getUI().ifPresent(this::onTargetAttach);
if (target.getUI().isPresent()) {
onTargetAttach();
}
targetAttachRegistration = target
.addAttachListener(e -> onTargetAttach(e.getUI()));
.addAttachListener(e -> onTargetAttach());
targetDetachRegistration = target.addDetachListener(e -> {
if (autoAddedToTheUi) {
getElement().removeFromParent();
autoAddedToTheUi = false;
}
removeFromUiIfAutoAdded();
});
}

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

private void onTargetAttach() {
if (target != null) {
ui.beforeClientResponse(ui, context -> {
if (getElement().getNode().getParent() == null) {
// Remove the popover from its current state tree
getElement().removeFromTree(false);
ui.addToModalComponent(this);
autoAddedToTheUi = true;
}
});
if (getElement().getNode().getParent() == null) {
// Remove the popover from its current state tree
getElement().removeFromTree(false);
target.getElement().getParent().appendChild(getElement());
autoAddedToTheUi = true;
}
getElement().executeJs("this.target = $0", target.getElement());
}
}
Expand Down