From 54908f2c5be78e9144e242c04ed2203737cf7adf Mon Sep 17 00:00:00 2001 From: lahcene Date: Mon, 2 Dec 2024 14:46:01 +0100 Subject: [PATCH] fix: prevent dialog from closing when selecting elements from the dialog closes #129 --- libs/dialog/src/lib/dialog.component.ts | 5 ++++- libs/dialog/src/lib/specs/dialog.component.spec.ts | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/dialog/src/lib/dialog.component.ts b/libs/dialog/src/lib/dialog.component.ts index 1e775a2..7842b08 100644 --- a/libs/dialog/src/lib/dialog.component.ts +++ b/libs/dialog/src/lib/dialog.component.ts @@ -145,7 +145,10 @@ export class DialogComponent implements OnInit, OnDestroy { filter(({ key }) => key === 'Escape'), map(() => closeConfig.escape), ), - backdropClick$.pipe(map(() => closeConfig.backdrop)), + backdropClick$.pipe( + filter(() => this.document.getSelection()?.toString() === ''), + map(() => closeConfig.backdrop), + ), ) .pipe( takeUntil(this.destroy$), diff --git a/libs/dialog/src/lib/specs/dialog.component.spec.ts b/libs/dialog/src/lib/specs/dialog.component.spec.ts index 3e6194e..a096869 100644 --- a/libs/dialog/src/lib/specs/dialog.component.spec.ts +++ b/libs/dialog/src/lib/specs/dialog.component.spec.ts @@ -94,8 +94,17 @@ describe('DialogComponent', () => { expect(spectator.query('.ngneat-dialog-backdrop')).toBeTruthy(); }); + it('should not close when text is selected', () => { + const { close } = spectator.inject(InternalDialogRef); + spyOn(document, 'getSelection').and.returnValue({ toString: () => 'selected text' } as Selection); + + spectator.dispatchMouseEvent('.ngneat-dialog-backdrop', 'click'); + expect(close).not.toHaveBeenCalled(); + }); + it('backdropClick$ should point to element', () => { let backdropClicked = false; + spyOn(document, 'getSelection').and.returnValue({ toString: () => '' } as Selection); spectator.inject(InternalDialogRef).backdropClick$.subscribe({ next: () => (backdropClicked = true), });