-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.tsx
64 lines (56 loc) · 1.66 KB
/
index.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
import { onCleanup, createEffect, Component, splitProps } from 'solid-js'
import { useMapContext } from '../MapProvider'
import type { Popup as PopupType, PopupOptions, LngLatLike } from 'mapbox-gl'
type Props = {
/** Options for configuring the popup */
options?: PopupOptions
/** Flag for tracking the mouse pointer */
trackPointer?: boolean
/** Longitude and latitude for the popup */
lngLat?: LngLatLike
/** Callback for when the popup is opened */
onOpen?: () => void
/** Callback for when the popup is closed */
onClose?: () => void
/** Children to display within the popup */
children?: any
}
export const Popup: Component<Props> = (props: Props) => {
if (!props.trackPointer && !props.lngLat)
throw new Error('Popup - lngLat or trackPointer is required')
const [update, create] = splitProps(props, [
'lngLat',
'children',
'trackPointer',
])
const [ctx] = useMapContext()
let popup: PopupType | undefined
// Update Popup
createEffect(() => {
if (!ctx.map) return
popup?.remove()
popup = new window.MAPLIB.Popup({
closeOnClick: false,
focusAfterOpen: false,
...create.options,
})
.on('open', () => create.onOpen?.())
.on('close', () => create.onClose?.())
.addTo(ctx.map)
// Update Position
createEffect(() =>
update.trackPointer
? popup.trackPointer()
: popup.setLngLat(update.lngLat)
)
// Update Content
createEffect(() =>
typeof update.children === 'string'
? popup.setHTML(update.children)
: popup.setDOMContent(update.children)
)
})
// Remove Popup
onCleanup(() => popup?.remove())
return null
}