react-bkoi-gl 1.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 (138) hide show
  1. package/README.md +96 -0
  2. package/bkoi-gl/package.json +6 -0
  3. package/dist/components/attribution-control.d.ts +11 -0
  4. package/dist/components/attribution-control.js +19 -0
  5. package/dist/components/attribution-control.js.map +1 -0
  6. package/dist/components/fullscreen-control.d.ts +14 -0
  7. package/dist/components/fullscreen-control.js +21 -0
  8. package/dist/components/fullscreen-control.js.map +1 -0
  9. package/dist/components/geolocate-control.d.ts +37 -0
  10. package/dist/components/geolocate-control.js +51 -0
  11. package/dist/components/geolocate-control.js.map +1 -0
  12. package/dist/components/layer.d.ts +17 -0
  13. package/dist/components/layer.js +99 -0
  14. package/dist/components/layer.js.map +1 -0
  15. package/dist/components/map.d.ts +23 -0
  16. package/dist/components/map.js +102 -0
  17. package/dist/components/map.js.map +1 -0
  18. package/dist/components/marker.d.ts +41 -0
  19. package/dist/components/marker.js +102 -0
  20. package/dist/components/marker.js.map +1 -0
  21. package/dist/components/navigation-control.d.ts +11 -0
  22. package/dist/components/navigation-control.js +19 -0
  23. package/dist/components/navigation-control.js.map +1 -0
  24. package/dist/components/popup.d.ts +33 -0
  25. package/dist/components/popup.js +81 -0
  26. package/dist/components/popup.js.map +1 -0
  27. package/dist/components/scale-control.d.ts +13 -0
  28. package/dist/components/scale-control.js +31 -0
  29. package/dist/components/scale-control.js.map +1 -0
  30. package/dist/components/source.d.ts +7 -0
  31. package/dist/components/source.js +98 -0
  32. package/dist/components/source.js.map +1 -0
  33. package/dist/components/use-control.d.ts +9 -0
  34. package/dist/components/use-control.js +31 -0
  35. package/dist/components/use-control.js.map +1 -0
  36. package/dist/components/use-map.d.ts +20 -0
  37. package/dist/components/use-map.js +55 -0
  38. package/dist/components/use-map.js.map +1 -0
  39. package/dist/exports-bkoi-gl.cjs +1512 -0
  40. package/dist/exports-bkoi-gl.d.ts +89 -0
  41. package/dist/exports-bkoi-gl.js +36 -0
  42. package/dist/exports-bkoi-gl.js.map +1 -0
  43. package/dist/exports-mapbox.d.ts +139 -0
  44. package/dist/exports-mapbox.js +36 -0
  45. package/dist/exports-mapbox.js.map +1 -0
  46. package/dist/index.cjs +1514 -0
  47. package/dist/index.d.ts +2 -0
  48. package/dist/index.js +3 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/mapbox/create-ref.d.ts +9 -0
  51. package/dist/mapbox/create-ref.js +64 -0
  52. package/dist/mapbox/create-ref.js.map +1 -0
  53. package/dist/mapbox/mapbox.d.ts +81 -0
  54. package/dist/mapbox/mapbox.js +477 -0
  55. package/dist/mapbox/mapbox.js.map +1 -0
  56. package/dist/styles/react-bkoi-gl.css +1 -0
  57. package/dist/types/common.d.ts +78 -0
  58. package/dist/types/common.js +2 -0
  59. package/dist/types/common.js.map +1 -0
  60. package/dist/types/events-bkoi-gl.d.ts +49 -0
  61. package/dist/types/events-bkoi-gl.js +2 -0
  62. package/dist/types/events-bkoi-gl.js.map +1 -0
  63. package/dist/types/events-mapbox.d.ts +49 -0
  64. package/dist/types/events-mapbox.js +2 -0
  65. package/dist/types/events-mapbox.js.map +1 -0
  66. package/dist/types/events.d.ts +39 -0
  67. package/dist/types/events.js +2 -0
  68. package/dist/types/events.js.map +1 -0
  69. package/dist/types/index.d.ts +70 -0
  70. package/dist/types/index.js +5 -0
  71. package/dist/types/index.js.map +1 -0
  72. package/dist/types/lib.d.ts +224 -0
  73. package/dist/types/lib.js +2 -0
  74. package/dist/types/lib.js.map +1 -0
  75. package/dist/types/public.d.ts +2 -0
  76. package/dist/types/public.js +2 -0
  77. package/dist/types/public.js.map +1 -0
  78. package/dist/types/style-spec-bkoi-gl.d.ts +9 -0
  79. package/dist/types/style-spec-bkoi-gl.js +2 -0
  80. package/dist/types/style-spec-bkoi-gl.js.map +1 -0
  81. package/dist/types/style-spec-mapbox.d.ts +7 -0
  82. package/dist/types/style-spec-mapbox.js +2 -0
  83. package/dist/types/style-spec-mapbox.js.map +1 -0
  84. package/dist/utils/apply-react-style.d.ts +2 -0
  85. package/dist/utils/apply-react-style.js +16 -0
  86. package/dist/utils/apply-react-style.js.map +1 -0
  87. package/dist/utils/assert.d.ts +1 -0
  88. package/dist/utils/assert.js +6 -0
  89. package/dist/utils/assert.js.map +1 -0
  90. package/dist/utils/deep-equal.d.ts +15 -0
  91. package/dist/utils/deep-equal.js +46 -0
  92. package/dist/utils/deep-equal.js.map +1 -0
  93. package/dist/utils/set-globals.d.ts +22 -0
  94. package/dist/utils/set-globals.js +19 -0
  95. package/dist/utils/set-globals.js.map +1 -0
  96. package/dist/utils/style-utils.d.ts +2 -0
  97. package/dist/utils/style-utils.js +42 -0
  98. package/dist/utils/style-utils.js.map +1 -0
  99. package/dist/utils/transform.d.ts +26 -0
  100. package/dist/utils/transform.js +56 -0
  101. package/dist/utils/transform.js.map +1 -0
  102. package/dist/utils/use-isomorphic-layout-effect.d.ts +3 -0
  103. package/dist/utils/use-isomorphic-layout-effect.js +4 -0
  104. package/dist/utils/use-isomorphic-layout-effect.js.map +1 -0
  105. package/package.json +78 -0
  106. package/src/components/attribution-control.ts +32 -0
  107. package/src/components/fullscreen-control.tsx +37 -0
  108. package/src/components/geolocate-control.ts +90 -0
  109. package/src/components/layer.ts +133 -0
  110. package/src/components/map.tsx +155 -0
  111. package/src/components/marker.ts +137 -0
  112. package/src/components/navigation-control.ts +29 -0
  113. package/src/components/popup.ts +116 -0
  114. package/src/components/scale-control.ts +46 -0
  115. package/src/components/source.ts +158 -0
  116. package/src/components/use-control.ts +64 -0
  117. package/src/components/use-map.tsx +69 -0
  118. package/src/exports-bkoi-gl.ts +156 -0
  119. package/src/exports-mapbox.ts +159 -0
  120. package/src/index.ts +2 -0
  121. package/src/mapbox/create-ref.ts +118 -0
  122. package/src/mapbox/mapbox.ts +742 -0
  123. package/src/types/common.ts +94 -0
  124. package/src/types/events-bkoi-gl.ts +78 -0
  125. package/src/types/events-mapbox.ts +76 -0
  126. package/src/types/events.ts +73 -0
  127. package/src/types/index.ts +93 -0
  128. package/src/types/lib.ts +279 -0
  129. package/src/types/public.ts +9 -0
  130. package/src/types/style-spec-bkoi-gl.ts +81 -0
  131. package/src/types/style-spec-mapbox.ts +80 -0
  132. package/src/utils/apply-react-style.ts +20 -0
  133. package/src/utils/assert.ts +5 -0
  134. package/src/utils/deep-equal.ts +61 -0
  135. package/src/utils/set-globals.ts +57 -0
  136. package/src/utils/style-utils.ts +59 -0
  137. package/src/utils/transform.ts +86 -0
  138. package/src/utils/use-isomorphic-layout-effect.ts +7 -0
@@ -0,0 +1,102 @@
1
+ import * as React from 'react';
2
+ import { createPortal } from 'react-dom';
3
+ import { useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo } from 'react';
4
+ import { applyReactStyle } from "../utils/apply-react-style.js";
5
+ import { MapContext } from "./map.js";
6
+ import { arePointsEqual } from "../utils/deep-equal.js";
7
+ function Marker(props, ref) {
8
+ const {
9
+ map,
10
+ mapLib
11
+ } = useContext(MapContext);
12
+ const thisRef = useRef({
13
+ props
14
+ });
15
+ thisRef.current.props = props;
16
+ const marker = useMemo(() => {
17
+ let hasChildren = false;
18
+ React.Children.forEach(props.children, el => {
19
+ if (el) {
20
+ hasChildren = true;
21
+ }
22
+ });
23
+ const options = {
24
+ ...props,
25
+ element: hasChildren ? document.createElement('div') : null
26
+ };
27
+ const mk = new mapLib.Marker(options);
28
+ mk.setLngLat([props.longitude, props.latitude]);
29
+ mk.getElement().addEventListener('click', e => {
30
+ var _thisRef$current$prop, _thisRef$current$prop2;
31
+ (_thisRef$current$prop = (_thisRef$current$prop2 = thisRef.current.props).onClick) === null || _thisRef$current$prop === void 0 ? void 0 : _thisRef$current$prop.call(_thisRef$current$prop2, {
32
+ type: 'click',
33
+ target: mk,
34
+ originalEvent: e
35
+ });
36
+ });
37
+ mk.on('dragstart', e => {
38
+ var _thisRef$current$prop3, _thisRef$current$prop4;
39
+ const evt = e;
40
+ evt.lngLat = marker.getLngLat();
41
+ (_thisRef$current$prop3 = (_thisRef$current$prop4 = thisRef.current.props).onDragStart) === null || _thisRef$current$prop3 === void 0 ? void 0 : _thisRef$current$prop3.call(_thisRef$current$prop4, evt);
42
+ });
43
+ mk.on('drag', e => {
44
+ var _thisRef$current$prop5, _thisRef$current$prop6;
45
+ const evt = e;
46
+ evt.lngLat = marker.getLngLat();
47
+ (_thisRef$current$prop5 = (_thisRef$current$prop6 = thisRef.current.props).onDrag) === null || _thisRef$current$prop5 === void 0 ? void 0 : _thisRef$current$prop5.call(_thisRef$current$prop6, evt);
48
+ });
49
+ mk.on('dragend', e => {
50
+ var _thisRef$current$prop7, _thisRef$current$prop8;
51
+ const evt = e;
52
+ evt.lngLat = marker.getLngLat();
53
+ (_thisRef$current$prop7 = (_thisRef$current$prop8 = thisRef.current.props).onDragEnd) === null || _thisRef$current$prop7 === void 0 ? void 0 : _thisRef$current$prop7.call(_thisRef$current$prop8, evt);
54
+ });
55
+ return mk;
56
+ }, []);
57
+ useEffect(() => {
58
+ marker.addTo(map.getMap());
59
+ return () => {
60
+ marker.remove();
61
+ };
62
+ }, []);
63
+ const {
64
+ longitude,
65
+ latitude,
66
+ offset,
67
+ style,
68
+ draggable = false,
69
+ popup = null,
70
+ rotation = 0,
71
+ rotationAlignment = 'auto',
72
+ pitchAlignment = 'auto'
73
+ } = props;
74
+ useEffect(() => {
75
+ applyReactStyle(marker.getElement(), style);
76
+ }, [style]);
77
+ useImperativeHandle(ref, () => marker, []);
78
+ if (marker.getLngLat().lng !== longitude || marker.getLngLat().lat !== latitude) {
79
+ marker.setLngLat([longitude, latitude]);
80
+ }
81
+ if (offset && !arePointsEqual(marker.getOffset(), offset)) {
82
+ marker.setOffset(offset);
83
+ }
84
+ if (marker.isDraggable() !== draggable) {
85
+ marker.setDraggable(draggable);
86
+ }
87
+ if (marker.getRotation() !== rotation) {
88
+ marker.setRotation(rotation);
89
+ }
90
+ if (marker.getRotationAlignment() !== rotationAlignment) {
91
+ marker.setRotationAlignment(rotationAlignment);
92
+ }
93
+ if (marker.getPitchAlignment() !== pitchAlignment) {
94
+ marker.setPitchAlignment(pitchAlignment);
95
+ }
96
+ if (marker.getPopup() !== popup) {
97
+ marker.setPopup(popup);
98
+ }
99
+ return createPortal(props.children, marker.getElement());
100
+ }
101
+ export default memo(forwardRef(Marker));
102
+ //# sourceMappingURL=marker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marker.js","names":["React","createPortal","useImperativeHandle","useEffect","useMemo","useRef","useContext","forwardRef","memo","applyReactStyle","MapContext","arePointsEqual","Marker","props","ref","map","mapLib","thisRef","current","marker","hasChildren","Children","forEach","children","el","options","element","document","createElement","mk","setLngLat","longitude","latitude","getElement","addEventListener","e","_thisRef$current$prop","_thisRef$current$prop2","onClick","call","type","target","originalEvent","on","_thisRef$current$prop3","_thisRef$current$prop4","evt","lngLat","getLngLat","onDragStart","_thisRef$current$prop5","_thisRef$current$prop6","onDrag","_thisRef$current$prop7","_thisRef$current$prop8","onDragEnd","addTo","getMap","remove","offset","style","draggable","popup","rotation","rotationAlignment","pitchAlignment","lng","lat","getOffset","setOffset","isDraggable","setDraggable","getRotation","setRotation","getRotationAlignment","setRotationAlignment","getPitchAlignment","setPitchAlignment","getPopup","setPopup"],"sources":["../../src/components/marker.ts"],"sourcesContent":["/* global document */\nimport * as React from 'react';\nimport {createPortal} from 'react-dom';\nimport {useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo} from 'react';\nimport {applyReactStyle} from '../utils/apply-react-style';\n\nimport type {MarkerEvent, MarkerDragEvent, PointLike, MarkerInstance} from '../types';\n\nimport {MapContext} from './map';\nimport {arePointsEqual} from '../utils/deep-equal';\n\nexport type MarkerProps<OptionsT, MarkerT extends MarkerInstance> = OptionsT & {\n /** Longitude of the anchor location */\n longitude: number;\n /** Latitude of the anchor location */\n latitude: number;\n\n // These types will be further constraint by OptionsT\n draggable?: boolean;\n offset?: PointLike;\n pitchAlignment?: string;\n rotation?: number;\n rotationAlignment?: string;\n popup?: any;\n\n /** CSS style override, applied to the control's container */\n style?: React.CSSProperties;\n onClick?: (e: MarkerEvent<MarkerT, MouseEvent>) => void;\n onDragStart?: (e: MarkerDragEvent<MarkerT>) => void;\n onDrag?: (e: MarkerDragEvent<MarkerT>) => void;\n onDragEnd?: (e: MarkerDragEvent<MarkerT>) => void;\n children?: React.ReactNode;\n};\n\n/* eslint-disable complexity,max-statements */\nfunction Marker<MarkerOptions, MarkerT extends MarkerInstance>(\n props: MarkerProps<MarkerOptions, MarkerT>,\n ref: React.Ref<MarkerT>\n) {\n const {map, mapLib} = useContext(MapContext);\n const thisRef = useRef({props});\n thisRef.current.props = props;\n\n const marker: MarkerT = useMemo(() => {\n let hasChildren = false;\n React.Children.forEach(props.children, el => {\n if (el) {\n hasChildren = true;\n }\n });\n const options = {\n ...props,\n element: hasChildren ? document.createElement('div') : null\n };\n\n const mk = new mapLib.Marker(options) as MarkerT;\n mk.setLngLat([props.longitude, props.latitude]);\n\n mk.getElement().addEventListener('click', (e: MouseEvent) => {\n thisRef.current.props.onClick?.({\n type: 'click',\n target: mk,\n originalEvent: e\n });\n });\n\n mk.on('dragstart', e => {\n const evt = e as MarkerDragEvent<MarkerT>;\n evt.lngLat = marker.getLngLat();\n thisRef.current.props.onDragStart?.(evt);\n });\n mk.on('drag', e => {\n const evt = e as MarkerDragEvent<MarkerT>;\n evt.lngLat = marker.getLngLat();\n thisRef.current.props.onDrag?.(evt);\n });\n mk.on('dragend', e => {\n const evt = e as MarkerDragEvent<MarkerT>;\n evt.lngLat = marker.getLngLat();\n thisRef.current.props.onDragEnd?.(evt);\n });\n\n return mk;\n }, []);\n\n useEffect(() => {\n marker.addTo(map.getMap());\n\n return () => {\n marker.remove();\n };\n }, []);\n\n const {\n longitude,\n latitude,\n offset,\n style,\n draggable = false,\n popup = null,\n rotation = 0,\n rotationAlignment = 'auto',\n pitchAlignment = 'auto'\n } = props;\n\n useEffect(() => {\n applyReactStyle(marker.getElement(), style);\n }, [style]);\n\n useImperativeHandle(ref, () => marker, []);\n\n if (marker.getLngLat().lng !== longitude || marker.getLngLat().lat !== latitude) {\n marker.setLngLat([longitude, latitude]);\n }\n if (offset && !arePointsEqual(marker.getOffset(), offset)) {\n marker.setOffset(offset);\n }\n if (marker.isDraggable() !== draggable) {\n marker.setDraggable(draggable);\n }\n if (marker.getRotation() !== rotation) {\n marker.setRotation(rotation);\n }\n if (marker.getRotationAlignment() !== rotationAlignment) {\n marker.setRotationAlignment(rotationAlignment);\n }\n if (marker.getPitchAlignment() !== pitchAlignment) {\n marker.setPitchAlignment(pitchAlignment);\n }\n if (marker.getPopup() !== popup) {\n marker.setPopup(popup);\n }\n\n return createPortal(props.children, marker.getElement());\n}\n\nexport default memo(forwardRef(Marker));\n"],"mappings":"AACA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAAQC,YAAY,QAAO,WAAW;AACtC,SAAQC,mBAAmB,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,UAAU,EAAEC,UAAU,EAAEC,IAAI,QAAO,OAAO;AAAC,SAC5FC,eAAe;AAAA,SAIfC,UAAU;AAAA,SACVC,cAAc;AA0BtB,SAASC,MAAMA,CACbC,KAA0C,EAC1CC,GAAuB,EACvB;EACA,MAAM;IAACC,GAAG;IAAEC;EAAM,CAAC,GAAGV,UAAU,CAACI,UAAU,CAAC;EAC5C,MAAMO,OAAO,GAAGZ,MAAM,CAAC;IAACQ;EAAK,CAAC,CAAC;EAC/BI,OAAO,CAACC,OAAO,CAACL,KAAK,GAAGA,KAAK;EAE7B,MAAMM,MAAe,GAAGf,OAAO,CAAC,MAAM;IACpC,IAAIgB,WAAW,GAAG,KAAK;IACvBpB,KAAK,CAACqB,QAAQ,CAACC,OAAO,CAACT,KAAK,CAACU,QAAQ,EAAEC,EAAE,IAAI;MAC3C,IAAIA,EAAE,EAAE;QACNJ,WAAW,GAAG,IAAI;MACpB;IACF,CAAC,CAAC;IACF,MAAMK,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AAIA,MAAM,CAAC6B,SAAS,CAAC,CAAC,CAACkB,GAAG,KAAKnC,SAAS,IAAIZ,MAAM,CAAC6B,SAAS,CAAC,CAAC,CAACmB,GAAG,KAAKnC,QAAQ,EAAE;IAC/Eb,MAAM,CAACW,SAAS,CAAC,CAACC,SAAS,EAAEC,QAAQ,CAAC,CAAC;EACzC;EACA,IAAI2B,MAAM,IAAI,CAAChD,cAAc,CAACQ,MAAM,CAACiD,SAAS,CAAC,CAAC,EAAET,MAAM,CAAC,EAAE;IACzDxC,MAAM,CAACkD,SAAS,CAACV,MAAM,CAAC;EAC1B;EACA,IAAIxC,MAAM,CAACmD,WAAW,CAAC,CAAC,KAAKT,SAAS,EAAE;IACtC1C,MAAM,CAACoD,YAAY,CAACV,SAAS,CAAC;EAChC;EACA,IAAI1C,MAAM,CAACqD,WAAW,CAAC,CAAC,KAAKT,QAAQ,EAAE;IACrC5C,MAAM,CAACsD,WAAW,CAACV,QAAQ,CAAC;EAC9B;EACA,IAAI5C,MAAM,CAACuD,oBAAoB,CAAC,CAAC,KAAKV,iBAAiB,EAAE;IACvD7C,MAAM,CAACwD,oBAAoB,CAACX,iBAAiB,CAAC;EAChD;EACA,IAAI7C,MAAM,CAACyD,iBAAiB,CAAC,CAAC,KAAKX,cAAc,EAAE;IACjD9C,MAAM,CAAC0D,iBAAiB,CAACZ,cAAc,CAAC;EAC1C;EACA,IAAI9C,MAAM,CAAC2D,QAAQ,CAAC,CAAC,KAAKhB,KAAK,EAAE;IAC/B3C,MAAM,CAAC4D,QAAQ,CAACjB,KAAK,CAAC;EACxB;EAEA,OAAO7D,YAAY,CAACY,KAAK,CAACU,QAAQ,EAAEJ,MAAM,CAACc,UAAU,CAAC,CAAC,CAAC;AAC1D;AAEA,eAAezB,IAAI,CAACD,UAAU,CAACK,MAAM,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,11 @@
1
+ import * as React from 'react';
2
+ import type { ControlPosition, NavigationControlInstance } from '../types';
3
+ export declare type NavigationControlProps<OptionsT> = OptionsT & {
4
+ /** Placement of the control relative to the map. */
5
+ position?: ControlPosition;
6
+ /** CSS style override, applied to the control's container */
7
+ style?: React.CSSProperties;
8
+ };
9
+ declare function NavigationControl<NavigationControlOptions, ControlT extends NavigationControlInstance>(props: NavigationControlProps<NavigationControlOptions>): null;
10
+ declare const _default: React.MemoExoticComponent<typeof NavigationControl>;
11
+ export default _default;
@@ -0,0 +1,19 @@
1
+ import { useEffect, memo } from 'react';
2
+ import { applyReactStyle } from "../utils/apply-react-style.js";
3
+ import useControl from "./use-control.js";
4
+ function NavigationControl(props) {
5
+ const ctrl = useControl(_ref => {
6
+ let {
7
+ mapLib
8
+ } = _ref;
9
+ return new mapLib.NavigationControl(props);
10
+ }, {
11
+ position: props.position
12
+ });
13
+ useEffect(() => {
14
+ applyReactStyle(ctrl._container, props.style);
15
+ }, [props.style]);
16
+ return null;
17
+ }
18
+ export default memo(NavigationControl);
19
+ //# sourceMappingURL=navigation-control.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"navigation-control.js","names":["useEffect","memo","applyReactStyle","useControl","NavigationControl","props","ctrl","_ref","mapLib","position","_container","style"],"sources":["../../src/components/navigation-control.ts"],"sourcesContent":["import * as React from 'react';\nimport {useEffect, memo} from 'react';\nimport {applyReactStyle} from '../utils/apply-react-style';\nimport useControl from './use-control';\n\nimport type {ControlPosition, NavigationControlInstance} from '../types';\n\nexport type NavigationControlProps<OptionsT> = OptionsT & {\n /** Placement of the control relative to the map. */\n position?: ControlPosition;\n /** CSS style override, applied to the control's container */\n style?: React.CSSProperties;\n};\n\nfunction NavigationControl<NavigationControlOptions, ControlT extends NavigationControlInstance>(\n props: NavigationControlProps<NavigationControlOptions>\n): null {\n const ctrl = useControl<ControlT>(({mapLib}) => new mapLib.NavigationControl(props) as ControlT, {\n position: props.position\n });\n\n useEffect(() => {\n applyReactStyle(ctrl._container, props.style);\n }, [props.style]);\n\n return null;\n}\n\nexport default memo(NavigationControl);\n"],"mappings":"AACA,SAAQA,SAAS,EAAEC,IAAI,QAAO,OAAO;AAAC,SAC9BC,eAAe;AAAA,OAChBC,UAAU;AAWjB,SAASC,iBAAiBA,CACxBC,KAAuD,EACjD;EACN,MAAMC,IAAI,GAAGH,UAAU,CAAWI,IAAA;IAAA,IAAC;MAACC;IAAM,CAAC,GAAAD,IAAA;IAAA,OAAK,IAAIC,MAAM,CAACJ,iBAAiB,CAACC,KAAK,CAAC;EAAA,CAAY,EAAE;IAC/FI,QAAQ,EAAEJ,KAAK,CAACI;EAClB,CAAC,CAAC;EAEFT,SAAS,CAAC,MAAM;IACdE,eAAe,CAACI,IAAI,CAACI,UAAU,EAAEL,KAAK,CAACM,KAAK,CAAC;EAC/C,CAAC,EAAE,CAACN,KAAK,CAACM,KAAK,CAAC,CAAC;EAEjB,OAAO,IAAI;AACb;AAEA,eAAeV,IAAI,CAACG,iBAAiB,CAAC","ignoreList":[]}
@@ -0,0 +1,33 @@
1
+ import * as React from 'react';
2
+ import type { PopupEvent, PopupInstance } from '../types';
3
+ export declare type PopupProps<OptionsT, PopupT extends PopupInstance> = OptionsT & {
4
+ /** Longitude of the anchor location */
5
+ longitude: number;
6
+ /** Latitude of the anchor location */
7
+ latitude: number;
8
+ anchor?: string;
9
+ offset?: any;
10
+ className?: string;
11
+ maxWidth?: string;
12
+ /** CSS style override, applied to the control's container */
13
+ style?: React.CSSProperties;
14
+ onOpen?: (e: PopupEvent<PopupT>) => void;
15
+ onClose?: (e: PopupEvent<PopupT>) => void;
16
+ children?: React.ReactNode;
17
+ };
18
+ declare const _default: React.MemoExoticComponent<React.ForwardRefExoticComponent<{
19
+ /** Longitude of the anchor location */
20
+ longitude: number;
21
+ /** Latitude of the anchor location */
22
+ latitude: number;
23
+ anchor?: string;
24
+ offset?: any;
25
+ className?: string;
26
+ maxWidth?: string;
27
+ /** CSS style override, applied to the control's container */
28
+ style?: React.CSSProperties;
29
+ onOpen?: (e: PopupEvent<PopupInstance>) => void;
30
+ onClose?: (e: PopupEvent<PopupInstance>) => void;
31
+ children?: React.ReactNode;
32
+ } & React.RefAttributes<PopupInstance>>>;
33
+ export default _default;
@@ -0,0 +1,81 @@
1
+ import { createPortal } from 'react-dom';
2
+ import { useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo } from 'react';
3
+ import { applyReactStyle } from "../utils/apply-react-style.js";
4
+ import { MapContext } from "./map.js";
5
+ import { deepEqual } from "../utils/deep-equal.js";
6
+ function getClassList(className) {
7
+ return new Set(className ? className.trim().split(/\s+/) : []);
8
+ }
9
+ function Popup(props, ref) {
10
+ const {
11
+ map,
12
+ mapLib
13
+ } = useContext(MapContext);
14
+ const container = useMemo(() => {
15
+ return document.createElement('div');
16
+ }, []);
17
+ const thisRef = useRef({
18
+ props
19
+ });
20
+ thisRef.current.props = props;
21
+ const popup = useMemo(() => {
22
+ const options = {
23
+ ...props
24
+ };
25
+ const pp = new mapLib.Popup(options);
26
+ pp.setLngLat([props.longitude, props.latitude]);
27
+ pp.once('open', e => {
28
+ var _thisRef$current$prop, _thisRef$current$prop2;
29
+ (_thisRef$current$prop = (_thisRef$current$prop2 = thisRef.current.props).onOpen) === null || _thisRef$current$prop === void 0 ? void 0 : _thisRef$current$prop.call(_thisRef$current$prop2, e);
30
+ });
31
+ return pp;
32
+ }, []);
33
+ useEffect(() => {
34
+ const onClose = e => {
35
+ var _thisRef$current$prop3, _thisRef$current$prop4;
36
+ (_thisRef$current$prop3 = (_thisRef$current$prop4 = thisRef.current.props).onClose) === null || _thisRef$current$prop3 === void 0 ? void 0 : _thisRef$current$prop3.call(_thisRef$current$prop4, e);
37
+ };
38
+ popup.on('close', onClose);
39
+ popup.setDOMContent(container).addTo(map.getMap());
40
+ return () => {
41
+ popup.off('close', onClose);
42
+ if (popup.isOpen()) {
43
+ popup.remove();
44
+ }
45
+ };
46
+ }, []);
47
+ useEffect(() => {
48
+ applyReactStyle(popup.getElement(), props.style);
49
+ }, [props.style]);
50
+ useImperativeHandle(ref, () => popup, []);
51
+ if (popup.isOpen()) {
52
+ if (popup.getLngLat().lng !== props.longitude || popup.getLngLat().lat !== props.latitude) {
53
+ popup.setLngLat([props.longitude, props.latitude]);
54
+ }
55
+ if (props.offset && !deepEqual(popup.options.offset, props.offset)) {
56
+ popup.setOffset(props.offset);
57
+ }
58
+ if (popup.options.anchor !== props.anchor || popup.options.maxWidth !== props.maxWidth) {
59
+ popup.options.anchor = props.anchor;
60
+ popup.setMaxWidth(props.maxWidth);
61
+ }
62
+ if (popup.options.className !== props.className) {
63
+ const prevClassList = getClassList(popup.options.className);
64
+ const nextClassList = getClassList(props.className);
65
+ for (const c of prevClassList) {
66
+ if (!nextClassList.has(c)) {
67
+ popup.removeClassName(c);
68
+ }
69
+ }
70
+ for (const c of nextClassList) {
71
+ if (!prevClassList.has(c)) {
72
+ popup.addClassName(c);
73
+ }
74
+ }
75
+ popup.options.className = props.className;
76
+ }
77
+ }
78
+ return createPortal(props.children, container);
79
+ }
80
+ export default memo(forwardRef(Popup));
81
+ //# sourceMappingURL=popup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"popup.js","names":["createPortal","useImperativeHandle","useEffect","useMemo","useRef","useContext","forwardRef","memo","applyReactStyle","MapContext","deepEqual","getClassList","className","Set","trim","split","Popup","props","ref","map","mapLib","container","document","createElement","thisRef","current","popup","options","pp","setLngLat","longitude","latitude","once","e","_thisRef$current$prop","_thisRef$current$prop2","onOpen","call","onClose","_thisRef$current$prop3","_thisRef$current$prop4","on","setDOMContent","addTo","getMap","off","isOpen","remove","getElement","style","getLngLat","lng","lat","offset","setOffset","anchor","maxWidth","setMaxWidth","prevClassList","nextClassList","c","has","removeClassName","addClassName","children"],"sources":["../../src/components/popup.ts"],"sourcesContent":["/* global document */\nimport * as React from 'react';\nimport {createPortal} from 'react-dom';\nimport {useImperativeHandle, useEffect, useMemo, useRef, useContext, forwardRef, memo} from 'react';\nimport {applyReactStyle} from '../utils/apply-react-style';\n\nimport type {PopupEvent, PopupInstance} from '../types';\n\nimport {MapContext} from './map';\nimport {deepEqual} from '../utils/deep-equal';\n\nexport type PopupProps<OptionsT, PopupT extends PopupInstance> = OptionsT & {\n /** Longitude of the anchor location */\n longitude: number;\n /** Latitude of the anchor location */\n latitude: number;\n\n // These types will be further constraint by OptionsT\n anchor?: string;\n offset?: any;\n className?: string;\n maxWidth?: string;\n\n /** CSS style override, applied to the control's container */\n style?: React.CSSProperties;\n\n onOpen?: (e: PopupEvent<PopupT>) => void;\n onClose?: (e: PopupEvent<PopupT>) => void;\n children?: React.ReactNode;\n};\n\n// Adapted from https://github.com/mapbox/mapbox-gl-js/blob/v1.13.0/src/ui/popup.js\nfunction getClassList(className: string) {\n return new Set(className ? className.trim().split(/\\s+/) : []);\n}\n\n/* eslint-disable complexity,max-statements */\nfunction Popup<PopupOptions, PopupT extends PopupInstance>(\n props: PopupProps<PopupOptions, PopupT>,\n ref: React.Ref<PopupT>\n) {\n const {map, mapLib} = useContext(MapContext);\n const container = useMemo(() => {\n return document.createElement('div');\n }, []);\n const thisRef = useRef({props});\n thisRef.current.props = props;\n\n const popup: PopupT = useMemo(() => {\n const options = {...props};\n const pp = new mapLib.Popup(options) as PopupT;\n pp.setLngLat([props.longitude, props.latitude]);\n pp.once('open', e => {\n thisRef.current.props.onOpen?.(e as PopupEvent<PopupT>);\n });\n return pp;\n }, []);\n\n useEffect(() => {\n const onClose = e => {\n thisRef.current.props.onClose?.(e as PopupEvent<PopupT>);\n };\n popup.on('close', onClose);\n popup.setDOMContent(container).addTo(map.getMap());\n\n return () => {\n // https://github.com/visgl/react-map-gl/issues/1825\n // onClose should not be fired if the popup is removed by unmounting\n // When using React strict mode, the component is mounted twice.\n // Firing the onClose callback here would be a false signal to remove the component.\n popup.off('close', onClose);\n if (popup.isOpen()) {\n popup.remove();\n }\n };\n }, []);\n\n useEffect(() => {\n applyReactStyle(popup.getElement(), props.style);\n }, [props.style]);\n\n useImperativeHandle(ref, () => popup, []);\n\n if (popup.isOpen()) {\n if (popup.getLngLat().lng !== props.longitude || popup.getLngLat().lat !== props.latitude) {\n popup.setLngLat([props.longitude, props.latitude]);\n }\n if (props.offset && !deepEqual(popup.options.offset, props.offset)) {\n popup.setOffset(props.offset);\n }\n if (popup.options.anchor !== props.anchor || popup.options.maxWidth !== props.maxWidth) {\n popup.options.anchor = props.anchor;\n popup.setMaxWidth(props.maxWidth);\n }\n if (popup.options.className !== props.className) {\n const prevClassList = getClassList(popup.options.className);\n const nextClassList = getClassList(props.className);\n\n for (const c of prevClassList) {\n if (!nextClassList.has(c)) {\n popup.removeClassName(c);\n }\n }\n for (const c of nextClassList) {\n if (!prevClassList.has(c)) {\n popup.addClassName(c);\n }\n }\n popup.options.className = props.className;\n }\n }\n\n return createPortal(props.children, container);\n}\n\nexport default memo(forwardRef(Popup));\n"],"mappings":"AAEA,SAAQA,YAAY,QAAO,WAAW;AACtC,SAAQC,mBAAmB,EAAEC,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,UAAU,EAAEC,UAAU,EAAEC,IAAI,QAAO,OAAO;AAAC,SAC5FC,eAAe;AAAA,SAIfC,UAAU;AAAA,SACVC,SAAS;AAuBjB,SAASC,YAAYA,CAACC,SAAiB,EAAE;EACvC,OAAO,IAAIC,GAAG,CAACD,SAAS,GAAGA,SAAS,CAACE,IAAI,CAAC,CAAC,CAACC,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;AAChE;AAGA,SAASC,KAAKA,CACZC,KAAuC,EACvCC,GAAsB,EACtB;EACA,MAAM;IAACC,GAAG;IAAEC;EAAM,CAAC,GAAGf,UAAU,CAACI,UAAU,CAAC;EAC5C,MAAMY,SAAS,GAAGlB,OAAO,CAAC,MAAM;IAC9B,OAAOmB,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EACtC,CAAC,EAAE,EAAE,CAAC;EACN,MAAMC,OAAO,GAAGpB,MAAM,CAAC;IAACa;EAAK,CAAC,CAAC;EAC/BO,OAAO,CAACC,OAAO,CAACR,KAAK,GAA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AM+C,aAAa,GAAGhD,YAAY,CAACM,KAAK,CAACL,SAAS,CAAC;MAEnD,KAAK,MAAMgD,CAAC,IAAIF,aAAa,EAAE;QAC7B,IAAI,CAACC,aAAa,CAACE,GAAG,CAACD,CAAC,CAAC,EAAE;UACzBlC,KAAK,CAACoC,eAAe,CAACF,CAAC,CAAC;QAC1B;MACF;MACA,KAAK,MAAMA,CAAC,IAAID,aAAa,EAAE;QAC7B,IAAI,CAACD,aAAa,CAACG,GAAG,CAACD,CAAC,CAAC,EAAE;UACzBlC,KAAK,CAACqC,YAAY,CAACH,CAAC,CAAC;QACvB;MACF;MACAlC,KAAK,CAACC,OAAO,CAACf,SAAS,GAAGK,KAAK,CAACL,SAAS;IAC3C;EACF;EAEA,OAAOZ,YAAY,CAACiB,KAAK,CAAC+C,QAAQ,EAAE3C,SAAS,CAAC;AAChD;AAEA,eAAed,IAAI,CAACD,UAAU,CAACU,KAAK,CAAC,CAAC","ignoreList":[]}
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+ import type { ControlPosition, ScaleControlInstance } from '../types';
3
+ export declare type ScaleControlProps<OptionsT> = OptionsT & {
4
+ unit?: string;
5
+ maxWidth?: number;
6
+ /** Placement of the control relative to the map. */
7
+ position?: ControlPosition;
8
+ /** CSS style override, applied to the control's container */
9
+ style?: React.CSSProperties;
10
+ };
11
+ declare function ScaleControl<ScaleControlOptions, ControlT extends ScaleControlInstance>(props: ScaleControlProps<ScaleControlOptions>): null;
12
+ declare const _default: React.MemoExoticComponent<typeof ScaleControl>;
13
+ export default _default;
@@ -0,0 +1,31 @@
1
+ import { useEffect, useRef, memo } from 'react';
2
+ import { applyReactStyle } from "../utils/apply-react-style.js";
3
+ import useControl from "./use-control.js";
4
+ function ScaleControl(props) {
5
+ const ctrl = useControl(_ref => {
6
+ let {
7
+ mapLib
8
+ } = _ref;
9
+ return new mapLib.ScaleControl(props);
10
+ }, {
11
+ position: props.position
12
+ });
13
+ const propsRef = useRef(props);
14
+ const prevProps = propsRef.current;
15
+ propsRef.current = props;
16
+ const {
17
+ style
18
+ } = props;
19
+ if (props.maxWidth !== undefined && props.maxWidth !== prevProps.maxWidth) {
20
+ ctrl.options.maxWidth = props.maxWidth;
21
+ }
22
+ if (props.unit !== undefined && props.unit !== prevProps.unit) {
23
+ ctrl.setUnit(props.unit);
24
+ }
25
+ useEffect(() => {
26
+ applyReactStyle(ctrl._container, style);
27
+ }, [style]);
28
+ return null;
29
+ }
30
+ export default memo(ScaleControl);
31
+ //# sourceMappingURL=scale-control.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scale-control.js","names":["useEffect","useRef","memo","applyReactStyle","useControl","ScaleControl","props","ctrl","_ref","mapLib","position","propsRef","prevProps","current","style","maxWidth","undefined","options","unit","setUnit","_container"],"sources":["../../src/components/scale-control.ts"],"sourcesContent":["import * as React from 'react';\nimport {useEffect, useRef, memo} from 'react';\nimport {applyReactStyle} from '../utils/apply-react-style';\nimport useControl from './use-control';\n\nimport type {ControlPosition, ScaleControlInstance} from '../types';\n\nexport type ScaleControlProps<OptionsT> = OptionsT & {\n // These props will be further constraint by OptionsT\n unit?: string;\n maxWidth?: number;\n\n /** Placement of the control relative to the map. */\n position?: ControlPosition;\n /** CSS style override, applied to the control's container */\n style?: React.CSSProperties;\n};\n\nfunction ScaleControl<ScaleControlOptions, ControlT extends ScaleControlInstance>(\n props: ScaleControlProps<ScaleControlOptions>\n): null {\n const ctrl = useControl<ControlT>(({mapLib}) => new mapLib.ScaleControl(props) as ControlT, {\n position: props.position\n });\n const propsRef = useRef<ScaleControlProps<ScaleControlOptions>>(props);\n\n const prevProps = propsRef.current;\n propsRef.current = props;\n\n const {style} = props;\n\n if (props.maxWidth !== undefined && props.maxWidth !== prevProps.maxWidth) {\n ctrl.options.maxWidth = props.maxWidth;\n }\n if (props.unit !== undefined && props.unit !== prevProps.unit) {\n ctrl.setUnit(props.unit);\n }\n\n useEffect(() => {\n applyReactStyle(ctrl._container, style);\n }, [style]);\n\n return null;\n}\n\nexport default memo(ScaleControl);\n"],"mappings":"AACA,SAAQA,SAAS,EAAEC,MAAM,EAAEC,IAAI,QAAO,OAAO;AAAC,SACtCC,eAAe;AAAA,OAChBC,UAAU;AAejB,SAASC,YAAYA,CACnBC,KAA6C,EACvC;EACN,MAAMC,IAAI,GAAGH,UAAU,CAAWI,IAAA;IAAA,IAAC;MAACC;IAAM,CAAC,GAAAD,IAAA;IAAA,OAAK,IAAIC,MAAM,CAACJ,YAAY,CAACC,KAAK,CAAC;EAAA,CAAY,EAAE;IAC1FI,QAAQ,EAAEJ,KAAK,CAACI;EAClB,CAAC,CAAC;EACF,MAAMC,QAAQ,GAAGV,MAAM,CAAyCK,KAAK,CAAC;EAEtE,MAAMM,SAAS,GAAGD,QAAQ,CAACE,OAAO;EAClCF,QAAQ,CAACE,OAAO,GAAGP,KAAK;EAExB,MAAM;IAACQ;EAAK,CAAC,GAAGR,KAAK;EAErB,IAAIA,KAAK,CAACS,QAAQ,KAAKC,SAAS,IAAIV,KAAK,CAACS,QAAQ,KAAKH,SAAS,CAACG,QAAQ,EAAE;IACzER,IAAI,CAACU,OAAO,CAACF,QAAQ,GAAGT,KAAK,CAACS,QAAQ;EACxC;EACA,IAAIT,KAAK,CAACY,IAAI,KAAKF,SAAS,IAAIV,KAAK,CAACY,IAAI,KAAKN,SAAS,CAACM,IAAI,EAAE;IAC7DX,IAAI,CAACY,OAAO,CAACb,KAAK,CAACY,IAAI,CAAC;EAC1B;EAEAlB,SAAS,CAAC,MAAM;IACdG,eAAe,CAACI,IAAI,CAACa,UAAU,EAAEN,KAAK,CAAC;EACzC,CAAC,EAAE,CAACA,KAAK,CAAC,CAAC;EAEX,OAAO,IAAI;AACb;AAEA,eAAeZ,IAAI,CAACG,YAAY,CAAC","ignoreList":[]}
@@ -0,0 +1,7 @@
1
+ import type { ISource, CustomSource } from '../types';
2
+ export declare type SourceProps<SourceT> = (SourceT | CustomSource) & {
3
+ id?: string;
4
+ children?: any;
5
+ };
6
+ declare function Source<SourceT extends ISource>(props: SourceProps<SourceT>): any;
7
+ export default Source;
@@ -0,0 +1,98 @@
1
+ import * as React from 'react';
2
+ import { useContext, useEffect, useMemo, useState, useRef } from 'react';
3
+ import { cloneElement } from 'react';
4
+ import { MapContext } from "./map.js";
5
+ import assert from "../utils/assert.js";
6
+ import { deepEqual } from "../utils/deep-equal.js";
7
+ let sourceCounter = 0;
8
+ function createSource(map, id, props) {
9
+ if (map.style && map.style._loaded) {
10
+ const options = {
11
+ ...props
12
+ };
13
+ delete options.id;
14
+ delete options.children;
15
+ map.addSource(id, options);
16
+ return map.getSource(id);
17
+ }
18
+ return null;
19
+ }
20
+ function updateSource(source, props, prevProps) {
21
+ assert(props.id === prevProps.id, 'source id changed');
22
+ assert(props.type === prevProps.type, 'source type changed');
23
+ let changedKey = '';
24
+ let changedKeyCount = 0;
25
+ for (const key in props) {
26
+ if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) {
27
+ changedKey = key;
28
+ changedKeyCount++;
29
+ }
30
+ }
31
+ if (!changedKeyCount) {
32
+ return;
33
+ }
34
+ const type = props.type;
35
+ if (type === 'geojson') {
36
+ source.setData(props.data);
37
+ } else if (type === 'image') {
38
+ source.updateImage({
39
+ url: props.url,
40
+ coordinates: props.coordinates
41
+ });
42
+ } else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {
43
+ source.setCoordinates(props.coordinates);
44
+ } else if ('setUrl' in source) {
45
+ switch (changedKey) {
46
+ case 'url':
47
+ source.setUrl(props.url);
48
+ break;
49
+ case 'tiles':
50
+ source.setTiles(props.tiles);
51
+ break;
52
+ default:
53
+ }
54
+ } else {
55
+ console.warn(`Unable to update <Source> prop: ${changedKey}`);
56
+ }
57
+ }
58
+ function Source(props) {
59
+ const map = useContext(MapContext).map.getMap();
60
+ const propsRef = useRef(props);
61
+ const [, setStyleLoaded] = useState(0);
62
+ const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);
63
+ useEffect(() => {
64
+ if (map) {
65
+ const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);
66
+ map.on('styledata', forceUpdate);
67
+ forceUpdate();
68
+ return () => {
69
+ map.off('styledata', forceUpdate);
70
+ if (map.style && map.style._loaded && map.getSource(id)) {
71
+ var _map$getStyle;
72
+ const allLayers = (_map$getStyle = map.getStyle()) === null || _map$getStyle === void 0 ? void 0 : _map$getStyle.layers;
73
+ if (allLayers) {
74
+ for (const layer of allLayers) {
75
+ if (layer.source === id) {
76
+ map.removeLayer(layer.id);
77
+ }
78
+ }
79
+ }
80
+ map.removeSource(id);
81
+ }
82
+ };
83
+ }
84
+ return undefined;
85
+ }, [map]);
86
+ let source = map && map.style && map.getSource(id);
87
+ if (source) {
88
+ updateSource(source, props, propsRef.current);
89
+ } else {
90
+ source = createSource(map, id, props);
91
+ }
92
+ propsRef.current = props;
93
+ return source && React.Children.map(props.children, child => child && cloneElement(child, {
94
+ source: id
95
+ })) || null;
96
+ }
97
+ export default Source;
98
+ //# sourceMappingURL=source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source.js","names":["React","useContext","useEffect","useMemo","useState","useRef","cloneElement","MapContext","assert","deepEqual","sourceCounter","createSource","map","id","props","style","_loaded","options","children","addSource","getSource","updateSource","source","prevProps","type","changedKey","changedKeyCount","key","setData","data","updateImage","url","coordinates","setCoordinates","setUrl","setTiles","tiles","console","warn","Source","getMap","propsRef","setStyleLoaded","forceUpdate","setTimeout","version","on","off","_map$getStyle","allLayers","getStyle","layers","layer","removeLayer","removeSource","undefined","current","Children","child"],"sources":["../../src/components/source.ts"],"sourcesContent":["import * as React from 'react';\nimport {useContext, useEffect, useMemo, useState, useRef} from 'react';\nimport {cloneElement} from 'react';\nimport {MapContext} from './map';\nimport assert from '../utils/assert';\nimport {deepEqual} from '../utils/deep-equal';\n\nimport type {\n MapInstance,\n ISource,\n CustomSource,\n GeoJSONSourceImplementation,\n ImageSourceImplemtation,\n AnySourceImplementation\n} from '../types';\nimport type {GeoJSONSourceRaw, ImageSourceRaw, VectorSourceRaw} from '../types/style-spec-bkoi-gl';\n\nexport type SourceProps<SourceT> = (SourceT | CustomSource) & {\n id?: string;\n children?: any;\n};\n\nlet sourceCounter = 0;\n\nfunction createSource<SourceT extends ISource>(\n map: MapInstance,\n id: string,\n props: SourceProps<SourceT>\n) {\n // @ts-ignore\n if (map.style && map.style._loaded) {\n const options = {...props};\n delete options.id;\n delete options.children;\n // @ts-ignore\n map.addSource(id, options);\n return map.getSource(id);\n }\n return null;\n}\n\n/* eslint-disable complexity */\nfunction updateSource<SourceT extends ISource>(\n source: AnySourceImplementation,\n props: SourceProps<SourceT>,\n prevProps: SourceProps<SourceT>\n) {\n assert(props.id === prevProps.id, 'source id changed');\n assert(props.type === prevProps.type, 'source type changed');\n\n let changedKey = '';\n let changedKeyCount = 0;\n\n for (const key in props) {\n if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) {\n changedKey = key;\n changedKeyCount++;\n }\n }\n\n if (!changedKeyCount) {\n return;\n }\n\n const type = props.type;\n\n if (type === 'geojson') {\n (source as GeoJSONSourceImplementation).setData(\n (props as unknown as GeoJSONSourceRaw).data as any\n );\n } else if (type === 'image') {\n (source as ImageSourceImplemtation).updateImage({\n url: (props as unknown as ImageSourceRaw).url,\n coordinates: (props as unknown as ImageSourceRaw).coordinates\n });\n } else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {\n source.setCoordinates((props as ImageSourceRaw).coordinates);\n } else if ('setUrl' in source) {\n // Added in 1.12.0:\n // vectorTileSource.setTiles\n // vectorTileSource.setUrl\n switch (changedKey) {\n case 'url':\n source.setUrl((props as VectorSourceRaw).url);\n break;\n case 'tiles':\n source.setTiles((props as VectorSourceRaw).tiles);\n break;\n default:\n }\n } else {\n // eslint-disable-next-line\n console.warn(`Unable to update <Source> prop: ${changedKey}`);\n }\n}\n/* eslint-enable complexity */\n\nfunction Source<SourceT extends ISource>(props: SourceProps<SourceT>) {\n const map = useContext(MapContext).map.getMap();\n const propsRef = useRef(props);\n const [, setStyleLoaded] = useState(0);\n\n const id = useMemo(() => props.id || `jsx-source-${sourceCounter++}`, []);\n\n useEffect(() => {\n if (map) {\n /* global setTimeout */\n const forceUpdate = () => setTimeout(() => setStyleLoaded(version => version + 1), 0);\n map.on('styledata', forceUpdate);\n forceUpdate();\n\n return () => {\n map.off('styledata', forceUpdate);\n // @ts-ignore\n if (map.style && map.style._loaded && map.getSource(id)) {\n // Parent effects are destroyed before child ones, see\n // https://github.com/facebook/react/issues/16728\n // Source can only be removed after all child layers are removed\n const allLayers = map.getStyle()?.layers;\n if (allLayers) {\n for (const layer of allLayers) {\n // @ts-ignore (2339) source does not exist on all layer types\n if (layer.source === id) {\n map.removeLayer(layer.id);\n }\n }\n }\n map.removeSource(id);\n }\n };\n }\n return undefined;\n }, [map]);\n\n // @ts-ignore\n let source = map && map.style && map.getSource(id);\n if (source) {\n updateSource(source, props, propsRef.current);\n } else {\n source = createSource(map, id, props);\n }\n propsRef.current = props;\n\n return (\n (source &&\n React.Children.map(\n props.children,\n child =>\n child &&\n cloneElement(child, {\n source: id\n })\n )) ||\n null\n );\n}\n\nexport default Source;\n"],"mappings":"AAAA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAAQC,UAAU,EAAEC,SAAS,EAAEC,OAAO,EAAEC,QAAQ,EAAEC,MAAM,QAAO,OAAO;AACtE,SAAQC,YAAY,QAAO,OAAO;AAAC,SAC3BC,UAAU;AAAA,OACXC,MAAM;AAAA,SACLC,SAAS;AAiBjB,IAAIC,aAAa,GAAG,CAAC;AAErB,SAASC,YAAYA,CACnBC,GAAgB,EAChBC,EAAU,EACVC,KAA2B,EAC3B;EAEA,IAAIF,GAAG,CAACG,KAAK,IAAIH,GAAG,CAACG,KAAK,CAACC,OAAO,EAAE;IAClC,MAAMC,OAAO,GAAG;MAAC,GAAGH;IAAK,CAAC;IAC1B,OAAOG,OAAO,CAACJ,EAAE;IAC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AAAD,aAAA,GAAGpC,GAAG,CAACsC,QAAQ,CAAC,CAAC,cAAAF,aAAA,uBAAdA,aAAA,CAAgBG,MAAM;UACxC,IAAIF,SAAS,EAAE;YACb,KAAK,MAAMG,KAAK,IAAIH,SAAS,EAAE;cAE7B,IAAIG,KAAK,CAAC9B,MAAM,KAAKT,EAAE,EAAE;gBACvBD,GAAG,CAACyC,WAAW,CAACD,KAAK,CAACvC,EAAE,CAAC;cAC3B;YACF;UACF;UACAD,GAAG,CAAC0C,YAAY,CAACzC,EAAE,CAAC;QACtB;MACF,CAAC;IACH;IACA,OAAO0C,SAAS;EAClB,CAAC,EAAE,CAAC3C,GAAG,CAAC,CAAC;EAGT,IAAIU,MAAM,GAAGV,GAAG,IAAIA,GAAG,CAACG,KAAK,IAAIH,GAAG,CAACQ,SAAS,CAACP,EAAE,CAAC;EAClD,IAAIS,MAAM,EAAE;IACVD,YAAY,CAACC,MAAM,EAAER,KAAK,EAAE2B,QAAQ,CAACe,OAAO,CAAC;EAC/C,CAAC,MAAM;IACLlC,MAAM,GAAGX,YAAY,CAACC,GAAG,EAAEC,EAAE,EAAEC,KAAK,CAAC;EACvC;EACA2B,QAAQ,CAACe,OAAO,GAAG1C,KAAK;EAExB,OACGQ,MAAM,IACLtB,KAAK,CAACyD,QAAQ,CAAC7C,GAAG,CAChBE,KAAK,CAACI,QAAQ,EACdwC,KAAK,IACHA,KAAK,IACLpD,YAAY,CAACoD,KAAK,EAAE;IAClBpC,MAAM,EAAET;EACV,CAAC,CACL,CAAC,IACH,IAAI;AAER;AAEA,eAAe0B,MAAM","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { IControl, ControlPosition } from '../types';
2
+ import type { MapContextValue } from './map';
3
+ declare type ControlOptions = {
4
+ position?: ControlPosition;
5
+ };
6
+ declare function useControl<T extends IControl>(onCreate: (context: MapContextValue) => T, opts?: ControlOptions): T;
7
+ declare function useControl<T extends IControl>(onCreate: (context: MapContextValue) => T, onRemove: (context: MapContextValue) => void, opts?: ControlOptions): T;
8
+ declare function useControl<T extends IControl>(onCreate: (context: MapContextValue) => T, onAdd: (context: MapContextValue) => void, onRemove: (context: MapContextValue) => void, opts?: ControlOptions): T;
9
+ export default useControl;
@@ -0,0 +1,31 @@
1
+ import { useContext, useMemo, useEffect } from 'react';
2
+ import { MapContext } from "./map.js";
3
+ function useControl(onCreate, arg1, arg2, arg3) {
4
+ const context = useContext(MapContext);
5
+ const ctrl = useMemo(() => onCreate(context), []);
6
+ useEffect(() => {
7
+ const opts = arg3 || arg2 || arg1;
8
+ const onAdd = typeof arg1 === 'function' && typeof arg2 === 'function' ? arg1 : null;
9
+ const onRemove = typeof arg2 === 'function' ? arg2 : typeof arg1 === 'function' ? arg1 : null;
10
+ const {
11
+ map
12
+ } = context;
13
+ if (!map.hasControl(ctrl)) {
14
+ map.addControl(ctrl, opts === null || opts === void 0 ? void 0 : opts.position);
15
+ if (onAdd) {
16
+ onAdd(context);
17
+ }
18
+ }
19
+ return () => {
20
+ if (onRemove) {
21
+ onRemove(context);
22
+ }
23
+ if (map.hasControl(ctrl)) {
24
+ map.removeControl(ctrl);
25
+ }
26
+ };
27
+ }, []);
28
+ return ctrl;
29
+ }
30
+ export default useControl;
31
+ //# sourceMappingURL=use-control.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-control.js","names":["useContext","useMemo","useEffect","MapContext","useControl","onCreate","arg1","arg2","arg3","context","ctrl","opts","onAdd","onRemove","map","hasControl","addControl","position","removeControl"],"sources":["../../src/components/use-control.ts"],"sourcesContent":["import {useContext, useMemo, useEffect} from 'react';\nimport type {IControl, ControlPosition} from '../types';\nimport {MapContext} from './map';\nimport type {MapContextValue} from './map';\n\ntype ControlOptions = {\n position?: ControlPosition;\n};\n\nfunction useControl<T extends IControl>(\n onCreate: (context: MapContextValue) => T,\n opts?: ControlOptions\n): T;\n\nfunction useControl<T extends IControl>(\n onCreate: (context: MapContextValue) => T,\n onRemove: (context: MapContextValue) => void,\n opts?: ControlOptions\n): T;\n\nfunction useControl<T extends IControl>(\n onCreate: (context: MapContextValue) => T,\n onAdd: (context: MapContextValue) => void,\n onRemove: (context: MapContextValue) => void,\n opts?: ControlOptions\n): T;\n\nfunction useControl<T extends IControl>(\n onCreate: (context: MapContextValue) => T,\n arg1?: ((context: MapContextValue) => void) | ControlOptions,\n arg2?: ((context: MapContextValue) => void) | ControlOptions,\n arg3?: ControlOptions\n): T {\n const context = useContext(MapContext);\n const ctrl = useMemo(() => onCreate(context), []);\n\n useEffect(() => {\n const opts = (arg3 || arg2 || arg1) as ControlOptions;\n const onAdd = typeof arg1 === 'function' && typeof arg2 === 'function' ? arg1 : null;\n const onRemove = typeof arg2 === 'function' ? arg2 : typeof arg1 === 'function' ? arg1 : null;\n\n const {map} = context;\n if (!map.hasControl(ctrl)) {\n map.addControl(ctrl, opts?.position);\n if (onAdd) {\n onAdd(context);\n }\n }\n\n return () => {\n if (onRemove) {\n onRemove(context);\n }\n // Map might have been removed (parent effects are destroyed before child ones)\n if (map.hasControl(ctrl)) {\n map.removeControl(ctrl);\n }\n };\n }, []);\n\n return ctrl;\n}\n\nexport default useControl;\n"],"mappings":"AAAA,SAAQA,UAAU,EAAEC,OAAO,EAAEC,SAAS,QAAO,OAAO;AAAC,SAE7CC,UAAU;AAyBlB,SAASC,UAAUA,CACjBC,QAAyC,EACzCC,IAA4D,EAC5DC,IAA4D,EAC5DC,IAAqB,EAClB;EACH,MAAMC,OAAO,GAAGT,UAAU,CAACG,UAAU,CAAC;EACtC,MAAMO,IAAI,GAAGT,OAAO,CAAC,MAAMI,QAAQ,CAACI,OAAO,CAAC,EAAE,EAAE,CAAC;EAEjDP,SAAS,CAAC,MAAM;IACd,MAAMS,IAAI,GAAIH,IAAI,IAAID,IAAI,IAAID,IAAuB;IACrD,MAAMM,KAAK,GAAG,OAAON,IAAI,KAAK,UAAU,IAAI,OAAOC,IAAI,KAAK,UAAU,GAAGD,IAAI,GAAG,IAAI;IACpF,MAAMO,QAAQ,GAAG,OAAON,IAAI,KAAK,UAAU,GAAGA,IAAI,GAAG,OAAOD,IAAI,KAAK,UAAU,GAAGA,IAAI,GAAG,IAAI;IAE7F,MAAM;MAACQ;IAAG,CAAC,GAAGL,OAAO;IACrB,IAAI,CAACK,GAAG,CAACC,UAAU,CAACL,IAAI,CAAC,EAAE;MACzBI,GAAG,CAACE,UAAU,CAACN,IAAI,EAAEC,IAAI,aAAJA,IAAI,uBAAJA,IAAI,CAAEM,QAAQ,CAAC;MACpC,IAAIL,KAAK,EAAE;QACTA,KAAK,CAACH,OAAO,CAAC;MAChB;IACF;IAEA,OAAO,MAAM;MACX,IAAII,QAAQ,EAAE;QACZA,QAAQ,CAACJ,OAAO,CAAC;MACnB;MAEA,IAAIK,GAAG,CAACC,UAAU,CAACL,IAAI,CAAC,EAAE;QACxBI,GAAG,CAACI,aAAa,CAACR,IAAI,CAAC;MACzB;IACF,CAAC;EACH,CAAC,EAAE,EAAE,CAAC;EAEN,OAAOA,IAAI;AACb;AAEA,eAAeN,UAAU","ignoreList":[]}
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ import { MapRef } from '../mapbox/create-ref';
3
+ import { MapInstance } from '../types';
4
+ declare type MountedMapsContextValue = {
5
+ maps: {
6
+ [id: string]: MapRef<MapInstance>;
7
+ };
8
+ onMapMount: (map: MapRef<MapInstance>, id: string) => void;
9
+ onMapUnmount: (id: string) => void;
10
+ };
11
+ export declare const MountedMapsContext: React.Context<MountedMapsContextValue>;
12
+ export declare const MapProvider: React.FC<{
13
+ children?: React.ReactNode;
14
+ }>;
15
+ export declare type MapCollection<MapT extends MapInstance> = {
16
+ [id: string]: MapRef<MapT> | undefined;
17
+ current?: MapRef<MapT>;
18
+ };
19
+ export declare function useMap<MapT extends MapInstance>(): MapCollection<MapT>;
20
+ export {};
@@ -0,0 +1,55 @@
1
+ import * as React from 'react';
2
+ import { useState, useCallback, useMemo, useContext } from 'react';
3
+ import { MapContext } from "./map.js";
4
+ export const MountedMapsContext = React.createContext(null);
5
+ export const MapProvider = props => {
6
+ const [maps, setMaps] = useState({});
7
+ const onMapMount = useCallback(function (map) {
8
+ let id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
9
+ setMaps(currMaps => {
10
+ if (id === 'current') {
11
+ throw new Error("'current' cannot be used as map id");
12
+ }
13
+ if (currMaps[id]) {
14
+ throw new Error(`Multiple maps with the same id: ${id}`);
15
+ }
16
+ return {
17
+ ...currMaps,
18
+ [id]: map
19
+ };
20
+ });
21
+ }, []);
22
+ const onMapUnmount = useCallback(function () {
23
+ let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';
24
+ setMaps(currMaps => {
25
+ if (currMaps[id]) {
26
+ const nextMaps = {
27
+ ...currMaps
28
+ };
29
+ delete nextMaps[id];
30
+ return nextMaps;
31
+ }
32
+ return currMaps;
33
+ });
34
+ }, []);
35
+ return React.createElement(MountedMapsContext.Provider, {
36
+ value: {
37
+ maps,
38
+ onMapMount,
39
+ onMapUnmount
40
+ }
41
+ }, props.children);
42
+ };
43
+ export function useMap() {
44
+ var _useContext;
45
+ const maps = (_useContext = useContext(MountedMapsContext)) === null || _useContext === void 0 ? void 0 : _useContext.maps;
46
+ const currentMap = useContext(MapContext);
47
+ const mapsWithCurrent = useMemo(() => {
48
+ return {
49
+ ...maps,
50
+ current: currentMap === null || currentMap === void 0 ? void 0 : currentMap.map
51
+ };
52
+ }, [maps, currentMap]);
53
+ return mapsWithCurrent;
54
+ }
55
+ //# sourceMappingURL=use-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-map.js","names":["React","useState","useCallback","useMemo","useContext","MapContext","MountedMapsContext","createContext","MapProvider","props","maps","setMaps","onMapMount","map","id","arguments","length","undefined","currMaps","Error","onMapUnmount","nextMaps","createElement","Provider","value","children","useMap","_useContext","currentMap","mapsWithCurrent","current"],"sources":["../../src/components/use-map.tsx"],"sourcesContent":["import * as React from 'react';\nimport {useState, useCallback, useMemo, useContext} from 'react';\n\nimport {MapRef} from '../mapbox/create-ref';\nimport {MapContext} from './map';\nimport {MapInstance} from '../types';\n\ntype MountedMapsContextValue = {\n maps: {[id: string]: MapRef<MapInstance>};\n onMapMount: (map: MapRef<MapInstance>, id: string) => void;\n onMapUnmount: (id: string) => void;\n};\n\nexport const MountedMapsContext = React.createContext<MountedMapsContextValue>(null);\n\nexport const MapProvider: React.FC<{children?: React.ReactNode}> = props => {\n const [maps, setMaps] = useState<{[id: string]: MapRef<MapInstance>}>({});\n\n const onMapMount = useCallback((map: MapRef<MapInstance>, id: string = 'default') => {\n setMaps(currMaps => {\n if (id === 'current') {\n throw new Error(\"'current' cannot be used as map id\");\n }\n if (currMaps[id]) {\n throw new Error(`Multiple maps with the same id: ${id}`);\n }\n return {...currMaps, [id]: map};\n });\n }, []);\n\n const onMapUnmount = useCallback((id: string = 'default') => {\n setMaps(currMaps => {\n if (currMaps[id]) {\n const nextMaps = {...currMaps};\n delete nextMaps[id];\n return nextMaps;\n }\n return currMaps;\n });\n }, []);\n\n return (\n <MountedMapsContext.Provider\n value={{\n maps,\n onMapMount,\n onMapUnmount\n }}\n >\n {props.children}\n </MountedMapsContext.Provider>\n );\n};\n\nexport type MapCollection<MapT extends MapInstance> = {\n [id: string]: MapRef<MapT> | undefined;\n current?: MapRef<MapT>;\n};\n\nexport function useMap<MapT extends MapInstance>(): MapCollection<MapT> {\n const maps = useContext(MountedMapsContext)?.maps;\n const currentMap = useContext(MapContext);\n\n const mapsWithCurrent = useMemo(() => {\n return {...maps, current: currentMap?.map};\n }, [maps, currentMap]);\n\n return mapsWithCurrent as MapCollection<MapT>;\n}\n"],"mappings":"AAAA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAAQC,QAAQ,EAAEC,WAAW,EAAEC,OAAO,EAAEC,UAAU,QAAO,OAAO;AAAC,SAGzDC,UAAU;AASlB,OAAO,MAAMC,kBAAkB,GAAGN,KAAK,CAACO,aAAa,CAA0B,IAAI,CAAC;AAEpF,OAAO,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