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

refactor!: close popover without trigger on Esc and outside click (#8409) (CP: 24.6) #8666

Merged
merged 1 commit into from
Feb 10, 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
3 changes: 2 additions & 1 deletion packages/popover/src/vaadin-popover.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ declare class Popover extends PopoverPositionMixin(
* - outside click (unless `noCloseOnOutsideClick` property is true)
*
* When setting `trigger` property to `null`, `undefined` or empty array, the popover
* can be only opened or closed programmatically by changing `opened` property.
* can be only opened programmatically by changing `opened` property. Note, closing
* on Escape press or outside click is still allowed unless explicitly disabled.
*/
trigger: PopoverTrigger[] | null | undefined;

Expand Down
21 changes: 5 additions & 16 deletions packages/popover/src/vaadin-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ class Popover extends PopoverPositionMixin(
* - outside click (unless `noCloseOnOutsideClick` property is true)
*
* When setting `trigger` property to `null`, `undefined` or empty array, the popover
* can be only opened or closed programmatically by changing `opened` property.
* can be only opened programmatically by changing `opened` property. Note, closing
* on Escape press or outside click is still allowed unless explicitly disabled.
*/
trigger: {
type: Array,
Expand Down Expand Up @@ -611,7 +612,6 @@ class Popover extends PopoverPositionMixin(
__onGlobalClick(event) {
if (
this.opened &&
!this.__isManual &&
!this.modal &&
!event.composedPath().some((el) => el === this._overlayElement || el === this.target) &&
!this.noCloseOnOutsideClick &&
Expand Down Expand Up @@ -646,13 +646,7 @@ class Popover extends PopoverPositionMixin(
return;
}

if (
event.key === 'Escape' &&
!this.noCloseOnEsc &&
this.opened &&
!this.__isManual &&
isLastOverlay(this._overlayElement)
) {
if (event.key === 'Escape' && !this.noCloseOnEsc && this.opened && isLastOverlay(this._overlayElement)) {
// Prevent closing parent overlay (e.g. dialog)
event.stopPropagation();
this._openedStateController.close(true);
Expand Down Expand Up @@ -939,7 +933,7 @@ class Popover extends PopoverPositionMixin(
* @private
*/
__onEscapePress(e) {
if (this.noCloseOnEsc || this.__isManual) {
if (this.noCloseOnEsc) {
e.preventDefault();
}
}
Expand All @@ -949,7 +943,7 @@ class Popover extends PopoverPositionMixin(
* @private
*/
__onOutsideClick(e) {
if (this.noCloseOnOutsideClick || this.__isManual) {
if (this.noCloseOnOutsideClick) {
e.preventDefault();
}
}
Expand All @@ -959,11 +953,6 @@ class Popover extends PopoverPositionMixin(
return Array.isArray(this.trigger) && this.trigger.includes(trigger);
}

/** @private */
get __isManual() {
return this.trigger == null || (Array.isArray(this.trigger) && this.trigger.length === 0);
}

/** @private */
__updateDimension(overlay, dimension, value) {
const prop = `--_vaadin-popover-content-${dimension}`;
Expand Down
20 changes: 10 additions & 10 deletions packages/popover/test/trigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,51 +492,51 @@ describe('trigger', () => {
expect(overlay.opened).to.be.true;
});

it(`should not close on global Escape press with trigger set to ${trigger}`, async () => {
it(`should close on global Escape press with trigger set to ${trigger}`, async () => {
popover.opened = true;
await nextRender();

esc(document.body);
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
expect(overlay.opened).to.be.false;
});

it(`should not close on target Escape press with trigger set to ${trigger}`, async () => {
it(`should close on target Escape press with trigger set to ${trigger}`, async () => {
popover.opened = true;
await nextRender();

esc(target);
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
expect(overlay.opened).to.be.false;
});

it(`should not close on global Escape press when modal with trigger set to ${trigger}`, async () => {
it(`should close on global Escape press when modal with trigger set to ${trigger}`, async () => {
popover.modal = true;
popover.opened = true;
await nextRender();

esc(document.body);
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
expect(overlay.opened).to.be.false;
});

it(`should not close on outside click when not modal with trigger set to ${trigger}`, async () => {
it(`should close on outside click when not modal with trigger set to ${trigger}`, async () => {
popover.opened = true;
await nextRender();

outsideClick();
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
expect(overlay.opened).to.be.false;
});

it(`should not close on outside click when modal with trigger set to ${trigger}`, async () => {
it(`should close on outside click when modal with trigger set to ${trigger}`, async () => {
popover.modal = true;
popover.opened = true;
await nextRender();

outsideClick();
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
expect(overlay.opened).to.be.false;
});

it(`should close when setting opened to false with trigger set to ${trigger}`, async () => {
Expand Down