Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: migrate base dialog to typescript

Abdullah Atta05cec14c9b7b
diff --git a/apps/web/src/components/dialogs/add-reminder-dialog.tsx b/apps/web/src/components/dialogs/add-reminder-dialog.tsx
index 138c65348..e5b533ab2 100644
--- a/apps/web/src/components/dialogs/add-reminder-dialog.tsx
+++ b/apps/web/src/components/dialogs/add-reminder-dialog.tsx
@@ -152,7 +152,7 @@ export default function AddReminderDialog(props: AddReminderDialogProps) {
isOpen={true}
title={reminderId ? "Edit reminder" : "Add a reminder"}
description={""}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: reminderId ? "Save" : "Add",
disabled:
@@ -210,7 +210,7 @@ export default function AddReminderDialog(props: AddReminderDialogProps) {
props.onClose(true);
}
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Field
id="title"
diff --git a/apps/web/src/components/dialogs/dialog-loader.tsx b/apps/web/src/components/dialogs/dialog-loader.tsx
deleted file mode 100644
index 6d7b6df89..000000000
--- a/apps/web/src/components/dialogs/dialog-loader.tsx
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
-This file is part of the Notesnook project (https://notesnook.com/)
-
-Copyright (C) 2023 Streetwriters (Private) Limited
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-import Dialog from "./dialog";
-import Skeleton from "react-loading-skeleton";
-import "react-loading-skeleton/dist/skeleton.css";
-import { Flex } from "@theme-ui/components";
-
-export default function DialogLoader() {
- return (
- <Dialog
- isOpen={true}
- title={<Skeleton height={30} width={150} />}
- description={<Skeleton height={15} width={100} />}
- >
- <Skeleton height={200} />
- <Flex
- sx={{
- mb: 3,
- mt: 1,
- justifyContent: "flex-end",
- alignItems: "flex-end"
- }}
- >
- <Skeleton height={25} width={70} />
- </Flex>
- </Dialog>
- );
-}
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.tsx
similarity index 52%
rename from apps/web/src/components/dialogs/dialog.js
rename to apps/web/src/components/dialogs/dialog.tsx
index 9107a6f35..b05b1e149 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.tsx
@@ -17,39 +17,48 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Flex, Text, Button as RebassButton } from "@theme-ui/components";
+import React from "react";
+import { Flex, Text, Button, ButtonProps } from "@theme-ui/components";
import * as Icon from "../icons";
-import Modal from "react-modal";
-import { useTheme } from "@emotion/react";
+import ReactModal from "react-modal";
import { FlexScrollContainer } from "../scroll-container";
-Modal.setAppElement("#root");
-function Dialog(props) {
- const theme = useTheme();
+ReactModal.setAppElement("#root");
+
+type DialogButtonProps = ButtonProps & {
+ onClick?: () => void;
+ disabled?: boolean;
+ text: JSX.Element | string;
+ loading?: boolean;
+};
+
+type DialogProps = {
+ isOpen?: boolean;
+ onClose?: (
+ event?: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element>
+ ) => void;
+ onOpen?: () => void;
+ width?: number | string;
+ showCloseButton?: boolean;
+ textAlignment?: "left" | "right" | "center";
+ buttonsAlignment?: "start" | "center" | "end";
+ title?: string;
+ description?: string;
+ positiveButton?: DialogButtonProps | null;
+ negativeButton?: DialogButtonProps | null;
+ footer?: React.Component;
+};
+
+function BaseDialog(props: React.PropsWithChildren<DialogProps>) {
+ // const theme: any = useTheme();
return (
- <Modal
+ <ReactModal
isOpen={props.isOpen || false}
onRequestClose={props.onClose}
shouldCloseOnEsc
shouldReturnFocusAfterClose
shouldFocusAfterRender
- onAfterOpen={(e) => {
- if (!props.onClose) return;
- // we need this work around because ReactModal content spreads over the overlay
- const child = e.contentEl.firstElementChild;
- e.contentEl.onmousedown = function (e) {
- if (!e.screenX && !e.screenY) return;
- if (
- e.x < child.offsetLeft ||
- e.x > child.offsetLeft + child.clientWidth ||
- e.y < child.offsetTop ||
- e.y > child.offsetTop + child.clientHeight
- ) {
- props.onClose();
- }
- };
- if (props.onOpen) props.onOpen();
- }}
+ onAfterOpen={(e) => onAfterOpen(e, props)}
style={{
content: {
top: 0,
@@ -67,27 +76,28 @@ function Dialog(props) {
},
overlay: {
zIndex: 999,
- background: theme.colors.overlay
+ background: "var(--overlay)"
}
}}
>
<Flex
- bg="background"
sx={{
- justifyContent: "stretch",
- position: "relative",
- overflow: "hidden",
- boxShadow: "4px 5px 18px 2px #00000038",
- borderRadius: "dialog",
flexDirection: "column",
width: ["100%", "90%", props.width || "380px"],
- height: ["100%", "auto", "auto"],
maxHeight: ["100%", "80%", "70%"],
+ height: ["100%", "auto", "auto"],
+ bg: "background",
alignSelf: "center",
- overflowY: "hidden"
+ overflowY: "hidden",
+
+ justifyContent: "stretch",
+ position: "relative",
+ overflow: "hidden",
+ boxShadow: "4px 5px 18px 2px #00000038",
+ borderRadius: "dialog"
}}
>
- {props.showClose && (
+ {props.showCloseButton && (
<Icon.Close
sx={{
position: "absolute",
@@ -101,12 +111,12 @@ function Dialog(props) {
onClick={props.onClose}
/>
)}
- <Flex p={4} pb={0} sx={{ flexDirection: "column" }}>
+ <Flex sx={{ flexDirection: "column" }} p={4} pb={0}>
<Text
variant="heading"
sx={{
fontSize: "subheading",
- textAlign: props.alignment || "left",
+ textAlign: props.textAlignment || "left",
color: "text"
}}
>
@@ -116,7 +126,7 @@ function Dialog(props) {
<Text
variant="body"
sx={{
- textAlign: props.alignment || "left",
+ textAlign: props.textAlignment || "left",
color: "fontTertiary"
}}
>
@@ -125,58 +135,82 @@ function Dialog(props) {
)}
</Flex>
<Flex variant="columnFill" sx={{ overflowY: "hidden" }} my={1}>
- {props.noScroll ? (
- props.children
- ) : (
- <FlexScrollContainer style={{ paddingRight: 20, paddingLeft: 20 }}>
- {props.children}
- </FlexScrollContainer>
- )}
+ <FlexScrollContainer style={{ paddingRight: 20, paddingLeft: 20 }}>
+ {props.children}
+ </FlexScrollContainer>
</Flex>
+
{(props.positiveButton || props.negativeButton) && (
<Flex
- sx={{ justifyContent: props.alignment || "flex-end" }}
+ sx={{ justifyContent: props.buttonsAlignment || "end" }}
bg="bgSecondary"
p={1}
px={2}
mt={2}
>
{props.negativeButton && (
- <RebassButton
- variant="dialog"
- data-test-id="dialog-no"
- onClick={props.negativeButton.onClick}
+ <DialogButton
+ {...props.negativeButton}
color="text"
- >
- {props.negativeButton.text || "Cancel"}
- </RebassButton>
+ data-test-id="dialog-no"
+ />
)}
{props.positiveButton && (
- <RebassButton
- {...props.positiveButton.props}
- variant="dialog"
+ <DialogButton
+ {...props.positiveButton}
+ color="primary"
data-test-id="dialog-yes"
- autoFocus={props.positiveButton.autoFocus}
- disabled={props.positiveButton.disabled || false}
- onClick={
- !props.positiveButton.disabled
- ? props.positiveButton.onClick
- : undefined
- }
- >
- {props.positiveButton.loading ? (
- <Icon.Loading size={16} color="primary" />
- ) : (
- props.positiveButton.text || "OK"
- )}
- </RebassButton>
+ />
)}
</Flex>
)}
{props.footer}
</Flex>
- </Modal>
+ </ReactModal>
);
}
-export default Dialog;
+export default BaseDialog;
+
+function DialogButton(props: DialogButtonProps) {
+ return (
+ <Button
+ {...props}
+ variant="primary"
+ sx={{
+ opacity: props.disabled ? 0.7 : 1,
+ fontWeight: "bold",
+ bg: "transparent",
+
+ ":hover": { bg: "bgSecondary" }
+ }}
+ disabled={props.disabled}
+ onClick={props.disabled ? undefined : props.onClick}
+ >
+ {props.loading ? <Icon.Loading size={16} color="primary" /> : props.text}
+ </Button>
+ );
+}
+
+function onAfterOpen(
+ e: ReactModal.OnAfterOpenCallbackOptions | undefined,
+ props: DialogProps
+) {
+ if (!props.onClose || !e) return;
+ // we need this work around because ReactModal content spreads over the overlay
+ const child = e.contentEl.firstElementChild as HTMLElement;
+ if (!child) return;
+
+ e.contentEl.onmousedown = function (e) {
+ if (!e.screenX && !e.screenY) return;
+ if (
+ e.x < child.offsetLeft ||
+ e.x > child.offsetLeft + child.clientWidth ||
+ e.y < child.offsetTop ||
+ e.y > child.offsetTop + child.clientHeight
+ ) {
+ if (props.onClose) props.onClose();
+ }
+ };
+ if (props.onOpen) props.onOpen();
+}
diff --git a/apps/web/src/components/dialogs/email-change-dialog.tsx b/apps/web/src/components/dialogs/email-change-dialog.tsx
index 7a6232201..8077e16dc 100644
--- a/apps/web/src/components/dialogs/email-change-dialog.tsx
+++ b/apps/web/src/components/dialogs/email-change-dialog.tsx
@@ -69,14 +69,14 @@ export default function EmailChangeDialog(props: EmailChangeDialogProps) {
description={
"Your account email will be changed without affecting your subscription or any other settings."
}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: "Next",
disabled: isLoading,
loading: isLoading,
- props: { form: "changeEmailForm" }
+ form: "changeEmailForm"
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Flex
id="changeEmailForm"
diff --git a/apps/web/src/components/dialogs/feature-dialog.tsx b/apps/web/src/components/dialogs/feature-dialog.tsx
index 26bdf1c77..3bad8613b 100644
--- a/apps/web/src/components/dialogs/feature-dialog.tsx
+++ b/apps/web/src/components/dialogs/feature-dialog.tsx
@@ -144,7 +144,7 @@ function FeatureDialog(props: FeatureDialogProps) {
isOpen={true}
title={feature.title}
description={feature.subtitle}
- alignment="center"
+ textAlignment="center"
positiveButton={{
text: (
<Flex>
diff --git a/apps/web/src/components/dialogs/import-dialog.tsx b/apps/web/src/components/dialogs/import-dialog.tsx
index f0cc404a8..b7aa4ad49 100644
--- a/apps/web/src/components/dialogs/import-dialog.tsx
+++ b/apps/web/src/components/dialogs/import-dialog.tsx
@@ -68,13 +68,13 @@ function ImportDialog(props: ImportDialogProps) {
? undefined
: {
text: "Cancel",
- onClick: props.onClose
+ onClick: () => props.onClose(false)
}
}
positiveButton={
isDone
? {
- onClick: props.onClose,
+ onClick: () => props.onClose(true),
text: "Close"
}
: {
diff --git a/apps/web/src/components/dialogs/issue-dialog.tsx b/apps/web/src/components/dialogs/issue-dialog.tsx
index d66459aae..7eddd259f 100644
--- a/apps/web/src/components/dialogs/issue-dialog.tsx
+++ b/apps/web/src/components/dialogs/issue-dialog.tsx
@@ -52,16 +52,14 @@ function IssueDialog(props: IssueDialogProps) {
<Dialog
isOpen={true}
title={"Report an issue"}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: "Submit",
- props: {
- form: "issueForm"
- },
+ form: "issueForm",
loading: isSubmitting,
disabled: isSubmitting
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Flex
id="issueForm"
diff --git a/apps/web/src/components/dialogs/language-selector-dialog.tsx b/apps/web/src/components/dialogs/language-selector-dialog.tsx
index 772b51efb..8be9bf14e 100644
--- a/apps/web/src/components/dialogs/language-selector-dialog.tsx
+++ b/apps/web/src/components/dialogs/language-selector-dialog.tsx
@@ -63,12 +63,12 @@ export default function LanguageSelectorDialog(
description={
"Choose the languages you want to spell check while editing."
}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: "Done",
- onClick: props.onClose
+ onClick: () => props.onClose(true)
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Input
placeholder="Filter languages"
diff --git a/apps/web/src/components/dialogs/mfa/multi-factor-dialog.tsx b/apps/web/src/components/dialogs/mfa/multi-factor-dialog.tsx
index 55f7a8db2..8eac13469 100644
--- a/apps/web/src/components/dialogs/mfa/multi-factor-dialog.tsx
+++ b/apps/web/src/components/dialogs/mfa/multi-factor-dialog.tsx
@@ -56,7 +56,7 @@ export default function MultifactorDialog(props: MultifactorDialogProps) {
step.next
? {
text: "Continue",
- props: { form: "2faForm" }
+ form: "2faForm"
}
: null
}
@@ -64,7 +64,7 @@ export default function MultifactorDialog(props: MultifactorDialogProps) {
step.cancellable
? {
text: "Cancel",
- onClick: onClose
+ onClick: () => props.onClose(false)
}
: null
}
diff --git a/apps/web/src/components/dialogs/mfa/recovery-code-dialog.tsx b/apps/web/src/components/dialogs/mfa/recovery-code-dialog.tsx
index 7dabb5aaa..9da5a1aa7 100644
--- a/apps/web/src/components/dialogs/mfa/recovery-code-dialog.tsx
+++ b/apps/web/src/components/dialogs/mfa/recovery-code-dialog.tsx
@@ -42,7 +42,7 @@ export default function RecoveryCodesDialog(props: RecoveryCodesDialogProps) {
width={500}
positiveButton={{
text: "Okay",
- onClick: onClose
+ onClick: () => onClose(true)
}}
>
{step.component && (
diff --git a/apps/web/src/components/dialogs/prompt.tsx b/apps/web/src/components/dialogs/prompt.tsx
index 1c56ff2d2..7c2e509df 100644
--- a/apps/web/src/components/dialogs/prompt.tsx
+++ b/apps/web/src/components/dialogs/prompt.tsx
@@ -37,12 +37,12 @@ export default function Prompt(props: PromptDialogProps) {
isOpen={true}
title={props.title}
description={props.description}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: "Done",
onClick: () => props.onSave(inputRef.current?.value || "")
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Field inputRef={inputRef} defaultValue={props.defaultValue} autoFocus />
</Dialog>
diff --git a/apps/web/src/components/dialogs/reminder-dialog.js b/apps/web/src/components/dialogs/reminder-dialog.js
index d15fd93e5..ca7f5f2f0 100644
--- a/apps/web/src/components/dialogs/reminder-dialog.js
+++ b/apps/web/src/components/dialogs/reminder-dialog.js
@@ -99,7 +99,7 @@ function ReminderDialog(props) {
isOpen={true}
title={reminder.title}
onClose={props.onClose}
- showClose
+ showCloseButton
description={reminder.description}
alignment="center"
positiveButton={{
diff --git a/apps/web/src/components/dialogs/reminder-preview-dialog.tsx b/apps/web/src/components/dialogs/reminder-preview-dialog.tsx
index bf17c8e64..88597509a 100644
--- a/apps/web/src/components/dialogs/reminder-preview-dialog.tsx
+++ b/apps/web/src/components/dialogs/reminder-preview-dialog.tsx
@@ -69,10 +69,10 @@ export default function ReminderPreviewDialog(
isOpen={true}
title={reminder.title}
description={reminder.description}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
negativeButton={{
text: "Close",
- onClick: props.onClose
+ onClick: () => props.onClose(false)
}}
>
<Flex
diff --git a/apps/web/src/components/dialogs/toolbar-config-dialog.tsx b/apps/web/src/components/dialogs/toolbar-config-dialog.tsx
index 23732f8e8..cd65cd205 100644
--- a/apps/web/src/components/dialogs/toolbar-config-dialog.tsx
+++ b/apps/web/src/components/dialogs/toolbar-config-dialog.tsx
@@ -95,7 +95,7 @@ export default function ToolbarConfigDialog(props: ToolbarConfigDialogProps) {
title={"Configure toolbar"}
description={"Customize the editor toolbar to fit your needs."}
width={500}
- onClose={props.onClose}
+ onClose={() => props.onClose(false)}
positiveButton={{
text: "Save",
onClick: async () => {
@@ -110,7 +110,7 @@ export default function ToolbarConfigDialog(props: ToolbarConfigDialogProps) {
props.onClose(true);
}
}}
- negativeButton={{ text: "Cancel", onClick: props.onClose }}
+ negativeButton={{ text: "Cancel", onClick: () => props.onClose(false) }}
>
<Flex sx={{ flexDirection: "column" }}>
<Flex