Skip to content

Commit

Permalink
fix: ensure compatibility with SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
shishkin committed Nov 8, 2022
1 parent f6a4b58 commit beb0aeb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
15 changes: 6 additions & 9 deletions src/controls.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import * as maplibre from "maplibre-gl";
import { createSignal, JSX, onCleanup, splitProps } from "solid-js";
import { JSX, onCleanup, splitProps } from "solid-js";
import { useMapEffect, useMap } from "./map";

export const createControl =
<Control extends maplibre.IControl, Options>(ctor: new (options: Options) => Control) =>
(props: { options?: Options; position?: maplibre.ControlPosition }): JSX.Element => {
const [own, options] = splitProps(props, ["position"]);
const [control, setControl] = createSignal<maplibre.IControl>();
let control: maplibre.IControl | undefined;

useMapEffect((map) => {
const existing = control();
if (!existing) {
const created = new ctor(options.options ?? ({} as Options)) as maplibre.IControl;
map.addControl(created, own.position);
setControl(created);
if (!control) {
control = new ctor(options.options ?? ({} as Options)) as maplibre.IControl;
map.addControl(control, own.position);
}
});

onCleanup(() => {
const c = control();
const m = useMap()?.();
if (c && m && m.hasControl(c)) m.removeControl(c);
if (control && m && m.hasControl(control)) m.removeControl(control);
});

return <></>;
Expand Down
7 changes: 4 additions & 3 deletions src/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ export type MarkerProps = Partial<maplibre.MarkerOptions> & {

export function Marker(initial: MarkerProps) {
const [props, options] = splitProps(initial, ["position"]);

const marker = new maplibre.Marker(options);
let marker: maplibre.Marker | undefined;

useMapEffect((map) => {
if (!marker) marker = new maplibre.Marker(options);

if (props.position) {
marker.setLngLat(props.position).addTo(map);
} else {
marker.remove();
}
});

onCleanup(() => marker.remove());
onCleanup(() => marker?.remove());

return <></>;
}
6 changes: 4 additions & 2 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ export type PopUpProps = Partial<maplibre.PopupOptions> & {
export function Popup(initial: PopUpProps) {
const [props, options] = splitProps(initial, ["position", "content"]);

const popup = new maplibre.Popup(options);
let popup: maplibre.Popup | undefined;

useMapEffect((map) => {
if (!popup) popup = new maplibre.Popup(options);

if (props.position && props.content) {
popup.setLngLat(props.position).setHTML(props.content).addTo(map);
} else {
popup.remove();
}
});

onCleanup(() => popup.remove());
onCleanup(() => popup?.remove());

return <></>;
}

0 comments on commit beb0aeb

Please sign in to comment.