Browse Source

feat: create confirm dialog to use instead of window.confirm (#4256)

* feat: create confirm dialog to use instead of window.confirm

* move confirm to right

* add types

* less margin
vanilla_orig
Aakansha Doshi 3 years ago
parent
commit
3ff9744b39
No account linked to committer's email address

+ 9
- 33
src/components/ClearCanvas.tsx View File

1
 import { useState } from "react";
1
 import { useState } from "react";
2
 import { t } from "../i18n";
2
 import { t } from "../i18n";
3
 import { useIsMobile } from "./App";
3
 import { useIsMobile } from "./App";
4
-import { Dialog } from "./Dialog";
5
 import { trash } from "./icons";
4
 import { trash } from "./icons";
6
 import { ToolButton } from "./ToolButton";
5
 import { ToolButton } from "./ToolButton";
7
 
6
 
8
-import "./ClearCanvas.scss";
7
+import ConfirmDialog from "./ConfirmDialog";
9
 
8
 
10
 const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
9
 const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
11
   const [showDialog, setShowDialog] = useState(false);
10
   const [showDialog, setShowDialog] = useState(false);
26
       />
25
       />
27
 
26
 
28
       {showDialog && (
27
       {showDialog && (
29
-        <Dialog
30
-          onCloseRequest={toggleDialog}
28
+        <ConfirmDialog
29
+          onConfirm={() => {
30
+            onConfirm();
31
+            toggleDialog();
32
+          }}
33
+          onCancel={toggleDialog}
31
           title={t("clearCanvasDialog.title")}
34
           title={t("clearCanvasDialog.title")}
32
-          className="clear-canvas"
33
-          small={true}
34
         >
35
         >
35
-          <>
36
-            <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
37
-            <div className="clear-canvas-buttons">
38
-              <ToolButton
39
-                type="button"
40
-                title={t("buttons.clear")}
41
-                aria-label={t("buttons.clear")}
42
-                label={t("buttons.clear")}
43
-                onClick={() => {
44
-                  onConfirm();
45
-                  toggleDialog();
46
-                }}
47
-                data-testid="confirm-clear-canvas-button"
48
-                className="clear-canvas--confirm"
49
-              />
50
-              <ToolButton
51
-                type="button"
52
-                title={t("buttons.cancel")}
53
-                aria-label={t("buttons.cancel")}
54
-                label={t("buttons.cancel")}
55
-                onClick={toggleDialog}
56
-                data-testid="cancel-clear-canvas-button"
57
-                className="clear-canvas--cancel"
58
-              />
59
-            </div>
60
-          </>
61
-        </Dialog>
36
+          <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
37
+        </ConfirmDialog>
62
       )}
38
       )}
63
     </>
39
     </>
64
   );
40
   );

src/components/ClearCanvas.scss → src/components/ConfirmDialog.scss View File

1
 @import "../css/variables.module";
1
 @import "../css/variables.module";
2
 
2
 
3
 .excalidraw {
3
 .excalidraw {
4
-  .clear-canvas {
4
+  .confirm-dialog {
5
     &-buttons {
5
     &-buttons {
6
       display: flex;
6
       display: flex;
7
       padding: 0.2rem 0;
7
       padding: 0.2rem 0;
8
       justify-content: flex-end;
8
       justify-content: flex-end;
9
+    }
10
+    .ToolIcon__icon {
11
+      min-width: 2.5rem;
12
+      width: auto;
13
+      font-size: 1rem;
14
+    }
9
 
15
 
10
-      .ToolIcon__icon {
11
-        min-width: 2.5rem;
12
-        width: auto;
13
-        font-size: 1rem;
14
-      }
15
-
16
-      .ToolIcon_type_button {
17
-        margin-left: 1.5rem;
18
-        padding: 0 0.5rem;
19
-      }
16
+    .ToolIcon_type_button {
17
+      margin-left: 0.8rem;
18
+      padding: 0 0.5rem;
20
     }
19
     }
21
 
20
 
22
     &__content {
21
     &__content {
34
         color: $oc-white;
33
         color: $oc-white;
35
       }
34
       }
36
     }
35
     }
37
-
38
-    &--cancel.ToolIcon_type_button {
39
-      background-color: $oc-gray-2;
40
-    }
41
   }
36
   }
42
 }
37
 }

+ 52
- 0
src/components/ConfirmDialog.tsx View File

1
+import { t } from "../i18n";
2
+import { Dialog, DialogProps } from "./Dialog";
3
+import { ToolButton } from "./ToolButton";
4
+
5
+import "./ConfirmDialog.scss";
6
+
7
+interface Props extends Omit<DialogProps, "onCloseRequest"> {
8
+  onConfirm: () => void;
9
+  onCancel: () => void;
10
+  confirmText?: string;
11
+  cancelText?: string;
12
+}
13
+const ConfirmDialog = (props: Props) => {
14
+  const {
15
+    onConfirm,
16
+    onCancel,
17
+    children,
18
+    confirmText = t("buttons.confirm"),
19
+    cancelText = t("buttons.cancel"),
20
+    className = "",
21
+    ...rest
22
+  } = props;
23
+  return (
24
+    <Dialog
25
+      onCloseRequest={onCancel}
26
+      small={true}
27
+      {...rest}
28
+      className={`confirm-dialog ${className}`}
29
+    >
30
+      {children}
31
+      <div className="confirm-dialog-buttons">
32
+        <ToolButton
33
+          type="button"
34
+          title={cancelText}
35
+          aria-label={cancelText}
36
+          label={cancelText}
37
+          onClick={onCancel}
38
+          className="confirm-dialog--cancel"
39
+        />
40
+        <ToolButton
41
+          type="button"
42
+          title={confirmText}
43
+          aria-label={confirmText}
44
+          label={confirmText}
45
+          onClick={onConfirm}
46
+          className="confirm-dialog--confirm"
47
+        />
48
+      </div>
49
+    </Dialog>
50
+  );
51
+};
52
+export default ConfirmDialog;

+ 3
- 2
src/components/Dialog.tsx View File

10
 import { Modal } from "./Modal";
10
 import { Modal } from "./Modal";
11
 import { AppState } from "../types";
11
 import { AppState } from "../types";
12
 
12
 
13
-export const Dialog = (props: {
13
+export interface DialogProps {
14
   children: React.ReactNode;
14
   children: React.ReactNode;
15
   className?: string;
15
   className?: string;
16
   small?: boolean;
16
   small?: boolean;
18
   title: React.ReactNode;
18
   title: React.ReactNode;
19
   autofocus?: boolean;
19
   autofocus?: boolean;
20
   theme?: AppState["theme"];
20
   theme?: AppState["theme"];
21
-}) => {
21
+}
22
+export const Dialog = (props: DialogProps) => {
22
   const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>();
23
   const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>();
23
   const [lastActiveElement] = useState(document.activeElement);
24
   const [lastActiveElement] = useState(document.activeElement);
24
   const { id } = useExcalidrawContainer();
25
   const { id } = useExcalidrawContainer();

+ 2
- 1
src/locales/en.json View File

135
     "zenMode": "Zen mode",
135
     "zenMode": "Zen mode",
136
     "exitZenMode": "Exit zen mode",
136
     "exitZenMode": "Exit zen mode",
137
     "cancel": "Cancel",
137
     "cancel": "Cancel",
138
-    "clear": "Clear"
138
+    "clear": "Clear",
139
+    "confirm": "Confirm"
139
   },
140
   },
140
   "alerts": {
141
   "alerts": {
141
     "clearReset": "This will clear the whole canvas. Are you sure?",
142
     "clearReset": "This will clear the whole canvas. Are you sure?",

Loading…
Cancel
Save