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: ignore target set while popover is detached until re-attached (#8350) (CP: 24.5) #8353

Merged
merged 1 commit into from
Dec 16, 2024
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
29 changes: 18 additions & 11 deletions packages/popover/src/vaadin-popover-target-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,32 @@ export const PopoverTargetMixin = (superClass) =>
*/
target: {
type: Object,
observer: '__targetChanged',
},

/** @private */
__isConnected: {
type: Boolean,
sync: true,
},
};
}

static get observers() {
return ['__targetOrConnectedChanged(target, __isConnected)'];
}

/** @protected */
connectedCallback() {
super.connectedCallback();

if (this.target) {
this._addTargetListeners(this.target);
}
this.__isConnected = true;
}

/** @protected */
disconnectedCallback() {
super.disconnectedCallback();

if (this.target) {
this._removeTargetListeners(this.target);
}
this.__isConnected = false;
}

/** @private */
Expand Down Expand Up @@ -82,14 +87,16 @@ export const PopoverTargetMixin = (superClass) =>
}

/** @private */
__targetChanged(target, oldTarget) {
if (oldTarget) {
this._removeTargetListeners(oldTarget);
__targetOrConnectedChanged(target, isConnected) {
if (this.__previousTarget && (this.__previousTarget !== target || !isConnected)) {
this._removeTargetListeners(this.__previousTarget);
}

if (target) {
if (target && isConnected) {
this._addTargetListeners(target);
}

this.__previousTarget = target;
}

/**
Expand Down
69 changes: 69 additions & 0 deletions packages/popover/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,75 @@ describe('popover', () => {
});
});

describe('detach and re-attach', () => {
let target;

beforeEach(() => {
target = fixtureSync('<button>Target</button>');
});

it('should not open on target click when detached', async () => {
popover.target = target;
await nextUpdate(popover);

popover.remove();
target.click();

expect(popover.opened).to.be.false;
});

it('should open on target click when re-attached', async () => {
popover.target = target;
await nextUpdate(popover);

popover.remove();

target.parentNode.appendChild(popover);
await nextUpdate(popover);

target.click();

expect(popover.opened).to.be.true;
});

it('should not open on target click when target set while detached', async () => {
popover.remove();

popover.target = target;
await nextUpdate(popover);

target.click();

expect(popover.opened).to.be.false;
});

it('should open when target set while detached after re-attached', async () => {
popover.remove();

popover.target = target;
await nextUpdate(popover);

target.parentNode.appendChild(popover);
await nextUpdate(popover);

target.click();

expect(popover.opened).to.be.true;
});

it('should not open on target click when target is cleared', async () => {
popover.target = target;
await nextUpdate(popover);

popover.target = null;
await nextUpdate(popover);

target.click();

expect(popover.opened).to.be.false;
});
});

describe('dimensions', () => {
function getStyleValue(element) {
return element
Expand Down
Loading