Skip to content

Commit

Permalink
Merge pull request #130 from rg-lahcene/fix/backdrop-close-issue-on-s…
Browse files Browse the repository at this point in the history
…elect

fix: prevent dialog from closing  when selecting a text from the dialog
  • Loading branch information
NetanelBasal authored Dec 2, 2024
2 parents 40c8f52 + 54908f2 commit a138288
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/dialog/src/lib/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$),
Expand Down
9 changes: 9 additions & 0 deletions libs/dialog/src/lib/specs/dialog.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down

0 comments on commit a138288

Please sign in to comment.