Skip to content

Commit 9c7ebd0

Browse files
authored
feat: MapOptions close by clicking outside component on mobile (#420)
1 parent 8cfeb91 commit 9c7ebd0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/map/MapOptions.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react'
1+
import { useState, useEffect, useRef } from 'react'
22
import styles from './MapOptions.module.css'
33
import { MapOptionsStoreState } from '@/stores/MapOptionsStore'
44
import Dispatcher from '@/stores/Dispatcher'
@@ -9,8 +9,22 @@ import * as config from 'config'
99

1010
export default function (props: MapOptionsStoreState) {
1111
const [isOpen, setIsOpen] = useState(false)
12+
const containerRef = useRef<HTMLDivElement>(null)
13+
useEffect(() => {
14+
function handleClickOutside(event: MouseEvent) {
15+
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
16+
setIsOpen(false)
17+
}
18+
}
19+
20+
document.addEventListener('mousedown', handleClickOutside)
21+
return () => {
22+
document.removeEventListener('mousedown', handleClickOutside)
23+
}
24+
}, [])
1225
return (
1326
<div
27+
ref={containerRef}
1428
className={styles.mapOptionsContainer}
1529
onMouseEnter={() => setIsOpen(true)}
1630
onMouseLeave={() => setIsOpen(false)}

0 commit comments

Comments
 (0)