Skip to content

Commit fab2e07

Browse files
committed
feat: close confirm dialog when clicking outside
1 parent 84b06e6 commit fab2e07

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

_build/js/src/ui/cofirmDialog/index.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { createModAIShadow } from '../dom/modAIShadow';
44
import { createElement } from '../utils';
55

66
import type { Button } from '../dom/button';
7+
import type { El } from '../utils';
78

89
type ConfirmDialogOptions = {
910
title: string;
10-
content: string;
11+
content: El;
1112
onConfirm: () => void;
1213
onCancel?: () => void;
1314
confirmText: string;
@@ -51,31 +52,26 @@ export const confirmDialog = (config: ConfirmDialogOptions) => {
5152
{ tabIndex: -1 },
5253
);
5354

54-
const overlay = createElement(
55+
const dialog = createElement(
5556
'div',
56-
'modai--root overlay',
57+
'dialog',
5758
[
58-
createElement(
59-
'div',
60-
'dialog',
61-
[
62-
createElement('h3', 'title', config.title),
63-
createElement('p', 'message', config.content),
64-
createElement('div', 'buttons', [
65-
config.showCancel && cancelBtn,
66-
config.showConfirm && confirmBtn,
67-
]),
68-
],
69-
{ tabIndex: -1 },
70-
),
59+
createElement('h3', 'title', config.title),
60+
createElement('p', 'message', config.content),
61+
createElement('div', 'buttons', [
62+
config.showCancel && cancelBtn,
63+
config.showConfirm && confirmBtn,
64+
]),
7165
],
72-
{
73-
ariaModal: 'true',
74-
role: 'dialog',
75-
ariaLabel: config.title,
76-
},
66+
{ tabIndex: -1 },
7767
);
7868

69+
const overlay = createElement('div', 'modai--root overlay', [dialog], {
70+
ariaModal: 'true',
71+
role: 'dialog',
72+
ariaLabel: config.title,
73+
});
74+
7975
const destroyDialog = () => {
8076
document.removeEventListener('keydown', handleDialogKeyDown);
8177
overlay.remove();
@@ -124,6 +120,16 @@ export const confirmDialog = (config: ConfirmDialogOptions) => {
124120

125121
overlay.addEventListener('keydown', handleDialogKeyDown);
126122

123+
overlay.addEventListener('click', () => {
124+
closeDialog();
125+
});
126+
127+
dialog.addEventListener('click', (e) => {
128+
e.preventDefault();
129+
e.stopPropagation();
130+
e.stopImmediatePropagation();
131+
});
132+
127133
shadowRoot.appendChild(overlay);
128134
document.body.append(shadow);
129135
};

_build/js/src/ui/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const applyStyles = (element: HTMLElement, styleObj: string) => {
22
element.className = styleObj;
33
};
44

5-
type El = string | HTMLElement | Element | false | undefined;
5+
export type El = string | HTMLElement | Element | false | undefined;
66

77
export const createElement = <K extends keyof HTMLElementTagNameMap>(
88
type: K,

0 commit comments

Comments
 (0)