Skip to content

Commit c1f3c87

Browse files
authored
feat: add closed event when Notification is closed (#7473)
* feat: add `closed` event when Notification is closed * test: add tests for the closed event * docs: add @fires closed to component JSDoc * test: add typing test for the closed event * test: remove listenOnce and use addEventListener instead
1 parent 07d0f0f commit c1f3c87

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

packages/notification/src/vaadin-notification.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ export type NotificationRenderer = (root: HTMLElement, notification: Notificatio
2727
*/
2828
export type NotificationOpenedChangedEvent = CustomEvent<{ value: boolean }>;
2929

30+
/**
31+
* Fired when the notification is closed.
32+
*/
33+
export type NotificationClosedEvent = CustomEvent;
34+
3035
export interface NotificationCustomEventMap {
3136
'opened-changed': NotificationOpenedChangedEvent;
37+
closed: NotificationClosedEvent;
3238
}
3339

3440
export interface NotificationEventMap extends HTMLElementEventMap, NotificationCustomEventMap {}
@@ -99,6 +105,7 @@ declare class NotificationCard extends ThemableMixin(HTMLElement) {}
99105
* propagated to the internal `<vaadin-notification-card>`.
100106
*
101107
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
108+
* @fires {CustomEvent} closed - Fired when the notification is closed.
102109
*/
103110
declare class Notification extends OverlayClassMixin(ThemePropertyMixin(ElementMixin(HTMLElement))) {
104111
/**

packages/notification/src/vaadin-notification.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class NotificationCard extends ThemableMixin(PolymerElement) {
257257
* propagated to the internal `<vaadin-notification-card>`.
258258
*
259259
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
260+
* @fires {CustomEvent} closed - Fired when the notification is closed.
260261
*
261262
* @customElement
262263
* @extends HTMLElement
@@ -532,6 +533,7 @@ class Notification extends OverlayClassMixin(ThemePropertyMixin(ElementMixin(Pol
532533
}
533534
this._card.removeAttribute('closing');
534535
this._container.opened = Boolean(this._container.firstElementChild);
536+
this.dispatchEvent(new CustomEvent('closed'));
535537
}
536538

537539
/** @private */

packages/notification/test/notification.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ describe('vaadin-notification', () => {
5353
expect(notification.opened).to.be.true;
5454
});
5555

56+
it('should dispatch closed event', async () => {
57+
const spy = sinon.spy();
58+
notification.addEventListener('closed', spy);
59+
notification.opened = false;
60+
await aTimeout(0);
61+
expect(spy.calledOnce).to.be.true;
62+
});
63+
64+
it('closed event should be called after overlay is closed', async () => {
65+
const closedPromise = new Promise((resolve) => {
66+
const closedListener = () => {
67+
expect(notification._container.parentElement).to.be.not.ok;
68+
resolve();
69+
};
70+
listenOnce(notification, 'closed', closedListener);
71+
});
72+
notification.opened = false;
73+
await aTimeout(0);
74+
await closedPromise;
75+
});
76+
5677
describe('vaadin-notification-container', () => {
5778
it('should be in the body when notification opens', () => {
5879
expect(document.body.querySelectorAll('vaadin-notification-container').length).to.be.equal(1);

packages/notification/test/typings/notification.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { ElementMixinClass } from '@vaadin/component-base/src/element-mixin
33
import type { OverlayClassMixinClass } from '@vaadin/component-base/src/overlay-class-mixin.js';
44
import type { ThemePropertyMixinClass } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
55
import type {
6+
NotificationClosedEvent,
67
NotificationOpenedChangedEvent,
78
NotificationPosition,
89
NotificationRenderer,
@@ -31,6 +32,10 @@ notification.addEventListener('opened-changed', (event) => {
3132
assertType<boolean>(event.detail.value);
3233
});
3334

35+
notification.addEventListener('closed', (event) => {
36+
assertType<NotificationClosedEvent>(event);
37+
});
38+
3439
Notification.show('Hello world', { position: 'middle', duration: 7000, theme: 'error' });
3540

3641
const renderer: NotificationRenderer = (root, owner) => {

0 commit comments

Comments
 (0)