Skip to content

Commit

Permalink
feat: fire closed event when popover overlay is closed (#7532)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Jul 11, 2024
1 parent 1126416 commit a8ebb68
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/popover/src/vaadin-popover.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type PopoverOpenedChangedEvent = CustomEvent<{ value: boolean }>;

export interface PopoverCustomEventMap {
'opened-changed': PopoverOpenedChangedEvent;

closed: Event;
}

export type PopoverEventMap = HTMLElementEventMap & PopoverCustomEventMap;
Expand Down Expand Up @@ -64,6 +66,7 @@ export type PopoverEventMap = HTMLElementEventMap & PopoverCustomEventMap;
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} closed - Fired when the popover is closed.
*/
declare class Popover extends PopoverPositionMixin(
PopoverTargetMixin(OverlayClassMixin(ThemePropertyMixin(ElementMixin(HTMLElement)))),
Expand Down
9 changes: 9 additions & 0 deletions packages/popover/src/vaadin-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class PopoverOpenedStateController {
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} closed - Fired when the popover is closed.
*
* @customElement
* @extends HTMLElement
Expand Down Expand Up @@ -715,6 +716,8 @@ class Popover extends PopoverPositionMixin(
if (this.modal && this.target.style.pointerEvents) {
this.target.style.pointerEvents = '';
}

this.dispatchEvent(new CustomEvent('closed'));
}

/**
Expand Down Expand Up @@ -771,6 +774,12 @@ class Popover extends PopoverPositionMixin(
this.__updateDimension(overlay, 'width', width);
}
}

/**
* Fired when the popover is closed.
*
* @event closed
*/
}

defineCustomElement(Popover);
Expand Down
28 changes: 28 additions & 0 deletions packages/popover/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,32 @@ describe('popover', () => {
expect(overlay.getAttribute('style')).to.be.not.ok;
});
});

describe('closed event', () => {
beforeEach(async () => {
popover.opened = true;
await nextRender();
});

it('should dispatch closed event when closed', async () => {
const closedSpy = sinon.spy();
popover.addEventListener('closed', closedSpy);
popover.opened = false;
await nextRender();
expect(closedSpy).to.be.calledOnce;
});

it('should dispatch closed event after overlay is closed', async () => {
const closedPromise = new Promise((resolve) => {
const closedListener = () => {
expect(overlay.parentElement).to.be.not.ok;
resolve();
};
popover.addEventListener('closed', closedListener, { once: true });
});
popover.opened = false;
await nextRender();
await closedPromise;
});
});
});
4 changes: 4 additions & 0 deletions packages/popover/test/typings/popover.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ popover.addEventListener('opened-changed', (event) => {
assertType<PopoverOpenedChangedEvent>(event);
assertType<boolean>(event.detail.value);
});

popover.addEventListener('closed', (event) => {
assertType<Event>(event);
});

0 comments on commit a8ebb68

Please sign in to comment.