File tree Expand file tree Collapse file tree 3 files changed +14
-14
lines changed
Expand file tree Collapse file tree 3 files changed +14
-14
lines changed Original file line number Diff line number Diff line change 11import * as maplibre from "maplibre-gl" ;
2- import { createSignal , JSX , onCleanup , splitProps } from "solid-js" ;
2+ import { JSX , onCleanup , splitProps } from "solid-js" ;
33import { useMapEffect , useMap } from "./map" ;
44
55export const createControl =
66 < Control extends maplibre . IControl , Options > ( ctor : new ( options : Options ) => Control ) =>
77 ( props : { options ?: Options ; position ?: maplibre . ControlPosition } ) : JSX . Element => {
88 const [ own , options ] = splitProps ( props , [ "position" ] ) ;
9- const [ control , setControl ] = createSignal < maplibre . IControl > ( ) ;
9+ let control : maplibre . IControl | undefined ;
1010
1111 useMapEffect ( ( map ) => {
12- const existing = control ( ) ;
13- if ( ! existing ) {
14- const created = new ctor ( options . options ?? ( { } as Options ) ) as maplibre . IControl ;
15- map . addControl ( created , own . position ) ;
16- setControl ( created ) ;
12+ if ( ! control ) {
13+ control = new ctor ( options . options ?? ( { } as Options ) ) as maplibre . IControl ;
14+ map . addControl ( control , own . position ) ;
1715 }
1816 } ) ;
1917
2018 onCleanup ( ( ) => {
21- const c = control ( ) ;
2219 const m = useMap ( ) ?.( ) ;
23- if ( c && m && m . hasControl ( c ) ) m . removeControl ( c ) ;
20+ if ( control && m && m . hasControl ( control ) ) m . removeControl ( control ) ;
2421 } ) ;
2522
2623 return < > </ > ;
Original file line number Diff line number Diff line change @@ -8,18 +8,19 @@ export type MarkerProps = Partial<maplibre.MarkerOptions> & {
88
99export function Marker ( initial : MarkerProps ) {
1010 const [ props , options ] = splitProps ( initial , [ "position" ] ) ;
11-
12- const marker = new maplibre . Marker ( options ) ;
11+ let marker : maplibre . Marker | undefined ;
1312
1413 useMapEffect ( ( map ) => {
14+ if ( ! marker ) marker = new maplibre . Marker ( options ) ;
15+
1516 if ( props . position ) {
1617 marker . setLngLat ( props . position ) . addTo ( map ) ;
1718 } else {
1819 marker . remove ( ) ;
1920 }
2021 } ) ;
2122
22- onCleanup ( ( ) => marker . remove ( ) ) ;
23+ onCleanup ( ( ) => marker ? .remove ( ) ) ;
2324
2425 return < > </ > ;
2526}
Original file line number Diff line number Diff line change @@ -10,17 +10,19 @@ export type PopUpProps = Partial<maplibre.PopupOptions> & {
1010export function Popup ( initial : PopUpProps ) {
1111 const [ props , options ] = splitProps ( initial , [ "position" , "content" ] ) ;
1212
13- const popup = new maplibre . Popup ( options ) ;
13+ let popup : maplibre . Popup | undefined ;
1414
1515 useMapEffect ( ( map ) => {
16+ if ( ! popup ) popup = new maplibre . Popup ( options ) ;
17+
1618 if ( props . position && props . content ) {
1719 popup . setLngLat ( props . position ) . setHTML ( props . content ) . addTo ( map ) ;
1820 } else {
1921 popup . remove ( ) ;
2022 }
2123 } ) ;
2224
23- onCleanup ( ( ) => popup . remove ( ) ) ;
25+ onCleanup ( ( ) => popup ? .remove ( ) ) ;
2426
2527 return < > </ > ;
2628}
You can’t perform that action at this time.
0 commit comments