-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathmap-control.tsx
67 lines (58 loc) · 1.65 KB
/
map-control.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {useEffect, useMemo} from 'react';
import {createPortal} from 'react-dom';
import {useMap} from '../hooks/use-map';
import type {PropsWithChildren} from 'react';
type MapControlProps = PropsWithChildren<{
position: ControlPosition;
}>;
/**
* Copy of the `google.maps.ControlPosition` constants.
* They have to be duplicated here since we can't wait for the maps API to load to be able to use them.
*/
export const ControlPosition = {
TOP_LEFT: 1,
TOP_CENTER: 2,
TOP: 2,
TOP_RIGHT: 3,
LEFT_CENTER: 4,
LEFT_TOP: 5,
LEFT: 5,
LEFT_BOTTOM: 6,
RIGHT_TOP: 7,
RIGHT: 7,
RIGHT_CENTER: 8,
RIGHT_BOTTOM: 9,
BOTTOM_LEFT: 10,
BOTTOM_CENTER: 11,
BOTTOM: 11,
BOTTOM_RIGHT: 12,
CENTER: 13,
BLOCK_START_INLINE_START: 14,
BLOCK_START_INLINE_CENTER: 15,
BLOCK_START_INLINE_END: 16,
INLINE_START_BLOCK_CENTER: 17,
INLINE_START_BLOCK_START: 18,
INLINE_START_BLOCK_END: 19,
INLINE_END_BLOCK_START: 20,
INLINE_END_BLOCK_CENTER: 21,
INLINE_END_BLOCK_END: 22,
BLOCK_END_INLINE_START: 23,
BLOCK_END_INLINE_CENTER: 24,
BLOCK_END_INLINE_END: 25
} as const;
export type ControlPosition =
(typeof ControlPosition)[keyof typeof ControlPosition];
export const MapControl = ({children, position}: MapControlProps) => {
const controlContainer = useMemo(() => document.createElement('div'), []);
const map = useMap();
useEffect(() => {
if (!map) return;
const controls = map.controls[position];
controls.push(controlContainer);
return () => {
const index = controls.getArray().indexOf(controlContainer);
controls.removeAt(index);
};
}, [map, position]);
return createPortal(children, controlContainer);
};