import { Box, Container, Flex, Loader, useComputedColorScheme } from "@mantine/core"; import { PropsWithChildren, Suspense, useEffect, useRef } from "react"; import { Drawer as VaulDrawer } from "vaul"; import styles from "./styles.module.css"; interface DrawerProps extends PropsWithChildren { title?: string; opened: boolean; onChange: (next: boolean) => void; } const Drawer: React.FC = ({ title, children, opened, onChange, }) => { const colorScheme = useComputedColorScheme("light"); const contentRef = useRef(null); useEffect(() => { const appElement = document.querySelector(".app") as HTMLElement; if (!appElement) return; let themeColorMeta = document.querySelector( 'meta[name="theme-color"]' ) as HTMLMetaElement; if (!themeColorMeta) { themeColorMeta = document.createElement("meta"); themeColorMeta.name = "theme-color"; document.head.appendChild(themeColorMeta); } const colors = { light: { normal: "rgb(255,255,255)", overlay: "rgb(153,153,153)", }, dark: { normal: "rgb(36,36,36)", overlay: "rgb(22,22,22)", }, }; const currentColors = colors[colorScheme] || colors.light; if (opened) { appElement.classList.add("drawer-scaling"); themeColorMeta.content = currentColors.overlay; } else { appElement.classList.remove("drawer-scaling"); themeColorMeta.content = currentColors.normal; } return () => { appElement.classList.remove("drawer-scaling"); themeColorMeta.content = currentColors.normal; }; }, [opened, colorScheme]); useEffect(() => { if (!opened || !contentRef.current) return; const updateDrawerHeight = () => { if (contentRef.current) { const drawerContent = contentRef.current; const visualViewport = window.visualViewport; if (visualViewport) { const availableHeight = visualViewport.height; const maxDrawerHeight = Math.min(availableHeight * 0.75, window.innerHeight * 0.75); drawerContent.style.maxHeight = `${maxDrawerHeight}px`; } else { drawerContent.style.maxHeight = '75vh'; } } }; const resizeObserver = new ResizeObserver(() => { if (contentRef.current) { const drawerContent = contentRef.current.closest('[data-vaul-drawer-wrapper]'); if (drawerContent) { (drawerContent as HTMLElement).style.height = 'auto'; (drawerContent as HTMLElement).offsetHeight; } } }); updateDrawerHeight(); if (window.visualViewport) { window.visualViewport.addEventListener('resize', updateDrawerHeight); } resizeObserver.observe(contentRef.current); return () => { resizeObserver.disconnect(); if (window.visualViewport) { window.visualViewport.removeEventListener('resize', updateDrawerHeight); } }; }, [opened, children]); return ( {title} }> {children} ); }; export default Drawer;