maplibrex-svelte 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +300 -0
  3. package/dist/components/MapLibre.svelte +258 -0
  4. package/dist/components/MapLibre.svelte.d.ts +42 -0
  5. package/dist/components/controls/AttributionControl.svelte +21 -0
  6. package/dist/components/controls/AttributionControl.svelte.d.ts +11 -0
  7. package/dist/components/controls/Control.svelte +58 -0
  8. package/dist/components/controls/Control.svelte.d.ts +33 -0
  9. package/dist/components/controls/CustomControl.svelte +46 -0
  10. package/dist/components/controls/CustomControl.svelte.d.ts +13 -0
  11. package/dist/components/controls/FullscreenControl.svelte +32 -0
  12. package/dist/components/controls/FullscreenControl.svelte.d.ts +12 -0
  13. package/dist/components/controls/GeolocateControl.svelte +32 -0
  14. package/dist/components/controls/GeolocateControl.svelte.d.ts +12 -0
  15. package/dist/components/controls/GlobeControl.svelte +16 -0
  16. package/dist/components/controls/GlobeControl.svelte.d.ts +11 -0
  17. package/dist/components/controls/LogoControl.svelte +21 -0
  18. package/dist/components/controls/LogoControl.svelte.d.ts +11 -0
  19. package/dist/components/controls/NavigationControl.svelte +21 -0
  20. package/dist/components/controls/NavigationControl.svelte.d.ts +11 -0
  21. package/dist/components/controls/ScaleControl.svelte +21 -0
  22. package/dist/components/controls/ScaleControl.svelte.d.ts +11 -0
  23. package/dist/components/controls/TerrainControl.svelte +21 -0
  24. package/dist/components/controls/TerrainControl.svelte.d.ts +11 -0
  25. package/dist/components/data/FeatureState.svelte +56 -0
  26. package/dist/components/data/FeatureState.svelte.d.ts +13 -0
  27. package/dist/components/data/Protocol.svelte +24 -0
  28. package/dist/components/data/Protocol.svelte.d.ts +9 -0
  29. package/dist/components/layers/BackgroundLayer.svelte +21 -0
  30. package/dist/components/layers/BackgroundLayer.svelte.d.ts +17 -0
  31. package/dist/components/layers/CircleLayer.svelte +25 -0
  32. package/dist/components/layers/CircleLayer.svelte.d.ts +21 -0
  33. package/dist/components/layers/ColorReliefLayer.svelte +25 -0
  34. package/dist/components/layers/ColorReliefLayer.svelte.d.ts +21 -0
  35. package/dist/components/layers/CustomLayer.svelte +35 -0
  36. package/dist/components/layers/CustomLayer.svelte.d.ts +13 -0
  37. package/dist/components/layers/FillExtrusionLayer.svelte +25 -0
  38. package/dist/components/layers/FillExtrusionLayer.svelte.d.ts +21 -0
  39. package/dist/components/layers/FillLayer.svelte +25 -0
  40. package/dist/components/layers/FillLayer.svelte.d.ts +21 -0
  41. package/dist/components/layers/HeatmapLayer.svelte +25 -0
  42. package/dist/components/layers/HeatmapLayer.svelte.d.ts +21 -0
  43. package/dist/components/layers/HillshadeLayer.svelte +25 -0
  44. package/dist/components/layers/HillshadeLayer.svelte.d.ts +21 -0
  45. package/dist/components/layers/Layer.svelte +204 -0
  46. package/dist/components/layers/Layer.svelte.d.ts +29 -0
  47. package/dist/components/layers/LineLayer.svelte +25 -0
  48. package/dist/components/layers/LineLayer.svelte.d.ts +21 -0
  49. package/dist/components/layers/RasterLayer.svelte +25 -0
  50. package/dist/components/layers/RasterLayer.svelte.d.ts +21 -0
  51. package/dist/components/layers/SymbolLayer.svelte +25 -0
  52. package/dist/components/layers/SymbolLayer.svelte.d.ts +21 -0
  53. package/dist/components/overlays/Marker.svelte +204 -0
  54. package/dist/components/overlays/Marker.svelte.d.ts +27 -0
  55. package/dist/components/overlays/Popup.svelte +184 -0
  56. package/dist/components/overlays/Popup.svelte.d.ts +24 -0
  57. package/dist/components/sources/CanvasSource.svelte +16 -0
  58. package/dist/components/sources/CanvasSource.svelte.d.ts +13 -0
  59. package/dist/components/sources/GeoJSONSource.svelte +16 -0
  60. package/dist/components/sources/GeoJSONSource.svelte.d.ts +13 -0
  61. package/dist/components/sources/ImageSource.svelte +16 -0
  62. package/dist/components/sources/ImageSource.svelte.d.ts +13 -0
  63. package/dist/components/sources/RasterDEMSource.svelte +16 -0
  64. package/dist/components/sources/RasterDEMSource.svelte.d.ts +13 -0
  65. package/dist/components/sources/RasterSource.svelte +16 -0
  66. package/dist/components/sources/RasterSource.svelte.d.ts +13 -0
  67. package/dist/components/sources/Source.svelte +88 -0
  68. package/dist/components/sources/Source.svelte.d.ts +14 -0
  69. package/dist/components/sources/VectorSource.svelte +16 -0
  70. package/dist/components/sources/VectorSource.svelte.d.ts +13 -0
  71. package/dist/components/sources/VideoSource.svelte +16 -0
  72. package/dist/components/sources/VideoSource.svelte.d.ts +13 -0
  73. package/dist/components/style/GlobalState.svelte +67 -0
  74. package/dist/components/style/GlobalState.svelte.d.ts +8 -0
  75. package/dist/components/style/Image.svelte +92 -0
  76. package/dist/components/style/Image.svelte.d.ts +14 -0
  77. package/dist/components/style/Light.svelte +31 -0
  78. package/dist/components/style/Light.svelte.d.ts +4 -0
  79. package/dist/components/style/Projection.svelte +31 -0
  80. package/dist/components/style/Projection.svelte.d.ts +4 -0
  81. package/dist/components/style/Sky.svelte +31 -0
  82. package/dist/components/style/Sky.svelte.d.ts +4 -0
  83. package/dist/components/style/Terrain.svelte +50 -0
  84. package/dist/components/style/Terrain.svelte.d.ts +9 -0
  85. package/dist/context.d.ts +41 -0
  86. package/dist/context.js +31 -0
  87. package/dist/index.d.ts +57 -0
  88. package/dist/index.js +48 -0
  89. package/dist/internal/bind-events.svelte.d.ts +26 -0
  90. package/dist/internal/bind-events.svelte.js +49 -0
  91. package/dist/internal/camera.d.ts +15 -0
  92. package/dist/internal/camera.js +33 -0
  93. package/dist/internal/deep-equal.d.ts +6 -0
  94. package/dist/internal/deep-equal.js +25 -0
  95. package/dist/internal/diff.d.ts +15 -0
  96. package/dist/internal/diff.js +29 -0
  97. package/dist/internal/events.d.ts +22 -0
  98. package/dist/internal/events.js +40 -0
  99. package/dist/internal/interactions.d.ts +22 -0
  100. package/dist/internal/interactions.js +36 -0
  101. package/dist/internal/source.d.ts +25 -0
  102. package/dist/internal/source.js +25 -0
  103. package/dist/internal/stable-key.d.ts +9 -0
  104. package/dist/internal/stable-key.js +17 -0
  105. package/dist/internal/style-ops.d.ts +26 -0
  106. package/dist/internal/style-ops.js +56 -0
  107. package/dist/types.d.ts +21 -0
  108. package/dist/types.js +1 -0
  109. package/package.json +92 -0
@@ -0,0 +1,49 @@
1
+ import { untrack } from 'svelte';
2
+ import { pickEventHandlers } from './events.js';
3
+ function keySetOf(handlers) {
4
+ return Object.keys(handlers).sort().join(' ');
5
+ }
6
+ /**
7
+ * Keeps MapLibre subscriptions in sync with a component's `on<event>` props.
8
+ *
9
+ * Subscriptions are rebuilt only when the SET OF EVENT NAMES changes. Handler
10
+ * identity is deliberately not tracked: inline arrow functions get a new
11
+ * identity on every render, and re-subscribing on each of them would churn
12
+ * listeners on a hot path. The indirection below always calls the current
13
+ * handler, so the latest closure still wins.
14
+ */
15
+ export function bindEvents(getTarget, getProps) {
16
+ const handlers = $derived(pickEventHandlers(getProps()));
17
+ const names = $derived(keySetOf(handlers));
18
+ $effect(() => {
19
+ const target = getTarget();
20
+ if (!target)
21
+ return;
22
+ void names;
23
+ const subscriptions = untrack(() => Object.keys(handlers).map((name) => target.on(name, ((event) => handlers[name]?.(event)))));
24
+ return () => {
25
+ for (const subscription of subscriptions)
26
+ subscription.unsubscribe();
27
+ };
28
+ });
29
+ }
30
+ /**
31
+ * The layer-scoped variant: MapLibre filters these events by rendered features,
32
+ * so the layer id is part of the subscription and a layer rename must rebuild it.
33
+ */
34
+ export function bindLayerEvents(getMap, getLayerId, getProps) {
35
+ const handlers = $derived(pickEventHandlers(getProps()));
36
+ const names = $derived(keySetOf(handlers));
37
+ $effect(() => {
38
+ const map = getMap();
39
+ const layerId = getLayerId();
40
+ if (!map || !layerId)
41
+ return;
42
+ void names;
43
+ const subscriptions = untrack(() => Object.keys(handlers).map((name) => map.on(name, layerId, (event) => handlers[name]?.(event))));
44
+ return () => {
45
+ for (const subscription of subscriptions)
46
+ subscription.unsubscribe();
47
+ };
48
+ });
49
+ }
@@ -0,0 +1,15 @@
1
+ import type { LngLatLike } from 'maplibre-gl';
2
+ /** Below this, a difference is float noise from MapLibre's own transform math. */
3
+ export declare const CAMERA_EPSILON = 0.000001;
4
+ export type LngLatTuple = [lng: number, lat: number];
5
+ /**
6
+ * Normalises every accepted `LngLatLike` shape into a plain tuple.
7
+ * Comparison must not depend on which of the four shapes the caller used.
8
+ */
9
+ export declare function toLngLatTuple(value: LngLatLike | undefined | null): LngLatTuple | null;
10
+ export declare function sameNumber(a: number | undefined | null, b: number | undefined | null, epsilon?: number): boolean;
11
+ /**
12
+ * Guards the two-way camera binding: the map emits `move` continuously, so
13
+ * writing a prop back without this check would feed an endless update loop.
14
+ */
15
+ export declare function sameLngLat(a: LngLatLike | undefined | null, b: LngLatLike | undefined | null, epsilon?: number): boolean;
@@ -0,0 +1,33 @@
1
+ /** Below this, a difference is float noise from MapLibre's own transform math. */
2
+ export const CAMERA_EPSILON = 1e-6;
3
+ /**
4
+ * Normalises every accepted `LngLatLike` shape into a plain tuple.
5
+ * Comparison must not depend on which of the four shapes the caller used.
6
+ */
7
+ export function toLngLatTuple(value) {
8
+ if (value == null)
9
+ return null;
10
+ if (Array.isArray(value))
11
+ return [value[0], value[1]];
12
+ if ('lng' in value)
13
+ return [value.lng, value.lat];
14
+ if ('lon' in value)
15
+ return [value.lon, value.lat];
16
+ return null;
17
+ }
18
+ export function sameNumber(a, b, epsilon = CAMERA_EPSILON) {
19
+ if (a == null || b == null)
20
+ return a == null && b == null;
21
+ return Math.abs(a - b) <= epsilon;
22
+ }
23
+ /**
24
+ * Guards the two-way camera binding: the map emits `move` continuously, so
25
+ * writing a prop back without this check would feed an endless update loop.
26
+ */
27
+ export function sameLngLat(a, b, epsilon = CAMERA_EPSILON) {
28
+ const left = toLngLatTuple(a);
29
+ const right = toLngLatTuple(b);
30
+ if (left === null || right === null)
31
+ return left === right;
32
+ return sameNumber(left[0], right[0], epsilon) && sameNumber(left[1], right[1], epsilon);
33
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Structural equality for style-spec values (expressions, arrays, plain objects).
3
+ * Style specifications are JSON, so reference identity is never a safe shortcut:
4
+ * a template literal or an inline array produces a new reference on every render.
5
+ */
6
+ export declare function deepEqual(a: unknown, b: unknown): boolean;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Structural equality for style-spec values (expressions, arrays, plain objects).
3
+ * Style specifications are JSON, so reference identity is never a safe shortcut:
4
+ * a template literal or an inline array produces a new reference on every render.
5
+ */
6
+ export function deepEqual(a, b) {
7
+ if (a === b)
8
+ return true;
9
+ if (typeof a !== typeof b)
10
+ return false;
11
+ if (a === null || b === null || typeof a !== 'object')
12
+ return false;
13
+ if (Array.isArray(a) || Array.isArray(b)) {
14
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length)
15
+ return false;
16
+ return a.every((item, index) => deepEqual(item, b[index]));
17
+ }
18
+ const left = a;
19
+ const right = b;
20
+ const leftKeys = Object.keys(left);
21
+ const rightKeys = Object.keys(right);
22
+ if (leftKeys.length !== rightKeys.length)
23
+ return false;
24
+ return leftKeys.every((key) => Object.hasOwn(right, key) && deepEqual(left[key], right[key]));
25
+ }
@@ -0,0 +1,15 @@
1
+ export type RecordDiff<TValue> = {
2
+ /** Keys whose value must be pushed to MapLibre, with their new value. */
3
+ changed: Array<[string, TValue]>;
4
+ /** Keys that must be reset to their style default. */
5
+ removed: string[];
6
+ };
7
+ /**
8
+ * Compares two paint/layout records and returns the minimal set of updates.
9
+ *
10
+ * Recreating a layer to change one paint property throws away its tiles and
11
+ * restarts every transition, so callers push only what actually changed.
12
+ * An explicit `undefined` value counts as a removal, matching the MapLibre
13
+ * setter contract where `undefined` restores the style default.
14
+ */
15
+ export declare function diffRecords<TValue>(previous: Record<string, TValue | undefined> | undefined, next: Record<string, TValue | undefined> | undefined): RecordDiff<TValue>;
@@ -0,0 +1,29 @@
1
+ import { deepEqual } from './deep-equal.js';
2
+ /**
3
+ * Compares two paint/layout records and returns the minimal set of updates.
4
+ *
5
+ * Recreating a layer to change one paint property throws away its tiles and
6
+ * restarts every transition, so callers push only what actually changed.
7
+ * An explicit `undefined` value counts as a removal, matching the MapLibre
8
+ * setter contract where `undefined` restores the style default.
9
+ */
10
+ export function diffRecords(previous, next) {
11
+ const before = previous ?? {};
12
+ const after = next ?? {};
13
+ const changed = [];
14
+ const removed = [];
15
+ for (const [key, value] of Object.entries(after)) {
16
+ if (value === undefined) {
17
+ if (before[key] !== undefined)
18
+ removed.push(key);
19
+ continue;
20
+ }
21
+ if (!deepEqual(before[key], value))
22
+ changed.push([key, value]);
23
+ }
24
+ for (const key of Object.keys(before)) {
25
+ if (before[key] !== undefined && !Object.hasOwn(after, key))
26
+ removed.push(key);
27
+ }
28
+ return { changed, removed };
29
+ }
@@ -0,0 +1,22 @@
1
+ export type EventHandler = (event: never) => void;
2
+ /** `onclick` -> `click`. MapLibre event names are already lowercase. */
3
+ export declare function eventNameOf(propName: string): string;
4
+ /**
5
+ * Extracts `on<event>` callback props into a `{ eventName: handler }` record.
6
+ *
7
+ * Components accept every MapLibre event as a typed callback prop, so this is
8
+ * what turns that surface into concrete subscriptions without enumerating the
9
+ * event list by hand in each component.
10
+ */
11
+ export declare function pickEventHandlers(props: Record<string, unknown>): Record<string, EventHandler>;
12
+ /**
13
+ * Separates `on<event>` callbacks from everything else.
14
+ *
15
+ * Control components pass their remaining props straight to a MapLibre
16
+ * constructor, so an event handler left among them would be handed over as if
17
+ * it were an option. Splitting first is what lets a control accept both.
18
+ */
19
+ export declare function splitEventProps(props: Record<string, unknown>): {
20
+ events: Record<string, EventHandler>;
21
+ rest: Record<string, unknown>;
22
+ };
@@ -0,0 +1,40 @@
1
+ /** `onclick` -> `click`. MapLibre event names are already lowercase. */
2
+ export function eventNameOf(propName) {
3
+ return propName.slice(2);
4
+ }
5
+ function isHandlerProp(key, value) {
6
+ return key.length > 2 && key.startsWith('on') && typeof value === 'function';
7
+ }
8
+ /**
9
+ * Extracts `on<event>` callback props into a `{ eventName: handler }` record.
10
+ *
11
+ * Components accept every MapLibre event as a typed callback prop, so this is
12
+ * what turns that surface into concrete subscriptions without enumerating the
13
+ * event list by hand in each component.
14
+ */
15
+ export function pickEventHandlers(props) {
16
+ const handlers = {};
17
+ for (const [key, value] of Object.entries(props)) {
18
+ if (isHandlerProp(key, value))
19
+ handlers[eventNameOf(key)] = value;
20
+ }
21
+ return handlers;
22
+ }
23
+ /**
24
+ * Separates `on<event>` callbacks from everything else.
25
+ *
26
+ * Control components pass their remaining props straight to a MapLibre
27
+ * constructor, so an event handler left among them would be handed over as if
28
+ * it were an option. Splitting first is what lets a control accept both.
29
+ */
30
+ export function splitEventProps(props) {
31
+ const events = {};
32
+ const rest = {};
33
+ for (const [key, value] of Object.entries(props)) {
34
+ if (isHandlerProp(key, value))
35
+ events[key] = value;
36
+ else
37
+ rest[key] = value;
38
+ }
39
+ return { events, rest };
40
+ }
@@ -0,0 +1,22 @@
1
+ import type { Map as MapLibreMap } from 'maplibre-gl';
2
+ /**
3
+ * The gesture handlers MapLibre exposes as properties on the map, each with
4
+ * `enable()`, `disable()` and `isEnabled()`. Sorted so that planned changes
5
+ * are applied in a stable order.
6
+ */
7
+ export declare const INTERACTION_NAMES: readonly ["boxZoom", "cooperativeGestures", "doubleClickZoom", "dragPan", "dragRotate", "keyboard", "scrollZoom", "touchPitch", "touchZoomRotate"];
8
+ export type InteractionName = (typeof INTERACTION_NAMES)[number];
9
+ /** What the caller wants; an omitted or `undefined` entry means "leave it alone". */
10
+ export type InteractionRequest = Partial<Record<InteractionName, boolean | undefined>>;
11
+ export type InteractionChange = [name: InteractionName, enabled: boolean];
12
+ /**
13
+ * Works out which handlers actually need toggling.
14
+ *
15
+ * Calling `enable()` on an already-enabled handler is not free: MapLibre
16
+ * re-registers DOM listeners, and for the cooperative gestures handler it
17
+ * rebuilds its overlay element. Comparing first keeps a reactive prop from
18
+ * churning the handler on every unrelated render.
19
+ */
20
+ export declare function planInteractionChanges(current: Partial<Record<InteractionName, boolean>>, requested: InteractionRequest | undefined): InteractionChange[];
21
+ /** Reads the live enabled state of every handler on a map. */
22
+ export declare function readInteractionState(map: MapLibreMap): Record<InteractionName, boolean>;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The gesture handlers MapLibre exposes as properties on the map, each with
3
+ * `enable()`, `disable()` and `isEnabled()`. Sorted so that planned changes
4
+ * are applied in a stable order.
5
+ */
6
+ export const INTERACTION_NAMES = [
7
+ 'boxZoom',
8
+ 'cooperativeGestures',
9
+ 'doubleClickZoom',
10
+ 'dragPan',
11
+ 'dragRotate',
12
+ 'keyboard',
13
+ 'scrollZoom',
14
+ 'touchPitch',
15
+ 'touchZoomRotate'
16
+ ];
17
+ /**
18
+ * Works out which handlers actually need toggling.
19
+ *
20
+ * Calling `enable()` on an already-enabled handler is not free: MapLibre
21
+ * re-registers DOM listeners, and for the cooperative gestures handler it
22
+ * rebuilds its overlay element. Comparing first keeps a reactive prop from
23
+ * churning the handler on every unrelated render.
24
+ */
25
+ export function planInteractionChanges(current, requested) {
26
+ if (!requested)
27
+ return [];
28
+ return INTERACTION_NAMES.filter((name) => {
29
+ const want = requested[name];
30
+ return want !== undefined && current[name] !== undefined && current[name] !== want;
31
+ }).map((name) => [name, requested[name]]);
32
+ }
33
+ /** Reads the live enabled state of every handler on a map. */
34
+ export function readInteractionState(map) {
35
+ return Object.fromEntries(INTERACTION_NAMES.map((name) => [name, map[name].isEnabled()]));
36
+ }
@@ -0,0 +1,25 @@
1
+ import type { CanvasSourceSpecification, SourceSpecification } from 'maplibre-gl';
2
+ /**
3
+ * Canvas sources sit outside the style specification union because they are
4
+ * runtime-only, but `map.addSource` accepts them all the same.
5
+ */
6
+ export type AnySourceSpecification = SourceSpecification | CanvasSourceSpecification;
7
+ export type SourceUpdatePlan = {
8
+ kind: 'noop';
9
+ } | {
10
+ kind: 'create';
11
+ } | {
12
+ kind: 'recreate';
13
+ } | {
14
+ kind: 'set-data';
15
+ data: NonNullable<unknown>;
16
+ };
17
+ /**
18
+ * Decides how to move a source from one specification to the next.
19
+ *
20
+ * GeoJSON data changes are the hot path of any real map, and `setData` keeps
21
+ * the source alive; recreating it would force every dependent layer to be
22
+ * torn down and re-added. Every other change still needs a full recreate,
23
+ * because MapLibre exposes no setter for the remaining source options.
24
+ */
25
+ export declare function planSourceUpdate(previous: AnySourceSpecification | undefined, next: AnySourceSpecification): SourceUpdatePlan;
@@ -0,0 +1,25 @@
1
+ import { deepEqual } from './deep-equal.js';
2
+ /**
3
+ * Decides how to move a source from one specification to the next.
4
+ *
5
+ * GeoJSON data changes are the hot path of any real map, and `setData` keeps
6
+ * the source alive; recreating it would force every dependent layer to be
7
+ * torn down and re-added. Every other change still needs a full recreate,
8
+ * because MapLibre exposes no setter for the remaining source options.
9
+ */
10
+ export function planSourceUpdate(previous, next) {
11
+ if (previous === undefined)
12
+ return { kind: 'create' };
13
+ if (previous.type !== next.type)
14
+ return { kind: 'recreate' };
15
+ if (deepEqual(previous, next))
16
+ return { kind: 'noop' };
17
+ if (previous.type === 'geojson' && next.type === 'geojson') {
18
+ const withoutData = { ...previous, data: null };
19
+ const nextWithoutData = { ...next, data: null };
20
+ if (deepEqual(withoutData, nextWithoutData) && next.data !== undefined) {
21
+ return { kind: 'set-data', data: next.data };
22
+ }
23
+ }
24
+ return { kind: 'recreate' };
25
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A deterministic string identity for a plain options object.
3
+ *
4
+ * MapLibre controls take their options in the constructor and expose no
5
+ * setters, so the only way to honour a changed option is to recreate the
6
+ * control. Effects need a tracked scalar to decide that, and key order must not
7
+ * count as a change: `{a, b}` and `{b, a}` describe the same control.
8
+ */
9
+ export declare function stableKey(value: unknown): string;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A deterministic string identity for a plain options object.
3
+ *
4
+ * MapLibre controls take their options in the constructor and expose no
5
+ * setters, so the only way to honour a changed option is to recreate the
6
+ * control. Effects need a tracked scalar to decide that, and key order must not
7
+ * count as a change: `{a, b}` and `{b, a}` describe the same control.
8
+ */
9
+ export function stableKey(value) {
10
+ if (value === undefined)
11
+ return 'undefined';
12
+ return JSON.stringify(value, (_key, item) => {
13
+ if (item === null || typeof item !== 'object' || Array.isArray(item))
14
+ return item;
15
+ return Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b)));
16
+ });
17
+ }
@@ -0,0 +1,26 @@
1
+ import type { Map as MapLibreMap } from 'maplibre-gl';
2
+ /**
3
+ * Removal is always best-effort.
4
+ *
5
+ * Teardown races with two things the component cannot observe: a style swap
6
+ * that already dropped everything the user added, and a map that is being
7
+ * destroyed. Both make a plain `removeLayer`/`removeSource` throw on an entity
8
+ * that is legitimately gone, so absence is the success case, not an error.
9
+ */
10
+ export declare function isStyleAlive(map: MapLibreMap): boolean;
11
+ export declare function removeLayerIfPresent(map: MapLibreMap, layerId: string): void;
12
+ export declare function removeSourceIfPresent(map: MapLibreMap, sourceId: string): void;
13
+ /**
14
+ * MapLibre refuses to remove a source that layers still reference, so a source
15
+ * recreate has to evict its dependents first. The ids are returned for logging
16
+ * and tests; the layer components re-add themselves on the next source epoch.
17
+ */
18
+ export declare function removeLayersUsingSource(map: MapLibreMap, sourceId: string): string[];
19
+ /**
20
+ * MapLibre types the paint and layout setters against exhaustive property
21
+ * unions, but a component receives its keys as data at runtime. The typed
22
+ * layer wrappers are what keep the consumer-facing surface honest, so the
23
+ * widening is expressed here once instead of at every call site.
24
+ */
25
+ export declare function setPaintProperty(map: MapLibreMap, layerId: string, name: string, value: unknown): void;
26
+ export declare function setLayoutProperty(map: MapLibreMap, layerId: string, name: string, value: unknown): void;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Removal is always best-effort.
3
+ *
4
+ * Teardown races with two things the component cannot observe: a style swap
5
+ * that already dropped everything the user added, and a map that is being
6
+ * destroyed. Both make a plain `removeLayer`/`removeSource` throw on an entity
7
+ * that is legitimately gone, so absence is the success case, not an error.
8
+ */
9
+ export function isStyleAlive(map) {
10
+ try {
11
+ return Boolean(map.getStyle());
12
+ }
13
+ catch {
14
+ return false;
15
+ }
16
+ }
17
+ export function removeLayerIfPresent(map, layerId) {
18
+ if (!isStyleAlive(map))
19
+ return;
20
+ if (map.getLayer(layerId))
21
+ map.removeLayer(layerId);
22
+ }
23
+ export function removeSourceIfPresent(map, sourceId) {
24
+ if (!isStyleAlive(map))
25
+ return;
26
+ if (map.getSource(sourceId))
27
+ map.removeSource(sourceId);
28
+ }
29
+ /**
30
+ * MapLibre refuses to remove a source that layers still reference, so a source
31
+ * recreate has to evict its dependents first. The ids are returned for logging
32
+ * and tests; the layer components re-add themselves on the next source epoch.
33
+ */
34
+ export function removeLayersUsingSource(map, sourceId) {
35
+ if (!isStyleAlive(map))
36
+ return [];
37
+ const dependents = map
38
+ .getStyle()
39
+ .layers.filter((layer) => 'source' in layer && layer.source === sourceId)
40
+ .map((layer) => layer.id);
41
+ for (const layerId of dependents)
42
+ removeLayerIfPresent(map, layerId);
43
+ return dependents;
44
+ }
45
+ /**
46
+ * MapLibre types the paint and layout setters against exhaustive property
47
+ * unions, but a component receives its keys as data at runtime. The typed
48
+ * layer wrappers are what keep the consumer-facing surface honest, so the
49
+ * widening is expressed here once instead of at every call site.
50
+ */
51
+ export function setPaintProperty(map, layerId, name, value) {
52
+ map.setPaintProperty(layerId, name, value);
53
+ }
54
+ export function setLayoutProperty(map, layerId, name, value) {
55
+ map.setLayoutProperty(layerId, name, value);
56
+ }
@@ -0,0 +1,21 @@
1
+ import type { MapEventType, MapLayerEventType, MarkerEventType, PopupEventType } from 'maplibre-gl';
2
+ /**
3
+ * Turns an event map into `on<event>` callback props.
4
+ *
5
+ * Deriving the props from MapLibre's own event maps means a new upstream event
6
+ * becomes available without touching this library, and every handler keeps its
7
+ * exact payload type.
8
+ */
9
+ export type EventProps<TEventMap> = {
10
+ [K in keyof TEventMap as `on${K & string}`]?: (event: TEventMap[K]) => void;
11
+ };
12
+ /** Every map-level event, e.g. `onload`, `onmoveend`, `onerror`. */
13
+ export type MapEventProps = EventProps<MapEventType>;
14
+ /** Every layer-scoped event, e.g. `onclick`, `onmouseenter`, `onmouseleave`. */
15
+ export type LayerEventProps = EventProps<MapLayerEventType>;
16
+ /** `ondragstart`, `ondrag`, `ondragend`, `onclick`. */
17
+ export type MarkerEventProps = EventProps<MarkerEventType>;
18
+ /** `onopen`, `onclose`. */
19
+ export type PopupEventProps = EventProps<PopupEventType>;
20
+ /** How a camera prop change is applied to the map. */
21
+ export type CameraMode = 'jump' | 'ease' | 'fly';
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "maplibrex-svelte",
3
+ "version": "0.1.0",
4
+ "description": "Component-oriented Svelte 5 bindings for MapLibre GL JS, written in TypeScript.",
5
+ "license": "MIT",
6
+ "author": "Yendris Rogelio Cruz Mendoza",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/CountlinkX-Solutions/maplibrex-svelte.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/CountlinkX-Solutions/maplibrex-svelte/issues"
13
+ },
14
+ "homepage": "https://github.com/CountlinkX-Solutions/maplibrex-svelte#readme",
15
+ "keywords": [
16
+ "svelte",
17
+ "sveltekit",
18
+ "maplibre",
19
+ "maplibre-gl",
20
+ "map",
21
+ "gis",
22
+ "webgl",
23
+ "typescript"
24
+ ],
25
+ "engines": {
26
+ "node": "^20.19.0 || >=22.12.0"
27
+ },
28
+ "scripts": {
29
+ "dev": "vite dev",
30
+ "build": "vite build && npm run prepack",
31
+ "preview": "vite preview",
32
+ "prepare": "svelte-kit sync || echo ''",
33
+ "prepack": "svelte-kit sync && svelte-package && publint",
34
+ "prepublishOnly": "npm run check && npm run test",
35
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
36
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
37
+ "lint": "prettier --check . && eslint .",
38
+ "format": "prettier --write .",
39
+ "test:unit": "vitest",
40
+ "test": "npm run test:unit -- --run"
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "!dist/**/*.test.*",
45
+ "!dist/**/*.spec.*"
46
+ ],
47
+ "sideEffects": [
48
+ "**/*.css"
49
+ ],
50
+ "svelte": "./dist/index.js",
51
+ "types": "./dist/index.d.ts",
52
+ "type": "module",
53
+ "exports": {
54
+ ".": {
55
+ "types": "./dist/index.d.ts",
56
+ "svelte": "./dist/index.js",
57
+ "default": "./dist/index.js"
58
+ },
59
+ "./package.json": "./package.json"
60
+ },
61
+ "peerDependencies": {
62
+ "maplibre-gl": "^6.0.0",
63
+ "svelte": "^5.0.0"
64
+ },
65
+ "devDependencies": {
66
+ "@eslint/js": "^10.0.1",
67
+ "@sveltejs/adapter-auto": "^7.0.1",
68
+ "@sveltejs/kit": "^2.70.3",
69
+ "@sveltejs/package": "^2.5.8",
70
+ "@sveltejs/vite-plugin-svelte": "^7.3.0",
71
+ "@testing-library/jest-dom": "^6.8.0",
72
+ "@testing-library/svelte": "^5.4.2",
73
+ "@types/node": "^24",
74
+ "@vitest/browser-playwright": "^4.1.11",
75
+ "eslint": "^10.4.1",
76
+ "eslint-config-prettier": "^10.1.8",
77
+ "eslint-plugin-svelte": "^3.19.0",
78
+ "globals": "^17.6.0",
79
+ "jsdom": "^26.1.0",
80
+ "maplibre-gl": "^6.6.0",
81
+ "prettier": "^3.8.3",
82
+ "prettier-plugin-svelte": "^4.1.0",
83
+ "publint": "^0.3.24",
84
+ "svelte": "^5.56.10",
85
+ "svelte-check": "^4.7.6",
86
+ "typescript": "^6.0.3",
87
+ "typescript-eslint": "^8.60.1",
88
+ "vite": "^8.2.2",
89
+ "vitest": "^4.1.11",
90
+ "vitest-browser-svelte": "^2.2.1"
91
+ }
92
+ }