diff --git a/src/components/app-registration/AppRegistration/AppRegistrationForm/RegisterAppDialogError/RegisterAppDialogError.tsx b/src/components/app-registration/AppRegistration/AppRegistrationForm/RegisterAppDialogError/RegisterAppDialogError.tsx index 2d9990a4..0b65bb16 100644 --- a/src/components/app-registration/AppRegistration/AppRegistrationForm/RegisterAppDialogError/RegisterAppDialogError.tsx +++ b/src/components/app-registration/AppRegistration/AppRegistrationForm/RegisterAppDialogError/RegisterAppDialogError.tsx @@ -7,7 +7,7 @@ import DelayedFallback from '../../../../global/DelayedFallback/DelayedFallback' const Modal = lazy(() => import('../../../../global/Modal/Modal')); export default function RegisterAppDialogError({ error }) { - const isModalOpen = useSelector(stateService, isRegisterErrorSelector); + const isModalOpen: boolean = useSelector(stateService, isRegisterErrorSelector); if (!isModalOpen) { return null; } diff --git a/src/components/app-registration/AppRegistration/RegisterAppDialogSuccess/RegisterAppDialogSuccess.tsx b/src/components/app-registration/AppRegistration/RegisterAppDialogSuccess/RegisterAppDialogSuccess.tsx index d7708a46..1257b9a2 100644 --- a/src/components/app-registration/AppRegistration/RegisterAppDialogSuccess/RegisterAppDialogSuccess.tsx +++ b/src/components/app-registration/AppRegistration/RegisterAppDialogSuccess/RegisterAppDialogSuccess.tsx @@ -6,7 +6,7 @@ import { stateService } from '../../../../state/stateSignal'; const Modal = lazy(() => import('../../../global/Modal/Modal')); export default function RegisterAppDialogSuccess() { - const isModalOpen = useSelector(stateService, isRegisterSuccessSelector); + const isModalOpen: boolean = useSelector(stateService, isRegisterSuccessSelector); const isUpdateMode = useSelector(stateService, isUpdateModeSelector); const description = isUpdateMode ? 'Your app has been updated successfully.' diff --git a/src/components/global/Dialog/Dialog.jsx b/src/components/global/Dialog/Dialog.jsx deleted file mode 100644 index 1e9a99b5..00000000 --- a/src/components/global/Dialog/Dialog.jsx +++ /dev/null @@ -1,53 +0,0 @@ -import React, { useEffect, useRef } from 'react'; - -import { useDialogPolyfill } from './useDialogPolyfill'; -import styles from './Dialog.module.scss'; - -export default function Dialog({ closeOnOutsideClick, onRequestClose, open, ...props }) { - const dialogRef = useRef(); - - useDialogPolyfill(dialogRef); - useDialogOpening(dialogRef, open); - useDialogClosing(dialogRef, onRequestClose); - - function handleOutsideClick(event) { - const dialogNode = dialogRef.current; - if (closeOnOutsideClick && event.target === dialogNode) { - onRequestClose(); - } - } - - return ( - - - - ); -} - -const useDialogOpening = (dialogRef, open) => { - const lastActiveElement = useRef(null); - useEffect(() => { - const dialogNode = dialogRef.current; - if (open) { - lastActiveElement.current = document.activeElement; - dialogNode.showModal(); - } else { - dialogNode.close(); - lastActiveElement.current.focus(); - } - }, [open]); -}; - -const useDialogClosing = (dialogRef, onRequestClose) => { - useEffect(() => { - const dialogNode = dialogRef.current; - const handleCancel = event => { - event.preventDefault(); - onRequestClose(); - }; - dialogNode.addEventListener('cancel', handleCancel); - return () => { - dialogNode.removeEventListener('cancel', handleCancel); - }; - }, [onRequestClose]); -}; diff --git a/src/components/global/Dialog/Dialog.tsx b/src/components/global/Dialog/Dialog.tsx new file mode 100644 index 00000000..4f730373 --- /dev/null +++ b/src/components/global/Dialog/Dialog.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import styles from './Dialog.module.scss'; + +type TDialog = { + closeOnOutsideClick: boolean; + onRequestClose: () => void; + open: boolean; + children: React.ReactNode[] | HTMLElement[]; +}; + +type TDialogRef = React.RefObject; + +export const Dialog = ({ closeOnOutsideClick, onRequestClose, open, ...children }: TDialog) => { + const dialog_ref = React.useRef(null); + + useDialogOpening(dialog_ref, open); + useDialogClosing(dialog_ref, onRequestClose); + + const handleOutsideClick = (event: React.MouseEvent) => { + const dialog_node = dialog_ref?.current; + if (closeOnOutsideClick && event?.target === dialog_node) { + onRequestClose(); + } + }; + + return ( + + + + ); +}; + +const useDialogOpening = (dialog_ref: TDialogRef, open: boolean) => { + const lastActiveElement = React.useRef(null); + React.useEffect(() => { + const dialog_node = dialog_ref?.current; + if (open) { + dialog_node?.showModal(); + } else { + dialog_node?.close(); + lastActiveElement?.current?.focus(); + } + }, [open]); +}; + +const useDialogClosing = (dialog_ref: TDialogRef, onRequestClose: () => void) => { + React.useEffect(() => { + const dialog_node = dialog_ref?.current; + const handleCancel: EventListener = event => { + event.preventDefault(); + onRequestClose(); + }; + dialog_node?.addEventListener('cancel', handleCancel); + return () => { + dialog_node?.removeEventListener('cancel', handleCancel); + }; + }, [onRequestClose]); +}; diff --git a/src/components/global/Modal/Modal.tsx b/src/components/global/Modal/Modal.tsx index b7479e27..db94a7a7 100644 --- a/src/components/global/Modal/Modal.tsx +++ b/src/components/global/Modal/Modal.tsx @@ -1,7 +1,19 @@ import Button from '../Button/Button'; -import Dialog from '../Dialog/Dialog'; +import { Dialog } from '../Dialog/Dialog'; import styles from './Modal.module.scss'; +type TModal = { + onRequestClose: () => void; + open: boolean; + type: 'success' | 'warning'; + title: string; + description: string; + primaryButtonText?: string | null; + secondaryButtonText: string; + onPrimaryButtonClick?: () => void; + onSecondaryButtonClick: () => void; +}; + export default function Modal({ onRequestClose, open, @@ -12,17 +24,7 @@ export default function Modal({ secondaryButtonText, onPrimaryButtonClick, onSecondaryButtonClick, -}: { - onRequestClose: () => void; - open: any; - type: 'success' | 'warning'; - title: string; - description: string; - primaryButtonText?: any; - secondaryButtonText: string; - onPrimaryButtonClick?: () => void; - onSecondaryButtonClick: () => void; -}) { +}: TModal) { return ( @@ -32,9 +34,7 @@ export default function Modal({ {type === 'success' && } {type === 'warning' && } {title} - - {description} - + {description} diff --git a/src/index.d.ts b/src/index.d.ts index b8bae3ed..aa643c6a 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1 +1,3 @@ -declare module '*.png'; \ No newline at end of file +declare module '*.png'; +declare module '*.css'; +declare module '*.scss'; \ No newline at end of file