react-bkoi-gl 1.0.7 → 2.0.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 (167) hide show
  1. package/README.md +163 -18
  2. package/dist/components/attribution-control.d.ts +4 -6
  3. package/dist/components/attribution-control.js +6 -5
  4. package/dist/components/attribution-control.js.map +1 -1
  5. package/dist/components/fullscreen-control.d.ts +4 -6
  6. package/dist/components/fullscreen-control.js +4 -4
  7. package/dist/components/fullscreen-control.js.map +1 -1
  8. package/dist/components/geolocate-control.d.ts +10 -26
  9. package/dist/components/geolocate-control.js +11 -11
  10. package/dist/components/geolocate-control.js.map +1 -1
  11. package/dist/components/layer.d.ts +7 -6
  12. package/dist/components/layer.js +8 -9
  13. package/dist/components/layer.js.map +1 -1
  14. package/dist/components/logo-control.d.ts +9 -0
  15. package/dist/components/logo-control.js +28 -0
  16. package/dist/components/logo-control.js.map +1 -0
  17. package/dist/components/map.d.ts +20 -16
  18. package/dist/components/map.js +38 -32
  19. package/dist/components/map.js.map +1 -1
  20. package/dist/components/marker.d.ts +10 -33
  21. package/dist/components/marker.js +22 -15
  22. package/dist/components/marker.js.map +1 -1
  23. package/dist/components/navigation-control.d.ts +4 -6
  24. package/dist/components/navigation-control.js +4 -4
  25. package/dist/components/navigation-control.js.map +1 -1
  26. package/dist/components/popup.d.ts +7 -25
  27. package/dist/components/popup.js +17 -28
  28. package/dist/components/popup.js.map +1 -1
  29. package/dist/components/scale-control.d.ts +4 -6
  30. package/dist/components/scale-control.js +4 -4
  31. package/dist/components/scale-control.js.map +1 -1
  32. package/dist/components/source.d.ts +3 -4
  33. package/dist/components/source.js +20 -22
  34. package/dist/components/source.js.map +1 -1
  35. package/dist/components/terrain-control.d.ts +10 -0
  36. package/dist/components/terrain-control.js +19 -0
  37. package/dist/components/terrain-control.js.map +1 -0
  38. package/dist/components/use-control.d.ts +6 -6
  39. package/dist/components/use-control.js +4 -5
  40. package/dist/components/use-control.js.map +1 -1
  41. package/dist/components/use-map.d.ts +8 -9
  42. package/dist/components/use-map.js +5 -5
  43. package/dist/components/use-map.js.map +1 -1
  44. package/dist/exports-maplibre-gl.d.ts +33 -0
  45. package/dist/exports-maplibre-gl.js +21 -0
  46. package/dist/exports-maplibre-gl.js.map +1 -0
  47. package/dist/index.cjs +590 -681
  48. package/dist/index.d.ts +2 -2
  49. package/dist/index.js +2 -2
  50. package/dist/index.js.map +1 -1
  51. package/dist/{mapbox → maplibre}/create-ref.d.ts +6 -6
  52. package/dist/maplibre/create-ref.js +30 -0
  53. package/dist/maplibre/create-ref.js.map +1 -0
  54. package/dist/maplibre/maplibre.d.ts +81 -0
  55. package/dist/maplibre/maplibre.js +387 -0
  56. package/dist/maplibre/maplibre.js.map +1 -0
  57. package/dist/styles/index.d.ts +4 -0
  58. package/dist/styles/react-bkoi-gl.css +1 -1
  59. package/dist/styles.d.js +2 -0
  60. package/dist/styles.d.js.map +1 -0
  61. package/dist/tsconfig.build.tsbuildinfo +1 -0
  62. package/dist/types/common.d.ts +2 -60
  63. package/dist/types/common.js.map +1 -1
  64. package/dist/types/events.d.ts +64 -23
  65. package/dist/types/events.js.map +1 -1
  66. package/dist/types/internal.d.ts +14 -0
  67. package/dist/types/internal.js +2 -0
  68. package/dist/types/internal.js.map +1 -0
  69. package/dist/types/lib.d.ts +16 -200
  70. package/dist/types/lib.js.map +1 -1
  71. package/dist/types/style-spec.d.ts +1 -0
  72. package/dist/types/style-spec.js +2 -0
  73. package/dist/types/style-spec.js.map +1 -0
  74. package/dist/utils/apply-react-style.d.ts +1 -1
  75. package/dist/utils/apply-react-style.js.map +1 -1
  76. package/dist/utils/compare-class-names.d.ts +2 -0
  77. package/dist/utils/compare-class-names.js +23 -0
  78. package/dist/utils/compare-class-names.js.map +1 -0
  79. package/dist/utils/deep-equal.d.ts +1 -1
  80. package/dist/utils/deep-equal.js +1 -1
  81. package/dist/utils/deep-equal.js.map +1 -1
  82. package/dist/utils/set-globals.d.ts +6 -8
  83. package/dist/utils/set-globals.js +22 -10
  84. package/dist/utils/set-globals.js.map +1 -1
  85. package/dist/utils/style-utils.d.ts +3 -2
  86. package/dist/utils/style-utils.js +4 -4
  87. package/dist/utils/style-utils.js.map +1 -1
  88. package/dist/utils/transform.d.ts +11 -19
  89. package/dist/utils/transform.js +13 -36
  90. package/dist/utils/transform.js.map +1 -1
  91. package/dist/utils/use-isomorphic-layout-effect.d.ts +1 -1
  92. package/dist/utils/use-isomorphic-layout-effect.js +2 -2
  93. package/dist/utils/use-isomorphic-layout-effect.js.map +1 -1
  94. package/package.json +42 -26
  95. package/src/components/attribution-control.ts +40 -16
  96. package/src/components/fullscreen-control.ts +40 -0
  97. package/src/components/geolocate-control.ts +45 -39
  98. package/src/components/layer.ts +37 -29
  99. package/src/components/logo-control.ts +44 -0
  100. package/src/components/map.tsx +86 -75
  101. package/src/components/marker.ts +141 -123
  102. package/src/components/navigation-control.ts +11 -12
  103. package/src/components/popup.ts +94 -97
  104. package/src/components/scale-control.ts +12 -14
  105. package/src/components/source.ts +58 -57
  106. package/src/components/terrain-control.ts +29 -0
  107. package/src/components/use-control.ts +21 -17
  108. package/src/components/use-map.tsx +26 -24
  109. package/src/exports-maplibre-gl.ts +37 -0
  110. package/src/index.ts +3 -2
  111. package/src/maplibre/create-ref.ts +70 -0
  112. package/src/maplibre/maplibre.ts +624 -0
  113. package/src/styles.d.ts +4 -0
  114. package/src/types/common.ts +12 -72
  115. package/src/types/events.ts +102 -42
  116. package/src/types/internal.ts +26 -0
  117. package/src/types/lib.ts +65 -260
  118. package/src/types/style-spec.ts +34 -0
  119. package/src/utils/apply-react-style.ts +7 -3
  120. package/src/utils/compare-class-names.ts +29 -0
  121. package/src/utils/deep-equal.ts +2 -2
  122. package/src/utils/set-globals.ts +22 -28
  123. package/src/utils/style-utils.ts +18 -9
  124. package/src/utils/transform.ts +32 -60
  125. package/src/utils/use-isomorphic-layout-effect.ts +3 -2
  126. package/bkoi-gl/package.json +0 -6
  127. package/dist/exports-bkoi-gl.cjs +0 -1512
  128. package/dist/exports-bkoi-gl.d.ts +0 -89
  129. package/dist/exports-bkoi-gl.js +0 -36
  130. package/dist/exports-bkoi-gl.js.map +0 -1
  131. package/dist/exports-mapbox.d.ts +0 -139
  132. package/dist/exports-mapbox.js +0 -36
  133. package/dist/exports-mapbox.js.map +0 -1
  134. package/dist/mapbox/create-ref.js +0 -64
  135. package/dist/mapbox/create-ref.js.map +0 -1
  136. package/dist/mapbox/mapbox.d.ts +0 -81
  137. package/dist/mapbox/mapbox.js +0 -476
  138. package/dist/mapbox/mapbox.js.map +0 -1
  139. package/dist/types/events-bkoi-gl.d.ts +0 -49
  140. package/dist/types/events-bkoi-gl.js +0 -2
  141. package/dist/types/events-bkoi-gl.js.map +0 -1
  142. package/dist/types/events-mapbox.d.ts +0 -49
  143. package/dist/types/events-mapbox.js +0 -2
  144. package/dist/types/events-mapbox.js.map +0 -1
  145. package/dist/types/index.d.ts +0 -70
  146. package/dist/types/index.js +0 -5
  147. package/dist/types/index.js.map +0 -1
  148. package/dist/types/public.d.ts +0 -2
  149. package/dist/types/public.js +0 -2
  150. package/dist/types/public.js.map +0 -1
  151. package/dist/types/style-spec-bkoi-gl.d.ts +0 -9
  152. package/dist/types/style-spec-bkoi-gl.js +0 -2
  153. package/dist/types/style-spec-bkoi-gl.js.map +0 -1
  154. package/dist/types/style-spec-mapbox.d.ts +0 -7
  155. package/dist/types/style-spec-mapbox.js +0 -2
  156. package/dist/types/style-spec-mapbox.js.map +0 -1
  157. package/src/components/fullscreen-control.tsx +0 -37
  158. package/src/exports-bkoi-gl.ts +0 -156
  159. package/src/exports-mapbox.ts +0 -159
  160. package/src/mapbox/create-ref.ts +0 -118
  161. package/src/mapbox/mapbox.ts +0 -742
  162. package/src/types/events-bkoi-gl.ts +0 -78
  163. package/src/types/events-mapbox.ts +0 -76
  164. package/src/types/index.ts +0 -93
  165. package/src/types/public.ts +0 -9
  166. package/src/types/style-spec-bkoi-gl.ts +0 -81
  167. package/src/types/style-spec-mapbox.ts +0 -80
@@ -1,105 +1,111 @@
1
- import * as React from 'react';
2
- import {useState, useRef, useEffect, useContext, useMemo, useImperativeHandle} from 'react';
3
-
4
- import {MountedMapsContext} from './use-map';
5
- import Mapbox, {MapboxProps} from '../mapbox/mapbox';
6
- import createRef, {MapRef} from '../mapbox/create-ref';
7
-
8
- import type {CSSProperties} from 'react';
9
- import useIsomorphicLayoutEffect from '../utils/use-isomorphic-layout-effect';
10
- import setGlobals, {GlobalSettings} from '../utils/set-globals';
11
- import type {MapLib, MapInstance, MapStyle, Callbacks} from '../types';
12
-
13
- export type MapContextValue<MapT extends MapInstance = MapInstance> = {
14
- mapLib: MapLib<MapT>;
15
- map: MapRef<MapT>;
1
+ /* eslint-disable react/display-name */
2
+ /* eslint-disable react/no-unknown-property */
3
+ import * as React from "react";
4
+ import {
5
+ useState,
6
+ useRef,
7
+ useEffect,
8
+ useContext,
9
+ useMemo,
10
+ useImperativeHandle,
11
+ } from "react";
12
+
13
+ import { MountedMapsContext } from "./use-map";
14
+ import Maplibre, { MaplibreProps } from "../maplibre/maplibre";
15
+ import createRef, { MapRef } from "../maplibre/create-ref";
16
+
17
+ import type { CSSProperties } from "react";
18
+ import useIsomorphicLayoutEffect from "../utils/use-isomorphic-layout-effect";
19
+ import setGlobals, { GlobalSettings } from "../utils/set-globals";
20
+ import type { MapLib, MapOptions } from "../types/lib";
21
+ import { LogoControl } from "./logo-control";
22
+ import { AttributionControl } from "./attribution-control";
23
+
24
+ export type MapContextValue = {
25
+ mapLib: MapLib;
26
+ map: MapRef;
16
27
  };
17
28
 
18
29
  export const MapContext = React.createContext<MapContextValue>(null);
19
30
 
20
- type MapInitOptions<MapOptions> = Omit<
31
+ type MapInitOptions = Omit<
21
32
  MapOptions,
22
- 'style' | 'container' | 'bounds' | 'fitBoundsOptions' | 'center'
33
+ "style" | "container" | "bounds" | "fitBoundsOptions" | "center"
23
34
  >;
24
35
 
25
- export type MapProps<
26
- MapOptions,
27
- StyleT extends MapStyle,
28
- CallbacksT extends Callbacks,
29
- MapT extends MapInstance
30
- > = MapInitOptions<MapOptions> &
31
- MapboxProps<StyleT, CallbacksT> &
36
+ export type MapProps = MapInitOptions &
37
+ MaplibreProps &
32
38
  GlobalSettings & {
33
- mapLib?: MapLib<MapT> | Promise<MapLib<MapT>>;
39
+ mapLib?: MapLib | Promise<MapLib>;
34
40
  reuseMaps?: boolean;
35
41
  /** Map container id */
36
42
  id?: string;
37
43
  /** Map container CSS style */
38
44
  style?: CSSProperties;
39
- children?: any;
40
- };
41
-
42
- export default function Map<
43
- MapOptions,
44
- StyleT extends MapStyle,
45
- CallbacksT extends Callbacks,
46
- MapT extends MapInstance
47
- >(
48
- props: MapProps<MapOptions, StyleT, CallbacksT, MapT>,
49
- ref: React.Ref<MapRef<MapT>>,
50
- defaultLib: MapLib<MapT> | Promise<MapLib<MapT>>
51
- ) {
45
+ children?: React.ReactNode;
46
+ /** Show Barikoi logo (default: true) */
47
+ showBarikoiLogo?: boolean;
48
+ /** Show Attribution (default: true) */
49
+ showAttribution?: boolean;
50
+ } & React.RefAttributes<MapRef>;
51
+
52
+ function _Map(props: MapProps, ref: React.Ref<MapRef>) {
52
53
  const mountedMapsContext = useContext(MountedMapsContext);
53
- const [mapInstance, setMapInstance] = useState<Mapbox<StyleT, CallbacksT, MapT>>(null);
54
+ const [mapInstance, setMapInstance] = useState<Maplibre>(null);
54
55
  const containerRef = useRef();
55
56
 
56
- const {current: contextValue} = useRef<MapContextValue<MapT>>({mapLib: null, map: null});
57
+ const { current: contextValue } = useRef<MapContextValue>({
58
+ mapLib: null,
59
+ map: null,
60
+ });
57
61
 
58
62
  useEffect(() => {
59
63
  const mapLib = props.mapLib;
60
64
  let isMounted = true;
61
- let mapbox: Mapbox<StyleT, CallbacksT, MapT>;
65
+ let maplibre: Maplibre;
62
66
 
63
- Promise.resolve(mapLib || defaultLib)
64
- .then((module: MapLib<MapT> | {default: MapLib<MapT>}) => {
67
+ Promise.resolve(mapLib || import("maplibre-gl"))
68
+ .then((module: MapLib | { default: MapLib }) => {
65
69
  if (!isMounted) {
66
70
  return;
67
71
  }
68
72
  if (!module) {
69
- throw new Error('Invalid mapLib');
73
+ throw new Error("Invalid mapLib");
70
74
  }
71
- const mapboxgl = 'Map' in module ? module : module.default;
75
+ const mapboxgl = "Map" in module ? module : module.default;
72
76
  if (!mapboxgl.Map) {
73
- throw new Error('Invalid mapLib');
77
+ throw new Error("Invalid mapLib");
74
78
  }
75
79
 
76
- // workerUrl & workerClass may change the result of supported()
77
- // https://github.com/visgl/react-map-gl/discussions/2027
78
80
  setGlobals(mapboxgl, props);
79
- if (!mapboxgl.supported || mapboxgl.supported(props)) {
80
- if (props.reuseMaps) {
81
- mapbox = Mapbox.reuse(props, containerRef.current);
82
- }
83
- if (!mapbox) {
84
- mapbox = new Mapbox(mapboxgl.Map, props, containerRef.current);
85
- }
86
- contextValue.map = createRef(mapbox);
87
- contextValue.mapLib = mapboxgl;
88
-
89
- setMapInstance(mapbox);
90
- mountedMapsContext?.onMapMount(contextValue.map, props.id);
91
- } else {
92
- throw new Error('Map is not supported by this browser');
81
+ if (props.reuseMaps) {
82
+ maplibre = Maplibre.reuse(props, containerRef.current);
93
83
  }
84
+ if (!maplibre) {
85
+ maplibre = new Maplibre(
86
+ mapboxgl.Map,
87
+ {
88
+ ...props,
89
+ // @ts-ignore
90
+ attributionControl: false,
91
+ },
92
+ containerRef.current,
93
+ );
94
+ }
95
+ contextValue.map = createRef(maplibre);
96
+ contextValue.mapLib = mapboxgl;
97
+
98
+ setMapInstance(maplibre);
99
+ mountedMapsContext?.onMapMount(contextValue.map, props.id);
94
100
  })
95
- .catch(error => {
96
- const {onError} = props;
101
+ .catch((error) => {
102
+ const { onError } = props;
97
103
  if (onError) {
98
104
  onError({
99
- type: 'error',
105
+ type: "error",
100
106
  target: null,
101
107
  originalEvent: null,
102
- error
108
+ error,
103
109
  });
104
110
  } else {
105
111
  console.error(error); // eslint-disable-line
@@ -108,12 +114,12 @@ export default function Map<
108
114
 
109
115
  return () => {
110
116
  isMounted = false;
111
- if (mapbox) {
117
+ if (maplibre) {
112
118
  mountedMapsContext?.onMapUnmount(props.id);
113
119
  if (props.reuseMaps) {
114
- mapbox.recycle();
120
+ maplibre.recycle();
115
121
  } else {
116
- mapbox.destroy();
122
+ maplibre.destroy();
117
123
  }
118
124
  }
119
125
  };
@@ -129,16 +135,16 @@ export default function Map<
129
135
 
130
136
  const style: CSSProperties = useMemo(
131
137
  () => ({
132
- position: 'relative',
133
- width: '100%',
134
- height: '100%',
135
- ...props.style
138
+ position: "relative",
139
+ width: "100%",
140
+ height: "100%",
141
+ ...props.style,
136
142
  }),
137
- [props.style]
143
+ [props.style],
138
144
  );
139
145
 
140
146
  const CHILD_CONTAINER_STYLE = {
141
- height: '100%'
147
+ height: "100%",
142
148
  };
143
149
 
144
150
  return (
@@ -146,6 +152,9 @@ export default function Map<
146
152
  {mapInstance && (
147
153
  <MapContext.Provider value={contextValue}>
148
154
  <div mapboxgl-children="" style={CHILD_CONTAINER_STYLE}>
155
+ {/* Automatically include Barikoi Logo and Attribution controls */}
156
+ <LogoControl position="bottom-left" />
157
+ <AttributionControl position="bottom-right" />
149
158
  {props.children}
150
159
  </div>
151
160
  </MapContext.Provider>
@@ -153,3 +162,5 @@ export default function Map<
153
162
  </div>
154
163
  );
155
164
  }
165
+
166
+ export const Map: React.FC<MapProps> = React.forwardRef(_Map);
@@ -1,137 +1,155 @@
1
1
  /* global document */
2
- import * as React from 'react';
3
- import {createPortal} from 'react-dom';
4
- import {useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo} from 'react';
5
- import {applyReactStyle} from '../utils/apply-react-style';
6
-
7
- import type {MarkerEvent, MarkerDragEvent, PointLike, MarkerInstance} from '../types';
8
-
9
- import {MapContext} from './map';
10
- import {arePointsEqual} from '../utils/deep-equal';
11
-
12
- export type MarkerProps<OptionsT, MarkerT extends MarkerInstance> = OptionsT & {
2
+ import * as React from "react";
3
+ import { createPortal } from "react-dom";
4
+ import {
5
+ useImperativeHandle,
6
+ useEffect,
7
+ useMemo,
8
+ useRef,
9
+ useContext,
10
+ forwardRef,
11
+ memo,
12
+ } from "react";
13
+ import { applyReactStyle } from "../utils/apply-react-style";
14
+
15
+ import type {
16
+ Popup as PopupInstance,
17
+ Marker as MarkerInstance,
18
+ MarkerOptions,
19
+ } from "../types/lib";
20
+ import type { MarkerEvent, MarkerDragEvent } from "../types/events";
21
+
22
+ import { MapContext } from "./map";
23
+ import { arePointsEqual } from "../utils/deep-equal";
24
+ import { compareClassNames } from "../utils/compare-class-names";
25
+
26
+ export type MarkerProps = MarkerOptions & {
13
27
  /** Longitude of the anchor location */
14
28
  longitude: number;
15
29
  /** Latitude of the anchor location */
16
30
  latitude: number;
17
31
 
18
- // These types will be further constraint by OptionsT
19
- draggable?: boolean;
20
- offset?: PointLike;
21
- pitchAlignment?: string;
22
- rotation?: number;
23
- rotationAlignment?: string;
24
- popup?: any;
32
+ popup?: PopupInstance;
25
33
 
26
34
  /** CSS style override, applied to the control's container */
27
35
  style?: React.CSSProperties;
28
- onClick?: (e: MarkerEvent<MarkerT, MouseEvent>) => void;
29
- onDragStart?: (e: MarkerDragEvent<MarkerT>) => void;
30
- onDrag?: (e: MarkerDragEvent<MarkerT>) => void;
31
- onDragEnd?: (e: MarkerDragEvent<MarkerT>) => void;
36
+ onClick?: (e: MarkerEvent<MouseEvent>) => void;
37
+ onDragStart?: (e: MarkerDragEvent) => void;
38
+ onDrag?: (e: MarkerDragEvent) => void;
39
+ onDragEnd?: (e: MarkerDragEvent) => void;
32
40
  children?: React.ReactNode;
33
41
  };
34
42
 
35
43
  /* eslint-disable complexity,max-statements */
36
- function Marker<MarkerOptions, MarkerT extends MarkerInstance>(
37
- props: MarkerProps<MarkerOptions, MarkerT>,
38
- ref: React.Ref<MarkerT>
39
- ) {
40
- const {map, mapLib} = useContext(MapContext);
41
- const thisRef = useRef({props});
42
- thisRef.current.props = props;
43
-
44
- const marker: MarkerT = useMemo(() => {
45
- let hasChildren = false;
46
- React.Children.forEach(props.children, el => {
47
- if (el) {
48
- hasChildren = true;
49
- }
50
- });
51
- const options = {
52
- ...props,
53
- element: hasChildren ? document.createElement('div') : null
54
- };
55
-
56
- const mk = new mapLib.Marker(options) as MarkerT;
57
- mk.setLngLat([props.longitude, props.latitude]);
58
-
59
- mk.getElement().addEventListener('click', (e: MouseEvent) => {
60
- thisRef.current.props.onClick?.({
61
- type: 'click',
62
- target: mk,
63
- originalEvent: e
44
+ export const Marker: React.FC<MarkerProps> = memo(
45
+ forwardRef((props: MarkerProps, ref: React.Ref<MarkerInstance>) => {
46
+ const { map, mapLib } = useContext(MapContext);
47
+ const thisRef = useRef({ props });
48
+
49
+ const marker: MarkerInstance = useMemo(() => {
50
+ let hasChildren = false;
51
+ React.Children.forEach(props.children, (el) => {
52
+ if (el) {
53
+ hasChildren = true;
54
+ }
55
+ });
56
+ const options = {
57
+ ...props,
58
+ element: hasChildren ? document.createElement("div") : undefined,
59
+ };
60
+
61
+ const mk = new mapLib.Marker(options);
62
+ mk.setLngLat([props.longitude, props.latitude]);
63
+
64
+ mk.getElement().addEventListener("click", (e: MouseEvent) => {
65
+ thisRef.current.props.onClick?.({
66
+ type: "click",
67
+ target: mk,
68
+ originalEvent: e,
69
+ });
70
+ });
71
+
72
+ mk.on("dragstart", (e) => {
73
+ const evt = e as MarkerDragEvent;
74
+ evt.lngLat = marker.getLngLat();
75
+ thisRef.current.props.onDragStart?.(evt);
76
+ });
77
+ mk.on("drag", (e) => {
78
+ const evt = e as MarkerDragEvent;
79
+ evt.lngLat = marker.getLngLat();
80
+ thisRef.current.props.onDrag?.(evt);
81
+ });
82
+ mk.on("dragend", (e) => {
83
+ const evt = e as MarkerDragEvent;
84
+ evt.lngLat = marker.getLngLat();
85
+ thisRef.current.props.onDragEnd?.(evt);
64
86
  });
65
- });
66
-
67
- mk.on('dragstart', e => {
68
- const evt = e as MarkerDragEvent<MarkerT>;
69
- evt.lngLat = marker.getLngLat();
70
- thisRef.current.props.onDragStart?.(evt);
71
- });
72
- mk.on('drag', e => {
73
- const evt = e as MarkerDragEvent<MarkerT>;
74
- evt.lngLat = marker.getLngLat();
75
- thisRef.current.props.onDrag?.(evt);
76
- });
77
- mk.on('dragend', e => {
78
- const evt = e as MarkerDragEvent<MarkerT>;
79
- evt.lngLat = marker.getLngLat();
80
- thisRef.current.props.onDragEnd?.(evt);
81
- });
82
-
83
- return mk;
84
- }, []);
85
-
86
- useEffect(() => {
87
- marker.addTo(map.getMap());
88
-
89
- return () => {
90
- marker.remove();
91
- };
92
- }, []);
93
-
94
- const {
95
- longitude,
96
- latitude,
97
- offset,
98
- style,
99
- draggable = false,
100
- popup = null,
101
- rotation = 0,
102
- rotationAlignment = 'auto',
103
- pitchAlignment = 'auto'
104
- } = props;
105
-
106
- useEffect(() => {
107
- applyReactStyle(marker.getElement(), style);
108
- }, [style]);
109
-
110
- useImperativeHandle(ref, () => marker, []);
111
-
112
- if (marker.getLngLat().lng !== longitude || marker.getLngLat().lat !== latitude) {
113
- marker.setLngLat([longitude, latitude]);
114
- }
115
- if (offset && !arePointsEqual(marker.getOffset(), offset)) {
116
- marker.setOffset(offset);
117
- }
118
- if (marker.isDraggable() !== draggable) {
119
- marker.setDraggable(draggable);
120
- }
121
- if (marker.getRotation() !== rotation) {
122
- marker.setRotation(rotation);
123
- }
124
- if (marker.getRotationAlignment() !== rotationAlignment) {
125
- marker.setRotationAlignment(rotationAlignment);
126
- }
127
- if (marker.getPitchAlignment() !== pitchAlignment) {
128
- marker.setPitchAlignment(pitchAlignment);
129
- }
130
- if (marker.getPopup() !== popup) {
131
- marker.setPopup(popup);
132
- }
133
-
134
- return createPortal(props.children, marker.getElement());
135
- }
136
-
137
- export default memo(forwardRef(Marker));
87
+
88
+ return mk;
89
+ }, []);
90
+
91
+ useEffect(() => {
92
+ marker.addTo(map.getMap());
93
+
94
+ return () => {
95
+ marker.remove();
96
+ };
97
+ }, []);
98
+
99
+ const {
100
+ longitude,
101
+ latitude,
102
+ offset,
103
+ style,
104
+ draggable = false,
105
+ popup = null,
106
+ rotation = 0,
107
+ rotationAlignment = "auto",
108
+ pitchAlignment = "auto",
109
+ } = props;
110
+
111
+ useEffect(() => {
112
+ applyReactStyle(marker.getElement(), style);
113
+ }, [style]);
114
+
115
+ useImperativeHandle(ref, () => marker, []);
116
+
117
+ const oldProps = thisRef.current.props;
118
+ if (
119
+ marker.getLngLat().lng !== longitude ||
120
+ marker.getLngLat().lat !== latitude
121
+ ) {
122
+ marker.setLngLat([longitude, latitude]);
123
+ }
124
+ if (offset && !arePointsEqual(marker.getOffset(), offset)) {
125
+ marker.setOffset(offset);
126
+ }
127
+ if (marker.isDraggable() !== draggable) {
128
+ marker.setDraggable(draggable);
129
+ }
130
+ if (marker.getRotation() !== rotation) {
131
+ marker.setRotation(rotation);
132
+ }
133
+ if (marker.getRotationAlignment() !== rotationAlignment) {
134
+ marker.setRotationAlignment(rotationAlignment);
135
+ }
136
+ if (marker.getPitchAlignment() !== pitchAlignment) {
137
+ marker.setPitchAlignment(pitchAlignment);
138
+ }
139
+ if (marker.getPopup() !== popup) {
140
+ marker.setPopup(popup);
141
+ }
142
+ const classNameDiff = compareClassNames(
143
+ oldProps.className,
144
+ props.className,
145
+ );
146
+ if (classNameDiff) {
147
+ for (const c of classNameDiff) {
148
+ marker.toggleClassName(c);
149
+ }
150
+ }
151
+
152
+ thisRef.current.props = props;
153
+ return createPortal(props.children, marker.getElement());
154
+ }),
155
+ );
@@ -1,22 +1,20 @@
1
- import * as React from 'react';
2
- import {useEffect, memo} from 'react';
3
- import {applyReactStyle} from '../utils/apply-react-style';
4
- import useControl from './use-control';
1
+ import * as React from "react";
2
+ import { useEffect, memo } from "react";
3
+ import { applyReactStyle } from "../utils/apply-react-style";
4
+ import { useControl } from "./use-control";
5
5
 
6
- import type {ControlPosition, NavigationControlInstance} from '../types';
6
+ import type { ControlPosition, NavigationControlOptions } from "../types/lib";
7
7
 
8
- export type NavigationControlProps<OptionsT> = OptionsT & {
8
+ export type NavigationControlProps = NavigationControlOptions & {
9
9
  /** Placement of the control relative to the map. */
10
10
  position?: ControlPosition;
11
11
  /** CSS style override, applied to the control's container */
12
12
  style?: React.CSSProperties;
13
13
  };
14
14
 
15
- function NavigationControl<NavigationControlOptions, ControlT extends NavigationControlInstance>(
16
- props: NavigationControlProps<NavigationControlOptions>
17
- ): null {
18
- const ctrl = useControl<ControlT>(({mapLib}) => new mapLib.NavigationControl(props) as ControlT, {
19
- position: props.position
15
+ function _NavigationControl(props: NavigationControlProps) {
16
+ const ctrl = useControl(({ mapLib }) => new mapLib.NavigationControl(props), {
17
+ position: props.position,
20
18
  });
21
19
 
22
20
  useEffect(() => {
@@ -26,4 +24,5 @@ function NavigationControl<NavigationControlOptions, ControlT extends Navigation
26
24
  return null;
27
25
  }
28
26
 
29
- export default memo(NavigationControl);
27
+ export const NavigationControl: React.FC<NavigationControlProps> =
28
+ memo(_NavigationControl);