Skip to content

Commit

Permalink
Merge pull request #1065 from Schleuse/task/#1064-HTMLDialogElement-c…
Browse files Browse the repository at this point in the history
…lose-emits-a-close-Event-even-when-the-dialog-is-already-closed

#1064@patch: Emit close event only when dialog was open.
  • Loading branch information
capricorn86 authored Sep 14, 2023
2 parents 63521a3 + e824b93 commit 4724033
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ export default class HTMLDialogElement extends HTMLElement implements IHTMLDialo
* @param [returnValue] ReturnValue.
*/
public close(returnValue = ''): void {
const wasOpen = this.open;
this.removeAttribute('open');
this.returnValue = returnValue;
this.dispatchEvent(new Event('close'));
if (wasOpen) {
this.dispatchEvent(new Event('close'));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ describe('HTMLDialogElement', () => {
expect((<Event>(<unknown>dispatched)).bubbles).toBe(false);
});

it('Should only dispatch a close event when dialog wasnt already closed', () => {
let dispatched: Event | null = null;
element.addEventListener('close', (event: Event) => (dispatched = event));
element.close();
expect(dispatched).toBe(null);
});

it('Should dispatch a close event when closing a modal', () => {
let dispatched: Event | null = null;
element.addEventListener('close', (event: Event) => (dispatched = event));
Expand Down

0 comments on commit 4724033

Please sign in to comment.