-
Notifications
You must be signed in to change notification settings - Fork 67
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
refactor: extract overlay auto add controller #7028
Draft
sissbruecker
wants to merge
4
commits into
main
Choose a base branch
from
refactor/extract-overlay-auto-add-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffaec87
refactor: extract overlay auto add controller
sissbruecker b52845f
use overlay auto add controller in Dialog
sissbruecker 7d74deb
use overlay auto add controller in LoginOverlay
sissbruecker d832564
use overlay auto add controller in Notification
sissbruecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ase/src/main/java/com/vaadin/flow/component/shared/internal/OverlayAutoAddController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2000-2025 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.shared.internal; | ||
|
||
import java.io.Serializable; | ||
|
||
import com.vaadin.flow.component.Component; | ||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.function.SerializableSupplier; | ||
import com.vaadin.flow.internal.StateTree; | ||
import com.vaadin.flow.router.NavigationTrigger; | ||
import com.vaadin.flow.shared.Registration; | ||
|
||
/** | ||
* An internal controller for automatically adding a component to the UI when | ||
* it's opened. Not intended to be used publicly. | ||
* | ||
* @param <C> | ||
* Type of the component that uses this controller. | ||
*/ | ||
public class OverlayAutoAddController<C extends Component> | ||
implements Serializable { | ||
private final C component; | ||
private final SerializableSupplier<Boolean> isModalSupplier; | ||
|
||
private boolean autoAdded; | ||
private Registration afterProgrammaticNavigationListenerRegistration; | ||
|
||
public OverlayAutoAddController(C component) { | ||
this(component, () -> false); | ||
} | ||
|
||
public OverlayAutoAddController(C component, | ||
SerializableSupplier<Boolean> isModalSupplier) { | ||
this.component = component; | ||
this.isModalSupplier = isModalSupplier; | ||
|
||
component.getElement().addPropertyChangeListener("opened", event -> { | ||
if (isOpened()) { | ||
handleOpen(); | ||
} else { | ||
handleClose(); | ||
} | ||
}); | ||
} | ||
|
||
private void handleOpen() { | ||
UI ui = getUI(); | ||
StateTree.ExecutionRegistration addToUiRegistration = ui | ||
.beforeClientResponse(ui, context -> { | ||
if (isOpened() && !isAttached()) { | ||
ui.addToModalComponent(component); | ||
ui.setChildComponentModal(component, | ||
isModalSupplier.get()); | ||
autoAdded = true; | ||
} | ||
if (afterProgrammaticNavigationListenerRegistration != null) { | ||
afterProgrammaticNavigationListenerRegistration | ||
.remove(); | ||
} | ||
}); | ||
if (ui.getSession() != null) { | ||
afterProgrammaticNavigationListenerRegistration = ui | ||
.addAfterNavigationListener(event -> { | ||
if (event.getLocationChangeEvent() | ||
.getTrigger() == NavigationTrigger.PROGRAMMATIC) { | ||
addToUiRegistration.remove(); | ||
afterProgrammaticNavigationListenerRegistration | ||
.remove(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private void handleClose() { | ||
if (!isOpened() && autoAdded) { | ||
autoAdded = false; | ||
component.getElement().removeFromParent(); | ||
} | ||
} | ||
|
||
private UI getUI() { | ||
UI ui = UI.getCurrent(); | ||
if (ui == null) { | ||
throw new IllegalStateException("UI instance is not available. " | ||
+ "It means that you are calling this method " | ||
+ "out of a normal workflow where it's always implicitly set. " | ||
+ "That may happen if you call the method from the custom thread without " | ||
+ "'UI::access' or from tests without proper initialization."); | ||
} | ||
return ui; | ||
} | ||
|
||
private boolean isOpened() { | ||
return component.getElement().getProperty("opened", false); | ||
} | ||
|
||
private boolean isAttached() { | ||
return component.getElement().getNode().getParent() != null; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are those listener added intentionally? Sounds like a breaking change that every confirm click automatically closes the dialog.
Example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently all of those close the dialog already: https://github.com/vaadin/web-components/blob/e76c4576c2d1875e16681677bbf829637bf48e4f/packages/confirm-dialog/src/vaadin-confirm-dialog-mixin.js#L435-L451
But I was thinking about using a different approach as well, it seems better to stick with the
opened-changed
event than make assumptions what individual actions would do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks! Didn't know the web component does that already (I'm using my own confirm dialog based on the normal dialog API)
Interesting.. another thing to keep in mind when using the official confirm dialog.