Files
web/components/MapModal/index.tsx
2024-11-06 08:52:21 +01:00

44 lines
1.2 KiB
TypeScript

"use client"
import { useRouter } from "next/navigation"
import { useEffect, useRef, useState } from "react"
import { Dialog, Modal } from "react-aria-components"
import styles from "./mapModal.module.css"
export function MapModal({ children }: { children: React.ReactNode }) {
const router = useRouter()
const [scrollHeightWhenOpened, setScrollHeightWhenOpened] = useState(0)
const hasMounted = useRef(false)
function onDismiss() {
router.back()
}
// Making sure the map is always opened at the top of the page, just below the header.
// When closing, the page should scroll back to the position it was before opening the map.
useEffect(() => {
// Skip the first render
if (!hasMounted.current) {
hasMounted.current = true
return
}
if (scrollHeightWhenOpened === 0) {
setScrollHeightWhenOpened(window.scrollY)
window.scrollTo({ top: 0, behavior: "instant" })
}
}, [scrollHeightWhenOpened])
return (
<Modal isOpen={true}>
<Dialog className={styles.dynamicMap}>
{children}
<button onClick={onDismiss} className="close-button">
close
</button>
</Dialog>
</Modal>
)
}