From aef478ffdf54d2511ec64f4353757741fe5f7734 Mon Sep 17 00:00:00 2001 From: Dobromir Yordanov <32433021+dobromiryor@users.noreply.github.com> Date: Wed, 21 Aug 2024 15:18:14 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=92=84=20fixed=20discrete=20transi?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/atoms/Modal.tsx | 206 ++++++++++++++++----------------- tailwind.config.ts | 102 ++++++++-------- 2 files changed, 158 insertions(+), 150 deletions(-) diff --git a/src/components/atoms/Modal.tsx b/src/components/atoms/Modal.tsx index 7d2e5de..d04087b 100644 --- a/src/components/atoms/Modal.tsx +++ b/src/components/atoms/Modal.tsx @@ -13,115 +13,113 @@ import { useModal } from "../../hooks/useModal"; import { Icon } from "./Icon"; interface ModalProps { - title: string; - children: ReactNode; - wrapperId?: string; + title: string; + children: ReactNode; + wrapperId?: string; } const createWrapperAndAppendToBody = (wrapperId: string) => { - const wrapperElement = document.createElement("div"); - wrapperElement.setAttribute("id", wrapperId); - document.body.appendChild(wrapperElement); - return wrapperElement; + const wrapperElement = document.createElement("div"); + wrapperElement.setAttribute("id", wrapperId); + document.body.appendChild(wrapperElement); + return wrapperElement; }; export const Modal = ({ - title, - children, - wrapperId = "portal-root", + title, + children, + wrapperId = "portal-root", }: ModalProps) => { - const [wrapperElement, setWrapperElement] = useState( - null - ); - - const modalRef = useRef(null); - - const [isOpen, setIsOpen] = useModal(); - - useLayoutEffect(() => { - let element = document.getElementById(wrapperId); - let systemCreated = false; - - if (!element) { - systemCreated = true; - element = createWrapperAndAppendToBody(wrapperId); - } - - setWrapperElement(element); - - return () => { - if (systemCreated && element.parentNode) { - element.parentNode.removeChild(element); - } - }; - }, [wrapperId]); - - const handleDismiss = useCallback(() => setIsOpen(false), [setIsOpen]); - const handleStopPropagation = (e: MouseEvent) => { - e.stopPropagation(); - return false; - }; - - useEffect(() => { - if (isOpen) { - modalRef.current?.focus(); - } - }, [isOpen]); - - useEffect(() => { - if (isOpen) { - const handleKeypress = (e: KeyboardEvent) => { - if (e.key === "Escape") { - handleDismiss(); - } - }; - - document.addEventListener("keyup", handleKeypress); - - return () => document.removeEventListener("keyup", handleKeypress); - } - }, [handleDismiss, isOpen]); - - if (wrapperElement === null) return null; - - return createPortal( -
-
-
-

- {title} -

- -
- {children} -
-
, - wrapperElement - ); + const [wrapperElement, setWrapperElement] = useState( + null + ); + + const modalRef = useRef(null); + + const [isOpen, setIsOpen] = useModal(); + + useLayoutEffect(() => { + let element = document.getElementById(wrapperId); + let systemCreated = false; + + if (!element) { + systemCreated = true; + element = createWrapperAndAppendToBody(wrapperId); + } + + setWrapperElement(element); + + return () => { + if (systemCreated && element.parentNode) { + element.parentNode.removeChild(element); + } + }; + }, [wrapperId]); + + const handleDismiss = useCallback(() => setIsOpen(false), [setIsOpen]); + const handleStopPropagation = (e: MouseEvent) => { + e.stopPropagation(); + return false; + }; + + useEffect(() => { + if (isOpen) { + modalRef.current?.focus(); + } + }, [isOpen]); + + useEffect(() => { + if (isOpen) { + const handleKeypress = (e: KeyboardEvent) => { + if (e.key === "Escape") { + handleDismiss(); + } + }; + + document.addEventListener("keyup", handleKeypress); + + return () => document.removeEventListener("keyup", handleKeypress); + } + }, [handleDismiss, isOpen]); + + if (wrapperElement === null) return null; + + return createPortal( +
+
+
+

+ {title} +

+ +
+ {children} +
+
, + wrapperElement + ); }; diff --git a/tailwind.config.ts b/tailwind.config.ts index a70d48d..5560ad3 100644 --- a/tailwind.config.ts +++ b/tailwind.config.ts @@ -2,51 +2,61 @@ import type { Config } from "tailwindcss"; import plugin from "tailwindcss/plugin"; export default { - content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"], - theme: { - extend: { - boxShadow: { - "center-inset-2": "0 0 2px 0 var(--tw-shadow-colored) inset", - }, - screens: { - xs: "360px", - }, - colors: { - text: "rgb(var(--text) / )", - "inverted-text": "rgb(var(--inverted-text) / )", - background: "rgb(var(--background) / )", - primary: "rgb(var(--primary) / )", - secondary: "rgb(var(--secondary) / )", - accent: "rgb(var(--accent) / )", + content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"], + theme: { + extend: { + boxShadow: { + "center-inset-2": "0 0 2px 0 var(--tw-shadow-colored) inset", + }, + screens: { + xs: "360px", + }, + colors: { + text: "rgb(var(--text) / )", + "inverted-text": "rgb(var(--inverted-text) / )", + background: "rgb(var(--background) / )", + primary: "rgb(var(--primary) / )", + secondary: "rgb(var(--secondary) / )", + accent: "rgb(var(--accent) / )", - slate: { - 150: "#EAEFF5", - 250: "#D7DFE9", - 350: "#B0BCCD", - 450: "#7C8CA2", - 550: "#56657A", - 650: "#3D4B5F", - 750: "#293548", - 850: "#172033", - 950: "#090F21", - }, - }, - fontFamily: { - sans: ["Noto Sans", "sans-serif"], - }, - }, - }, - darkMode: "class", - plugins: [ - plugin(function ({ addUtilities }) { - addUtilities({ - ".scrollbar-none": { - "scrollbar-width": "none", - "&::-webkit-scrollbar": { - display: "none", - }, - }, - }); - }), - ], + slate: { + 150: "#EAEFF5", + 250: "#D7DFE9", + 350: "#B0BCCD", + 450: "#7C8CA2", + 550: "#56657A", + 650: "#3D4B5F", + 750: "#293548", + 850: "#172033", + 950: "#090F21", + }, + }, + fontFamily: { + sans: ["Noto Sans", "sans-serif"], + }, + }, + }, + darkMode: "class", + plugins: [ + plugin(function ({ addUtilities }) { + addUtilities({ + ".scrollbar-none": { + "scrollbar-width": "none", + "&::-webkit-scrollbar": { + display: "none", + }, + }, + }); + }), + plugin(({ addComponents }) => { + addComponents({ + ".transition-allow-discrete": { + "transition-behavior": "allow-discrete", + }, + }); + }), + ({ addVariant }) => { + addVariant("starting", "@starting-style"); + }, + ], } satisfies Config;