Skip to content

Commit beb0aeb

Browse files
committed
fix: ensure compatibility with SSR
1 parent f6a4b58 commit beb0aeb

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/controls.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import * as maplibre from "maplibre-gl";
2-
import { createSignal, JSX, onCleanup, splitProps } from "solid-js";
2+
import { JSX, onCleanup, splitProps } from "solid-js";
33
import { useMapEffect, useMap } from "./map";
44

55
export 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 <></>;

src/marker.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ export type MarkerProps = Partial<maplibre.MarkerOptions> & {
88

99
export 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
}

src/popup.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ export type PopUpProps = Partial<maplibre.PopupOptions> & {
1010
export 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
}

0 commit comments

Comments
 (0)