Skip to content

Commit

Permalink
fix: ignore target set while popover is detached until re-attached (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Dec 16, 2024
1 parent a413851 commit b402736
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
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

0 comments on commit b402736

Please # to comment.