Skip to content

Commit 2e38b7a

Browse files
committed
fix event dispatching
1 parent 9d583b8 commit 2e38b7a

File tree

8 files changed

+105
-134
lines changed

8 files changed

+105
-134
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ npm install --save-dev @beyonk/svelte-mapbox
3030

3131
The container component is the map, and there are a variety of components which go on the map.
3232

33-
```jsx
33+
```svelte
3434
<Map
3535
accessToken="<your api key>" // add your api key here
3636
bind:this={mapComponent} // Get reference to your map component to use methods
37-
on:recentre={e => console.log(e.detail.center.lat, e.detail.center.lng) } // recentre events
37+
onrecentre={e => console.log(e.detail.center.lat, e.detail.center.lng) } // recentre events
3838
options={{ scrollZoom: false }} // // add arbitrary options to the map from the mapbox api
3939
>
4040
<Earthquakes /> // Any custom component you create or want here - see marker example
4141
<Marker lat={someLat} lng={someLng} color="rgb(255,255,255)" label="some marker label" popupClassName="class-name" /> // built in Marker component
4242
<NavigationControl />
43-
<GeolocateControl options={{ some: 'control-option' }} on:eventname={eventHandler} />
43+
<GeolocateControl options={{ some: 'control-option' }} oneventname={eventHandler} />
4444
<ScaleControl />
4545
</Map>
4646
@@ -73,13 +73,13 @@ The container component is the map, and there are a variety of components which
7373

7474
By default, markers are typical map pins to which you can pass a color property.
7575

76-
```jsx
76+
```svelte
7777
<Marker color={brandColour} />
7878
```
7979

8080
You may also create a custom pin with the default slot.
8181

82-
```jsx
82+
```svelte
8383
<Marker
8484
lat={waypoint.geo.lat}
8585
lng={waypoint.geo.lng}
@@ -98,13 +98,13 @@ lng={waypoint.geo.lng}
9898
### Marker Popups
9999
By default a popup is revealed when you click a pin. It is populated with text provided in the label property.
100100

101-
```jsx
101+
```svelte
102102
<Marker label={markerText} />
103103
```
104104

105105
To indicate interactivity, you may target the marker with some custom CSS:
106106

107-
```jsx
107+
```svelte
108108
<style>
109109
:global(.mapboxgl-marker){
110110
cursor: pointer;
@@ -114,13 +114,13 @@ To indicate interactivity, you may target the marker with some custom CSS:
114114

115115
Optionally, disable the popup with the `popup={false}` property:
116116

117-
```jsx
117+
```svelte
118118
<Marker popup={false} />
119119
```
120120

121121
You may alternatively pass a custom DOM element to the marker to use as a popup.
122122

123-
```jsx
123+
```svelte
124124
<Marker lat={pin.coordinates.latitude} lng={pin.coordinates.longitude}>
125125
<div class="content" slot="popup">
126126
<h3>{pin.name}</h3>
@@ -139,7 +139,7 @@ This also means that if you bind these properties to a variable, that variable w
139139

140140
This is often easier than waiting for events such as `recentre` or `zoom` to be fired, to update markers and similar:
141141

142-
```jsx
142+
```svelte
143143
<Map accessToken="<your api key>" bind:center bind:zoom>
144144
<Marker bind:lat bind:lng />
145145
</Map>
@@ -165,19 +165,19 @@ map is ready in your browser when you call it this way.
165165

166166
The Geocoder is an autocompleting place lookup, which returns a lat and lng for a place.
167167

168-
```jsx
169-
<Geocoder accessToken="<your api key>" on:result={somePlaceChangeFunction} />
168+
```svelte
169+
<Geocoder accessToken="<your api key>" onresult={somePlaceChangeFunction} />
170170
171171
<script>
172172
import { Geocoder } from '@beyonk/svelte-mapbox'
173173
</script>
174174
```
175175

176-
The geocoder has five events you can subscribe to: `on:loading`, `on:result`, `on:results`, `on:clear`, and `on:error` which are [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on)
176+
The geocoder has five events you can subscribe to: `onloading`, `onresult`, `onresults`, `onclear`, and `onerror` which are [documented here](https://github.com/mapbox/mapbox-gl-geocoder/blob/master/API.md#on)
177177

178-
The most important event is `on:result` which is fired when a user selects an autocomplete result.
178+
The most important event is `onresult` which is fired when a user selects an autocomplete result.
179179

180-
There is a sixth event specific to this library, which is `on:ready`, which is fired when the component is ready for use. You can likely ignore it.
180+
There is a sixth event specific to this library, which is `onready`, which is fired when the component is ready for use. You can likely ignore it.
181181

182182
## Custom CSS
183183

src/lib/event-bindings.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/lib/geocoder/Geocoder.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
value = null,
1111
customStylesheetUrl = false,
1212
geocoder = $bindable(),
13+
onresults,
14+
onresult,
15+
onloading,
16+
onerror,
17+
onclear,
18+
onload,
1319
...rest
1420
} = $props()
1521
@@ -24,15 +30,14 @@
2430
value
2531
}, options))
2632
27-
function init (e) {
28-
geocoder = e.detail.geocoder
29-
onready?.()
33+
function init (detail) {
34+
geocoder = detail.geocoder
3035
}
3136
</script>
3237

3338
<div
3439
id={fieldId}
35-
{@attach geocoderAttachment(optionsWithDefaults)}
40+
{@attach geocoderAttachment(optionsWithDefaults, { onresults, onresult, onloading, onerror, onclear, onload })}
3641
onready={init}
3742
{...rest}
3843
></div>
Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { load } from '../asset-loader.js'
2-
import { bindEvents } from '../event-bindings.js'
32

4-
export default function geocoderAttachment (options) {
3+
export default function geocoderAttachment (options, { onresults, onresult, onloading, onerror, onclear, onready }) {
54
return (element) => {
65
let geocoderInstance
7-
let unbind = () => {}
86

97
const resources = [
108
{ type: 'script', value: `//api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/${options.version}/mapbox-gl-geocoder.min.js`, id: 'byk-gc-js' }
@@ -23,33 +21,30 @@ export default function geocoderAttachment (options) {
2321
if (options.value) {
2422
geocoderInstance.setInput(options.value)
2523
}
26-
unbind = bindEvents(geocoderInstance, handlers, false, element)
24+
25+
geocoderInstance.on('results', (ev) => {
26+
onresults?.(ev)
27+
})
28+
geocoderInstance.on('result', (ev) => {
29+
console.log('result', onresult, ev)
30+
onresult?.(ev)
31+
})
32+
geocoderInstance.on('loading', (ev) => {
33+
onloading?.(ev)
34+
})
35+
geocoderInstance.on('error', (ev) => {
36+
onerror?.(ev)
37+
})
38+
geocoderInstance.on('clear', (ev) => {
39+
onclear?.(ev)
40+
})
41+
geocoderInstance.on('load', (ev) => {
42+
onready?.({ ...ev, geocoder: geocoderInstance })
43+
})
2744
})
2845

2946
return () => {
30-
unbind()
3147
geocoderInstance && geocoderInstance.remove && geocoderInstance.remove()
3248
}
3349
}
3450
}
35-
36-
const handlers = {
37-
results: (el, ev) => {
38-
return [ 'results', ev ]
39-
},
40-
result: (el, ev) => {
41-
return [ 'result', ev ]
42-
},
43-
loading: (el, ev) => {
44-
return [ 'loading', ev ]
45-
},
46-
error: (el, ev) => {
47-
return [ 'error', ev ]
48-
},
49-
clear: (el, ev) => {
50-
return [ 'clear', ev ]
51-
},
52-
load: el => {
53-
return [ 'ready', { geocoder: el } ]
54-
}
55-
}

src/lib/map/Map.svelte

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import { EventQueue } from '../queue.svelte.js'
66
77
let {
8-
map = $bindable(null),
98
version = 'v3.20.0',
109
center = [ 0, 0 ],
1110
zoom = $bindable(9),
@@ -17,13 +16,24 @@
1716
style = 'mapbox://styles/mapbox/streets-v11',
1817
children,
1918
onready,
19+
ondragend,
20+
ondrag,
21+
onmoveend,
22+
onzoomstart,
23+
onzoom,
24+
onzoomend,
2025
...rest
2126
} = $props()
2227
28+
let map = $state()
2329
let mapbox = $state()
2430
31+
setContext(contextKey, {
32+
getMap: () => map,
33+
getMapbox: () => mapbox
34+
})
2535
26-
const optionsWithDefaults = {
36+
const optionsWithDefaults = $derived.by(() => Object.assign({
2737
accessToken,
2838
style,
2939
center,
@@ -32,26 +42,21 @@
3242
wheelZoomRate,
3343
version,
3444
customStylesheetUrl,
35-
map,
3645
...options
37-
}
38-
39-
setContext(contextKey, {
40-
getMap: () => map,
41-
getMapbox: () => mapbox
42-
})
46+
}))
4347
4448
const queue = new EventQueue()
4549
46-
function init (e) {
47-
map = e.detail.map
48-
mapbox = e.detail.mapbox
50+
function init (detail) {
51+
map = detail.map
52+
mapbox = detail.mapbox
4953
queue.start(map)
50-
onready?.()
5154
5255
map.on('zoomend', (e) => {
5356
zoom = map.getZoom()
5457
})
58+
59+
onready?.({ map, mapbox })
5560
}
5661
5762
$effect(() => {
@@ -99,8 +104,10 @@
99104
</script>
100105
101106
<div
102-
{@attach mapAttachment(optionsWithDefaults)}
103-
onready={init}
107+
{@attach mapAttachment(
108+
optionsWithDefaults,
109+
{ onready: init, ondragend, ondrag, onmoveend, onzoomstart, onzoom, onzoomend }
110+
)}
104111
{...rest}
105112
role="presentation"
106113
>

src/lib/map/controls/GeolocateControl.svelte

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
<script>
22
import { getContext, onMount, untrack } from 'svelte'
33
import { contextKey } from '../../mapbox.js'
4-
import { bindEvents } from '../../event-bindings.js'
54
65
const { getMap, getMapbox } = getContext(contextKey)
76
const map = getMap()
87
const mapbox = getMapbox()
98
10-
let { position = 'top-left', options = {}, ...rest } = $props()
9+
let { position = 'top-left', options = {}, onerror, ongeolocate, onoutofmaxbounds, ontrackuserlocationend, ontrackuserlocationstart, ...rest } = $props()
1110
1211
let dispatcher = $state()
1312
14-
const handlers = {
15-
error: (el, ev) => [ 'error', ev ],
16-
geolocate: (el, ev) => [ 'geolocate', ev ],
17-
outofmaxbounds: (el, ev) => [ 'outofmaxbounds', ev ],
18-
trackuserlocationend: (el, ev) => [ 'trackuserlocationend', ev ],
19-
trackuserlocationstart: (el, ev) => [ 'trackuserlocationstart', ev ]
20-
}
21-
2213
const geolocate = new mapbox.GeolocateControl(untrack(() => options))
2314
map.addControl(geolocate, untrack(() => position))
2415
2516
onMount(() => {
26-
return bindEvents(geolocate, handlers, mapbox, dispatcher)
17+
geolocate.on('error', onerror)
18+
geolocate.on('geolocate', ongeolocate)
19+
geolocate.on('outofmaxbounds', onoutofmaxbounds)
20+
geolocate.on('trackuserlocationend', ontrackuserlocationend)
21+
geolocate.on('trackuserlocationstart', ontrackuserlocationstart)
2722
})
2823
2924
export function trigger () {

0 commit comments

Comments
 (0)