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
This commit is contained in:
Aakansha Doshi 2021-11-16 18:55:56 +05:30 committed by GitHub
parent b9abcc825a
commit 3ff9744b39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 51 deletions

View File

@ -1,11 +1,10 @@
import { useState } from "react"; import { useState } from "react";
import { t } from "../i18n"; import { t } from "../i18n";
import { useIsMobile } from "./App"; import { useIsMobile } from "./App";
import { Dialog } from "./Dialog";
import { trash } from "./icons"; import { trash } from "./icons";
import { ToolButton } from "./ToolButton"; import { ToolButton } from "./ToolButton";
import "./ClearCanvas.scss"; import ConfirmDialog from "./ConfirmDialog";
const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => { const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
const [showDialog, setShowDialog] = useState(false); const [showDialog, setShowDialog] = useState(false);
@ -26,39 +25,16 @@ const ClearCanvas = ({ onConfirm }: { onConfirm: () => void }) => {
/> />
{showDialog && ( {showDialog && (
<Dialog <ConfirmDialog
onCloseRequest={toggleDialog} onConfirm={() => {
title={t("clearCanvasDialog.title")}
className="clear-canvas"
small={true}
>
<>
<p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
<div className="clear-canvas-buttons">
<ToolButton
type="button"
title={t("buttons.clear")}
aria-label={t("buttons.clear")}
label={t("buttons.clear")}
onClick={() => {
onConfirm(); onConfirm();
toggleDialog(); toggleDialog();
}} }}
data-testid="confirm-clear-canvas-button" onCancel={toggleDialog}
className="clear-canvas--confirm" title={t("clearCanvasDialog.title")}
/> >
<ToolButton <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
type="button" </ConfirmDialog>
title={t("buttons.cancel")}
aria-label={t("buttons.cancel")}
label={t("buttons.cancel")}
onClick={toggleDialog}
data-testid="cancel-clear-canvas-button"
className="clear-canvas--cancel"
/>
</div>
</>
</Dialog>
)} )}
</> </>
); );

View File

@ -1,12 +1,12 @@
@import "../css/variables.module"; @import "../css/variables.module";
.excalidraw { .excalidraw {
.clear-canvas { .confirm-dialog {
&-buttons { &-buttons {
display: flex; display: flex;
padding: 0.2rem 0; padding: 0.2rem 0;
justify-content: flex-end; justify-content: flex-end;
}
.ToolIcon__icon { .ToolIcon__icon {
min-width: 2.5rem; min-width: 2.5rem;
width: auto; width: auto;
@ -14,10 +14,9 @@
} }
.ToolIcon_type_button { .ToolIcon_type_button {
margin-left: 1.5rem; margin-left: 0.8rem;
padding: 0 0.5rem; padding: 0 0.5rem;
} }
}
&__content { &__content {
font-size: 1rem; font-size: 1rem;
@ -34,9 +33,5 @@
color: $oc-white; color: $oc-white;
} }
} }
&--cancel.ToolIcon_type_button {
background-color: $oc-gray-2;
}
} }
} }

View File

@ -0,0 +1,52 @@
import { t } from "../i18n";
import { Dialog, DialogProps } from "./Dialog";
import { ToolButton } from "./ToolButton";
import "./ConfirmDialog.scss";
interface Props extends Omit<DialogProps, "onCloseRequest"> {
onConfirm: () => void;
onCancel: () => void;
confirmText?: string;
cancelText?: string;
}
const ConfirmDialog = (props: Props) => {
const {
onConfirm,
onCancel,
children,
confirmText = t("buttons.confirm"),
cancelText = t("buttons.cancel"),
className = "",
...rest
} = props;
return (
<Dialog
onCloseRequest={onCancel}
small={true}
{...rest}
className={`confirm-dialog ${className}`}
>
{children}
<div className="confirm-dialog-buttons">
<ToolButton
type="button"
title={cancelText}
aria-label={cancelText}
label={cancelText}
onClick={onCancel}
className="confirm-dialog--cancel"
/>
<ToolButton
type="button"
title={confirmText}
aria-label={confirmText}
label={confirmText}
onClick={onConfirm}
className="confirm-dialog--confirm"
/>
</div>
</Dialog>
);
};
export default ConfirmDialog;

View File

@ -10,7 +10,7 @@ import { Island } from "./Island";
import { Modal } from "./Modal"; import { Modal } from "./Modal";
import { AppState } from "../types"; import { AppState } from "../types";
export const Dialog = (props: { export interface DialogProps {
children: React.ReactNode; children: React.ReactNode;
className?: string; className?: string;
small?: boolean; small?: boolean;
@ -18,7 +18,8 @@ export const Dialog = (props: {
title: React.ReactNode; title: React.ReactNode;
autofocus?: boolean; autofocus?: boolean;
theme?: AppState["theme"]; theme?: AppState["theme"];
}) => { }
export const Dialog = (props: DialogProps) => {
const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>(); const [islandNode, setIslandNode] = useCallbackRefState<HTMLDivElement>();
const [lastActiveElement] = useState(document.activeElement); const [lastActiveElement] = useState(document.activeElement);
const { id } = useExcalidrawContainer(); const { id } = useExcalidrawContainer();

View File

@ -135,7 +135,8 @@
"zenMode": "Zen mode", "zenMode": "Zen mode",
"exitZenMode": "Exit zen mode", "exitZenMode": "Exit zen mode",
"cancel": "Cancel", "cancel": "Cancel",
"clear": "Clear" "clear": "Clear",
"confirm": "Confirm"
}, },
"alerts": { "alerts": {
"clearReset": "This will clear the whole canvas. Are you sure?", "clearReset": "This will clear the whole canvas. Are you sure?",