Skip to content

Commit 54908f2

Browse files
committed
fix: prevent dialog from closing when selecting elements from the dialog
closes #129
1 parent 40c8f52 commit 54908f2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

libs/dialog/src/lib/dialog.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ export class DialogComponent implements OnInit, OnDestroy {
145145
filter(({ key }) => key === 'Escape'),
146146
map(() => closeConfig.escape),
147147
),
148-
backdropClick$.pipe(map(() => closeConfig.backdrop)),
148+
backdropClick$.pipe(
149+
filter(() => this.document.getSelection()?.toString() === ''),
150+
map(() => closeConfig.backdrop),
151+
),
149152
)
150153
.pipe(
151154
takeUntil(this.destroy$),

libs/dialog/src/lib/specs/dialog.component.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,17 @@ describe('DialogComponent', () => {
9494
expect(spectator.query('.ngneat-dialog-backdrop')).toBeTruthy();
9595
});
9696

97+
it('should not close when text is selected', () => {
98+
const { close } = spectator.inject(InternalDialogRef);
99+
spyOn(document, 'getSelection').and.returnValue({ toString: () => 'selected text' } as Selection);
100+
101+
spectator.dispatchMouseEvent('.ngneat-dialog-backdrop', 'click');
102+
expect(close).not.toHaveBeenCalled();
103+
});
104+
97105
it('backdropClick$ should point to element', () => {
98106
let backdropClicked = false;
107+
spyOn(document, 'getSelection').and.returnValue({ toString: () => '' } as Selection);
99108
spectator.inject(InternalDialogRef).backdropClick$.subscribe({
100109
next: () => (backdropClicked = true),
101110
});

0 commit comments

Comments
 (0)