react-bkoi-gl 2.0.0 → 2.0.1

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 (135) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +9 -0
  3. package/dist/index.cjs +137 -111
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +370 -0
  6. package/dist/index.d.ts +370 -2
  7. package/dist/index.js +1434 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/styles/react-bkoi-gl.css +1 -1
  10. package/package.json +62 -30
  11. package/dist/components/attribution-control.d.ts +0 -9
  12. package/dist/components/attribution-control.js +0 -20
  13. package/dist/components/attribution-control.js.map +0 -1
  14. package/dist/components/fullscreen-control.d.ts +0 -12
  15. package/dist/components/fullscreen-control.js +0 -21
  16. package/dist/components/fullscreen-control.js.map +0 -1
  17. package/dist/components/geolocate-control.d.ts +0 -21
  18. package/dist/components/geolocate-control.js +0 -46
  19. package/dist/components/geolocate-control.js.map +0 -1
  20. package/dist/components/layer.d.ts +0 -18
  21. package/dist/components/layer.js +0 -98
  22. package/dist/components/layer.js.map +0 -1
  23. package/dist/components/logo-control.d.ts +0 -9
  24. package/dist/components/logo-control.js +0 -28
  25. package/dist/components/logo-control.js.map +0 -1
  26. package/dist/components/map.d.ts +0 -27
  27. package/dist/components/map.js +0 -108
  28. package/dist/components/map.js.map +0 -1
  29. package/dist/components/marker.d.ts +0 -18
  30. package/dist/components/marker.js +0 -105
  31. package/dist/components/marker.js.map +0 -1
  32. package/dist/components/navigation-control.d.ts +0 -9
  33. package/dist/components/navigation-control.js +0 -19
  34. package/dist/components/navigation-control.js.map +0 -1
  35. package/dist/components/popup.d.ts +0 -15
  36. package/dist/components/popup.js +0 -68
  37. package/dist/components/popup.js.map +0 -1
  38. package/dist/components/scale-control.d.ts +0 -11
  39. package/dist/components/scale-control.js +0 -31
  40. package/dist/components/scale-control.js.map +0 -1
  41. package/dist/components/source.d.ts +0 -6
  42. package/dist/components/source.js +0 -95
  43. package/dist/components/source.js.map +0 -1
  44. package/dist/components/terrain-control.d.ts +0 -10
  45. package/dist/components/terrain-control.js +0 -19
  46. package/dist/components/terrain-control.js.map +0 -1
  47. package/dist/components/use-control.d.ts +0 -9
  48. package/dist/components/use-control.js +0 -30
  49. package/dist/components/use-control.js.map +0 -1
  50. package/dist/components/use-map.d.ts +0 -19
  51. package/dist/components/use-map.js +0 -54
  52. package/dist/components/use-map.js.map +0 -1
  53. package/dist/exports-maplibre-gl.d.ts +0 -33
  54. package/dist/exports-maplibre-gl.js +0 -21
  55. package/dist/exports-maplibre-gl.js.map +0 -1
  56. package/dist/maplibre/create-ref.d.ts +0 -9
  57. package/dist/maplibre/create-ref.js +0 -30
  58. package/dist/maplibre/create-ref.js.map +0 -1
  59. package/dist/maplibre/maplibre.d.ts +0 -81
  60. package/dist/maplibre/maplibre.js +0 -387
  61. package/dist/maplibre/maplibre.js.map +0 -1
  62. package/dist/styles.d.js +0 -2
  63. package/dist/styles.d.js.map +0 -1
  64. package/dist/tsconfig.build.tsbuildinfo +0 -1
  65. package/dist/types/common.d.ts +0 -20
  66. package/dist/types/common.js +0 -2
  67. package/dist/types/common.js.map +0 -1
  68. package/dist/types/events.d.ts +0 -80
  69. package/dist/types/events.js +0 -2
  70. package/dist/types/events.js.map +0 -1
  71. package/dist/types/internal.d.ts +0 -14
  72. package/dist/types/internal.js +0 -2
  73. package/dist/types/internal.js.map +0 -1
  74. package/dist/types/lib.d.ts +0 -40
  75. package/dist/types/lib.js +0 -2
  76. package/dist/types/lib.js.map +0 -1
  77. package/dist/types/style-spec.d.ts +0 -1
  78. package/dist/types/style-spec.js +0 -2
  79. package/dist/types/style-spec.js.map +0 -1
  80. package/dist/utils/apply-react-style.d.ts +0 -2
  81. package/dist/utils/apply-react-style.js +0 -16
  82. package/dist/utils/apply-react-style.js.map +0 -1
  83. package/dist/utils/assert.d.ts +0 -1
  84. package/dist/utils/assert.js +0 -6
  85. package/dist/utils/assert.js.map +0 -1
  86. package/dist/utils/compare-class-names.d.ts +0 -2
  87. package/dist/utils/compare-class-names.js +0 -23
  88. package/dist/utils/compare-class-names.js.map +0 -1
  89. package/dist/utils/deep-equal.d.ts +0 -15
  90. package/dist/utils/deep-equal.js +0 -46
  91. package/dist/utils/deep-equal.js.map +0 -1
  92. package/dist/utils/set-globals.d.ts +0 -20
  93. package/dist/utils/set-globals.js +0 -31
  94. package/dist/utils/set-globals.js.map +0 -1
  95. package/dist/utils/style-utils.d.ts +0 -3
  96. package/dist/utils/style-utils.js +0 -42
  97. package/dist/utils/style-utils.js.map +0 -1
  98. package/dist/utils/transform.d.ts +0 -18
  99. package/dist/utils/transform.js +0 -33
  100. package/dist/utils/transform.js.map +0 -1
  101. package/dist/utils/use-isomorphic-layout-effect.d.ts +0 -3
  102. package/dist/utils/use-isomorphic-layout-effect.js +0 -4
  103. package/dist/utils/use-isomorphic-layout-effect.js.map +0 -1
  104. package/src/components/attribution-control.ts +0 -56
  105. package/src/components/fullscreen-control.ts +0 -40
  106. package/src/components/geolocate-control.ts +0 -96
  107. package/src/components/layer.ts +0 -141
  108. package/src/components/logo-control.ts +0 -44
  109. package/src/components/map.tsx +0 -166
  110. package/src/components/marker.ts +0 -155
  111. package/src/components/navigation-control.ts +0 -28
  112. package/src/components/popup.ts +0 -113
  113. package/src/components/scale-control.ts +0 -44
  114. package/src/components/source.ts +0 -159
  115. package/src/components/terrain-control.ts +0 -29
  116. package/src/components/use-control.ts +0 -68
  117. package/src/components/use-map.tsx +0 -71
  118. package/src/exports-maplibre-gl.ts +0 -37
  119. package/src/index.ts +0 -3
  120. package/src/maplibre/create-ref.ts +0 -70
  121. package/src/maplibre/maplibre.ts +0 -624
  122. package/src/styles.d.ts +0 -4
  123. package/src/types/common.ts +0 -34
  124. package/src/types/events.ts +0 -133
  125. package/src/types/internal.ts +0 -26
  126. package/src/types/lib.ts +0 -84
  127. package/src/types/style-spec.ts +0 -34
  128. package/src/utils/apply-react-style.ts +0 -24
  129. package/src/utils/assert.ts +0 -5
  130. package/src/utils/compare-class-names.ts +0 -29
  131. package/src/utils/deep-equal.ts +0 -61
  132. package/src/utils/set-globals.ts +0 -51
  133. package/src/utils/style-utils.ts +0 -68
  134. package/src/utils/transform.ts +0 -58
  135. package/src/utils/use-isomorphic-layout-effect.ts +0 -8
@@ -1,624 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-floating-promises */
2
- import {
3
- transformToViewState,
4
- applyViewStateToTransform,
5
- } from "../utils/transform";
6
- import { normalizeStyle } from "../utils/style-utils";
7
- import { deepEqual } from "../utils/deep-equal";
8
-
9
- import type { TransformLike } from "../types/internal";
10
- import type {
11
- ViewState,
12
- Point,
13
- PointLike,
14
- PaddingOptions,
15
- ImmutableLike,
16
- LngLatBoundsLike,
17
- MapGeoJSONFeature,
18
- } from "../types/common";
19
- import type {
20
- StyleSpecification,
21
- SkySpecification,
22
- LightSpecification,
23
- TerrainSpecification,
24
- ProjectionSpecification,
25
- } from "../types/style-spec";
26
- import type { Map as MapInstance } from "../types/lib";
27
- import type {
28
- MapCallbacks,
29
- ViewStateChangeEvent,
30
- MapEvent,
31
- ErrorEvent,
32
- MapMouseEvent,
33
- } from "../types/events";
34
-
35
- export type MaplibreProps = Partial<ViewState> &
36
- MapCallbacks & {
37
- /** Camera options used when constructing the Map instance */
38
- initialViewState?: Partial<ViewState> & {
39
- /** The initial bounds of the map. If bounds is specified, it overrides longitude, latitude and zoom options. */
40
- bounds?: LngLatBoundsLike;
41
- /** A fitBounds options object to use only when setting the bounds option. */
42
- fitBoundsOptions?: {
43
- offset?: PointLike;
44
- minZoom?: number;
45
- maxZoom?: number;
46
- padding?: number | PaddingOptions;
47
- };
48
- };
49
-
50
- /** If provided, render into an external WebGL context */
51
- gl?: WebGLRenderingContext;
52
-
53
- /** For external controller to override the camera state */
54
- viewState?: ViewState & {
55
- width: number;
56
- height: number;
57
- };
58
-
59
- // Styling
60
-
61
- /** Mapbox style */
62
- mapStyle?: string | StyleSpecification | ImmutableLike<StyleSpecification>;
63
- /** Enable diffing when the map style changes
64
- * @default true
65
- */
66
- styleDiffing?: boolean;
67
- /** The projection property of the style. Must conform to the Projection Style Specification.
68
- * @default 'mercator'
69
- */
70
- projection?: ProjectionSpecification | "mercator" | "globe";
71
- /** Light properties of the map. */
72
- light?: LightSpecification;
73
- /** Terrain property of the style. Must conform to the Terrain Style Specification.
74
- * If `undefined` is provided, removes terrain from the map. */
75
- terrain?: TerrainSpecification;
76
- /** Sky properties of the map. Must conform to the Sky Style Specification. */
77
- sky?: SkySpecification;
78
-
79
- /** Default layers to query on pointer events */
80
- interactiveLayerIds?: string[];
81
- /** CSS cursor */
82
- cursor?: string;
83
- };
84
-
85
- const DEFAULT_STYLE = {
86
- version: 8,
87
- sources: {},
88
- layers: [],
89
- } as StyleSpecification;
90
-
91
- const pointerEvents = {
92
- mousedown: "onMouseDown",
93
- mouseup: "onMouseUp",
94
- mouseover: "onMouseOver",
95
- mousemove: "onMouseMove",
96
- click: "onClick",
97
- dblclick: "onDblClick",
98
- mouseenter: "onMouseEnter",
99
- mouseleave: "onMouseLeave",
100
- mouseout: "onMouseOut",
101
- contextmenu: "onContextMenu",
102
- touchstart: "onTouchStart",
103
- touchend: "onTouchEnd",
104
- touchmove: "onTouchMove",
105
- touchcancel: "onTouchCancel",
106
- };
107
- const cameraEvents = {
108
- movestart: "onMoveStart",
109
- move: "onMove",
110
- moveend: "onMoveEnd",
111
- dragstart: "onDragStart",
112
- drag: "onDrag",
113
- dragend: "onDragEnd",
114
- zoomstart: "onZoomStart",
115
- zoom: "onZoom",
116
- zoomend: "onZoomEnd",
117
- rotatestart: "onRotateStart",
118
- rotate: "onRotate",
119
- rotateend: "onRotateEnd",
120
- pitchstart: "onPitchStart",
121
- pitch: "onPitch",
122
- pitchend: "onPitchEnd",
123
- };
124
- const otherEvents = {
125
- wheel: "onWheel",
126
- boxzoomstart: "onBoxZoomStart",
127
- boxzoomend: "onBoxZoomEnd",
128
- boxzoomcancel: "onBoxZoomCancel",
129
- resize: "onResize",
130
- load: "onLoad",
131
- render: "onRender",
132
- idle: "onIdle",
133
- remove: "onRemove",
134
- data: "onData",
135
- styledata: "onStyleData",
136
- sourcedata: "onSourceData",
137
- error: "onError",
138
- };
139
- const settingNames = [
140
- "minZoom",
141
- "maxZoom",
142
- "minPitch",
143
- "maxPitch",
144
- "maxBounds",
145
- "projection",
146
- "renderWorldCopies",
147
- ];
148
- const handlerNames = [
149
- "scrollZoom",
150
- "boxZoom",
151
- "dragRotate",
152
- "dragPan",
153
- "keyboard",
154
- "doubleClickZoom",
155
- "touchZoomRotate",
156
- "touchPitch",
157
- ];
158
-
159
- /**
160
- * A wrapper for mapbox-gl's Map class
161
- */
162
- export default class Maplibre {
163
- private _MapClass: { new (options: any): MapInstance };
164
- // mapboxgl.Map instance
165
- private _map: MapInstance = null;
166
- // User-supplied props
167
- props: MaplibreProps;
168
-
169
- // Internal states
170
- private _internalUpdate: boolean = false;
171
- private _hoveredFeatures: MapGeoJSONFeature[] = null;
172
- private _propsedCameraUpdate: ViewState | null = null;
173
- private _styleComponents: {
174
- light?: LightSpecification;
175
- sky?: SkySpecification;
176
- projection?: ProjectionSpecification;
177
- terrain?: TerrainSpecification | null;
178
- } = {};
179
-
180
- static savedMaps: Maplibre[] = [];
181
-
182
- constructor(
183
- MapClass: { new (options: any): MapInstance },
184
- props: MaplibreProps,
185
- container: HTMLDivElement,
186
- ) {
187
- this._MapClass = MapClass;
188
- this.props = props;
189
- this._initialize(container);
190
- }
191
-
192
- get map(): MapInstance {
193
- return this._map;
194
- }
195
-
196
- setProps(props: MaplibreProps) {
197
- const oldProps = this.props;
198
- this.props = props;
199
-
200
- const settingsChanged = this._updateSettings(props, oldProps);
201
- const sizeChanged = this._updateSize(props);
202
- const viewStateChanged = this._updateViewState(props);
203
- this._updateStyle(props, oldProps);
204
- this._updateStyleComponents(props);
205
- this._updateHandlers(props, oldProps);
206
-
207
- // If 1) view state has changed to match props and
208
- // 2) the props change is not triggered by map events,
209
- // it's driven by an external state change. Redraw immediately
210
- if (
211
- settingsChanged ||
212
- sizeChanged ||
213
- (viewStateChanged && !this._map.isMoving())
214
- ) {
215
- this.redraw();
216
- }
217
- }
218
-
219
- static reuse(props: MaplibreProps, container: HTMLDivElement): Maplibre {
220
- const that = Maplibre.savedMaps.pop();
221
- if (!that) {
222
- return null;
223
- }
224
-
225
- const map = that.map;
226
- // When reusing the saved map, we need to reparent the map(canvas) and other child nodes
227
- // intoto the new container from the props.
228
- // Step 1: reparenting child nodes from old container to new container
229
- const oldContainer = map.getContainer();
230
- container.className = oldContainer.className;
231
- while (oldContainer.childNodes.length > 0) {
232
- container.appendChild(oldContainer.childNodes[0]);
233
- }
234
- // Step 2: replace the internal container with new container from the react component
235
- // @ts-ignore
236
- map._container = container;
237
-
238
- // With maplibre-gl as mapLib, map uses ResizeObserver to observe when its container resizes.
239
- // When reusing the saved map, we need to disconnect the observer and observe the new container.
240
- // Step 3: telling the ResizeObserver to disconnect and observe the new container
241
- // @ts-ignore
242
- const resizeObserver = map._resizeObserver;
243
- if (resizeObserver) {
244
- resizeObserver.disconnect();
245
- resizeObserver.observe(container);
246
- }
247
-
248
- // Step 4: apply new props
249
- that.setProps({ ...props, styleDiffing: false });
250
- map.resize();
251
- const { initialViewState } = props;
252
- if (initialViewState) {
253
- if (initialViewState.bounds) {
254
- map.fitBounds(initialViewState.bounds, {
255
- ...initialViewState.fitBoundsOptions,
256
- duration: 0,
257
- });
258
- } else {
259
- that._updateViewState(initialViewState);
260
- }
261
- }
262
-
263
- // Simulate load event
264
- if (map.isStyleLoaded()) {
265
- map.fire("load");
266
- } else {
267
- map.once("style.load", () => map.fire("load"));
268
- }
269
-
270
- // Force reload
271
- // @ts-ignore
272
- map._update();
273
- return that;
274
- }
275
-
276
- /* eslint-disable complexity,max-statements */
277
- private _initialize(container: HTMLDivElement) {
278
- const { props } = this;
279
- const { mapStyle = DEFAULT_STYLE } = props;
280
- const mapOptions = {
281
- ...props,
282
- ...props.initialViewState,
283
- container,
284
- style: normalizeStyle(mapStyle),
285
- };
286
-
287
- const viewState =
288
- mapOptions.initialViewState || mapOptions.viewState || mapOptions;
289
- Object.assign(mapOptions, {
290
- center: [viewState.longitude || 0, viewState.latitude || 0],
291
- zoom: viewState.zoom || 0,
292
- pitch: viewState.pitch || 0,
293
- bearing: viewState.bearing || 0,
294
- });
295
-
296
- if (props.gl) {
297
- // eslint-disable-next-line
298
- const getContext = HTMLCanvasElement.prototype.getContext;
299
- // Hijack canvas.getContext to return our own WebGLContext
300
- // This will be called inside the mapboxgl.Map constructor
301
- // @ts-expect-error
302
- HTMLCanvasElement.prototype.getContext = () => {
303
- // Unhijack immediately
304
- HTMLCanvasElement.prototype.getContext = getContext;
305
- return props.gl;
306
- };
307
- }
308
-
309
- const map = new this._MapClass(mapOptions);
310
- // Props that are not part of constructor options
311
- if (viewState.padding) {
312
- map.setPadding(viewState.padding);
313
- }
314
- if (props.cursor) {
315
- map.getCanvas().style.cursor = props.cursor;
316
- }
317
-
318
- // add listeners
319
- map.transformCameraUpdate = this._onCameraUpdate;
320
- map.on("style.load", () => {
321
- // Map style has changed, this would have wiped out all settings from props
322
- this._styleComponents = {
323
- light: map.getLight(),
324
- sky: map.getSky(),
325
- // @ts-ignore getProjection() does not exist in v4
326
- projection: map.getProjection?.(),
327
- terrain: map.getTerrain(),
328
- };
329
- this._updateStyleComponents(this.props);
330
- });
331
- map.on("sourcedata", () => {
332
- // Some sources have loaded, we may need them to attach terrain
333
- this._updateStyleComponents(this.props);
334
- });
335
- for (const eventName in pointerEvents) {
336
- map.on(eventName, this._onPointerEvent);
337
- }
338
- for (const eventName in cameraEvents) {
339
- map.on(eventName, this._onCameraEvent);
340
- }
341
- for (const eventName in otherEvents) {
342
- map.on(eventName, this._onEvent);
343
- }
344
- this._map = map;
345
- }
346
- /* eslint-enable complexity,max-statements */
347
-
348
- recycle() {
349
- // Clean up unnecessary elements before storing for reuse.
350
- const container = this.map.getContainer();
351
- const children = container.querySelector("[mapboxgl-children]");
352
- children?.remove();
353
-
354
- Maplibre.savedMaps.push(this);
355
- }
356
-
357
- destroy() {
358
- this._map.remove();
359
- }
360
-
361
- // Force redraw the map now. Typically resize() and jumpTo() is reflected in the next
362
- // render cycle, which is managed by Mapbox's animation loop.
363
- // This removes the synchronization issue caused by requestAnimationFrame.
364
- redraw() {
365
- const map = this._map as any;
366
- // map._render will throw error if style does not exist
367
- // https://github.com/mapbox/mapbox-gl-js/blob/fb9fc316da14e99ff4368f3e4faa3888fb43c513
368
- // /src/ui/map.js#L1834
369
- if (map.style) {
370
- // cancel the scheduled update
371
- if (map._frame) {
372
- map._frame.cancel();
373
- map._frame = null;
374
- }
375
- // the order is important - render() may schedule another update
376
- map._render();
377
- }
378
- }
379
-
380
- /* Trigger map resize if size is controlled
381
- @param {object} nextProps
382
- @returns {bool} true if size has changed
383
- */
384
- private _updateSize(nextProps: MaplibreProps): boolean {
385
- // Check if size is controlled
386
- const { viewState } = nextProps;
387
- if (viewState) {
388
- const map = this._map;
389
- if (
390
- viewState.width !== map.transform.width ||
391
- viewState.height !== map.transform.height
392
- ) {
393
- map.resize();
394
- return true;
395
- }
396
- }
397
- return false;
398
- }
399
-
400
- // Adapted from map.jumpTo
401
- /* Update camera to match props
402
- @param {object} nextProps
403
- @param {bool} triggerEvents - should fire camera events
404
- @returns {bool} true if anything is changed
405
- */
406
- private _updateViewState(nextProps: MaplibreProps): boolean {
407
- const map = this._map;
408
- const tr = map.transform;
409
- const isMoving = map.isMoving();
410
-
411
- // Avoid manipulating the real transform when interaction/animation is ongoing
412
- // as it would interfere with Mapbox's handlers
413
- if (!isMoving) {
414
- const changes: any = applyViewStateToTransform(tr, nextProps);
415
- if (Object.keys(changes).length > 0) {
416
- this._internalUpdate = true;
417
- map.jumpTo(changes);
418
- this._internalUpdate = false;
419
- return true;
420
- }
421
- }
422
-
423
- return false;
424
- }
425
-
426
- /* Update camera constraints and projection settings to match props
427
- @param {object} nextProps
428
- @param {object} currProps
429
- @returns {bool} true if anything is changed
430
- */
431
- private _updateSettings(
432
- nextProps: MaplibreProps,
433
- currProps: MaplibreProps,
434
- ): boolean {
435
- const map = this._map;
436
- let changed = false;
437
- for (const propName of settingNames) {
438
- if (
439
- propName in nextProps &&
440
- !deepEqual(nextProps[propName], currProps[propName])
441
- ) {
442
- changed = true;
443
- const setter =
444
- map[`set${propName[0].toUpperCase()}${propName.slice(1)}`];
445
- setter?.call(map, nextProps[propName]);
446
- }
447
- }
448
- return changed;
449
- }
450
-
451
- /* Update map style to match props */
452
- private _updateStyle(
453
- nextProps: MaplibreProps,
454
- currProps: MaplibreProps,
455
- ): void {
456
- if (nextProps.cursor !== currProps.cursor) {
457
- this._map.getCanvas().style.cursor = nextProps.cursor || "";
458
- }
459
- if (nextProps.mapStyle !== currProps.mapStyle) {
460
- const { mapStyle = DEFAULT_STYLE, styleDiffing = true } = nextProps;
461
- const options: any = {
462
- diff: styleDiffing,
463
- };
464
- if ("localIdeographFontFamily" in nextProps) {
465
- // @ts-ignore Mapbox specific prop
466
- options.localIdeographFontFamily = nextProps.localIdeographFontFamily;
467
- }
468
- this._map.setStyle(normalizeStyle(mapStyle), options);
469
- }
470
- }
471
-
472
- /* Update fog, light, projection and terrain to match props
473
- * These props are special because
474
- * 1. They can not be applied right away. Certain conditions (style loaded, source loaded, etc.) must be met
475
- * 2. They can be overwritten by mapStyle
476
- */
477
- private _updateStyleComponents({
478
- light,
479
- projection,
480
- sky,
481
- terrain,
482
- }: MaplibreProps): void {
483
- const map = this._map;
484
- const currProps = this._styleComponents;
485
- // We can safely manipulate map style once it's loaded
486
- if (map.style._loaded) {
487
- if (light && !deepEqual(light, currProps.light)) {
488
- currProps.light = light;
489
- map.setLight(light);
490
- }
491
- if (
492
- projection &&
493
- !deepEqual(projection, currProps.projection) &&
494
- projection !== currProps.projection?.type
495
- ) {
496
- currProps.projection =
497
- typeof projection === "string" ? { type: projection } : projection;
498
- // @ts-ignore setProjection does not exist in v4
499
- map.setProjection?.(currProps.projection);
500
- }
501
- if (sky && !deepEqual(sky, currProps.sky)) {
502
- currProps.sky = sky;
503
- map.setSky(sky);
504
- }
505
- if (terrain !== undefined && !deepEqual(terrain, currProps.terrain)) {
506
- if (!terrain || map.getSource(terrain.source)) {
507
- currProps.terrain = terrain;
508
- map.setTerrain(terrain);
509
- }
510
- }
511
- }
512
- }
513
-
514
- /* Update interaction handlers to match props */
515
- private _updateHandlers(
516
- nextProps: MaplibreProps,
517
- currProps: MaplibreProps,
518
- ): void {
519
- const map = this._map;
520
- for (const propName of handlerNames) {
521
- const newValue = nextProps[propName] ?? true;
522
- const oldValue = currProps[propName] ?? true;
523
- if (!deepEqual(newValue, oldValue)) {
524
- if (newValue) {
525
- map[propName].enable(newValue);
526
- } else {
527
- map[propName].disable();
528
- }
529
- }
530
- }
531
- }
532
-
533
- private _onEvent = (e: MapEvent) => {
534
- // @ts-ignore
535
- const cb = this.props[otherEvents[e.type]];
536
- if (cb) {
537
- cb(e);
538
- } else if (e.type === "error") {
539
- console.error((e as ErrorEvent).error); // eslint-disable-line
540
- }
541
- };
542
-
543
- private _onCameraEvent = (e: ViewStateChangeEvent) => {
544
- if (this._internalUpdate) {
545
- return;
546
- }
547
- e.viewState =
548
- this._propsedCameraUpdate || transformToViewState(this._map.transform);
549
- // @ts-ignore
550
- const cb = this.props[cameraEvents[e.type]];
551
- if (cb) {
552
- cb(e);
553
- }
554
- };
555
-
556
- private _onCameraUpdate = (tr: TransformLike) => {
557
- if (this._internalUpdate) {
558
- return tr;
559
- }
560
- this._propsedCameraUpdate = transformToViewState(tr);
561
- return applyViewStateToTransform(tr, this.props) as any;
562
- };
563
-
564
- private _queryRenderedFeatures(point: Point) {
565
- const map = this._map;
566
- const { interactiveLayerIds = [] } = this.props;
567
- try {
568
- return map.queryRenderedFeatures(point, {
569
- layers: interactiveLayerIds.filter(map.getLayer.bind(map)),
570
- });
571
- } catch {
572
- // May fail if style is not loaded
573
- return [];
574
- }
575
- }
576
-
577
- private _updateHover(e: MapMouseEvent) {
578
- const { props } = this;
579
- const shouldTrackHoveredFeatures =
580
- props.interactiveLayerIds &&
581
- (props.onMouseMove || props.onMouseEnter || props.onMouseLeave);
582
-
583
- if (shouldTrackHoveredFeatures) {
584
- const eventType = e.type;
585
- const wasHovering = this._hoveredFeatures?.length > 0;
586
- const features = this._queryRenderedFeatures(e.point);
587
- const isHovering = features.length > 0;
588
-
589
- if (!isHovering && wasHovering) {
590
- e.type = "mouseleave";
591
- this._onPointerEvent(e);
592
- }
593
- this._hoveredFeatures = features;
594
- if (isHovering && !wasHovering) {
595
- e.type = "mouseenter";
596
- this._onPointerEvent(e);
597
- }
598
- e.type = eventType;
599
- } else {
600
- this._hoveredFeatures = null;
601
- }
602
- }
603
-
604
- private _onPointerEvent = (e: MapMouseEvent) => {
605
- if (e.type === "mousemove" || e.type === "mouseout") {
606
- this._updateHover(e);
607
- }
608
-
609
- // @ts-ignore
610
- const cb = this.props[pointerEvents[e.type]];
611
- if (cb) {
612
- if (
613
- this.props.interactiveLayerIds &&
614
- e.type !== "mouseover" &&
615
- e.type !== "mouseout"
616
- ) {
617
- e.features =
618
- this._hoveredFeatures || this._queryRenderedFeatures(e.point);
619
- }
620
- cb(e);
621
- delete e.features;
622
- }
623
- };
624
- }
package/src/styles.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare module 'react-bkoi-gl/styles' {
2
- const css: string;
3
- export default css;
4
- }
@@ -1,34 +0,0 @@
1
- import type { PaddingOptions } from "maplibre-gl";
2
-
3
- export type {
4
- Point,
5
- PointLike,
6
- LngLat,
7
- LngLatLike,
8
- LngLatBounds,
9
- LngLatBoundsLike,
10
- PaddingOptions,
11
- MapGeoJSONFeature,
12
- } from "maplibre-gl";
13
-
14
- /* Public */
15
-
16
- /** Describes the camera's state */
17
- export type ViewState = {
18
- /** Longitude at map center */
19
- longitude: number;
20
- /** Latitude at map center */
21
- latitude: number;
22
- /** Map zoom level */
23
- zoom: number;
24
- /** Map rotation bearing in degrees counter-clockwise from north */
25
- bearing: number;
26
- /** Map angle in degrees at which the camera is looking at the ground */
27
- pitch: number;
28
- /** Dimensions in pixels applied on each side of the viewport for shifting the vanishing point. */
29
- padding: PaddingOptions;
30
- };
31
-
32
- export interface ImmutableLike<T> {
33
- toJS: () => T;
34
- }