44 lines
1.2 KiB
TypeScript
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>
|
|
)
|
|
}
|