@mapcomponents/react-maplibre 1.0.2 → 1.0.4

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Change Log
2
2
 
3
+ ## [v1.0.4] - 2024-06-21
4
+
5
+ ### Added
6
+ - cab1848: add hints to buttons in catalogue stories
7
+ - c621883: Feature/add layer and source events (#167) …
8
+ - fcb3c75: add feature for additional pdf form info to MlPdfForm (#162) …
9
+ - 7994580: add client search component MlClientSearch (#166) …
10
+ - b0d2c32: add makeMapContextDecorators function to enable changing MapLibreMap options inside the sb-story (#165) …
11
+
12
+ ### Changed
13
+ - 20378bc: refactor geojson stories
14
+ - af7e0c4, 6fa9a9b: update README.md
15
+ - 8735ac8: redefiened MlLayer useLayerProps (#160) …
16
+
17
+ ### Fixed
18
+ - a48a8fa: fix MlImageMarkerLayer story
19
+ - 3afbe7b: remove useLayer hook initializedRef anti-pattern and fix reinitializa… (#169) …
20
+ - 14ef756: Fix/usesource stylechange (#168) …
21
+
22
+
23
+ ## [v1.0.3] - 2024-02-22
24
+
25
+ ### Fixed
26
+ - a80667a: import missing maplibre-gl.css again in MapLibreMap component (#164)
27
+
3
28
 
4
29
  ## [v1.0.1] - 2024-02-16
5
30
 
package/README.md CHANGED
@@ -29,13 +29,20 @@ The easiest way to start a new project using this framework is to bootstrap a re
29
29
 
30
30
  Run the following commands in the terminal:
31
31
 
32
- 1. `npx degit mapcomponents/template {your-app-name}`
33
- 2. `cd {your-app-name}`
34
- 3. `yarn`
35
- 4. `yarn dev`
32
+ ```
33
+ npx degit mapcomponents/template {your-app-name}
34
+ cd {your-app-name}
35
+ yarn
36
+ yarn dev
37
+ ```
38
+
39
+ <img src="docs_md/initial_0.gif" />
40
+
36
41
 
37
42
  This will start a development server that serves the mapcomponents app on port 5174 of your local machine as well as a browser tab with live reload enabled. This reloads the affected parts of the application if changes are detected to the corresponding files in the filesystem. Open the project folder in the IDE of your choice and start building your map client.
38
43
 
44
+ <img src="docs_md/initial_3.gif" width="400" />
45
+
39
46
  ### **... an existing react project**
40
47
 
41
48
  In this case, navigate to your project folder in a terminal and execute the following steps:
@@ -44,70 +51,44 @@ In this case, navigate to your project folder in a terminal and execute the foll
44
51
  2. Add the MapComponentsProvider (named export of this module) to your applications react-DOM where it makes sense. Only children of this component will be able to render a map canvas or interact with a maplibre-gl instance. Place it in the index.js entrypoint if your application is a dedicated map app and all components have a high probability to interact with the maplibre-gl instance. Place it somewhere higher in the JSX tree if the map constitutes only a small portion of your app and components outside of the MapComponentsProvider have no need to interact with the map instance.
45
52
  3. Add a MapLibreMap component to the react-DOM wherever the map canvas is supposed to be placed.
46
53
 
47
- ## How it works
48
- ### Anatomy of a MapComponent
49
-
50
- A MapComponent is a react component that accepts at least 1 attribute "mapId" (there are some exceptions) and is expected to retrieve a maplibre-gl instance from mapContext and directly manipulate it or watch its state.
51
- An example implementation of basic required functions for the maplibre instance retrieval process using the useMap hook, can be seen in [./components/MlComponentTemplate/MlComponentTemplate.tsx](https://github.com/mapcomponents/react-map-components-maplibre/blob/main/src/components/MlComponentTemplate/MlComponentTemplate.tsx)
52
- If no attribute mapId is provided the map component is expected to work with the map instance provided by mapContext at ```mapContext.map``` (the first maplibre instance that is registered in MapContext).
53
-
54
-
55
- ### Cleanup functions
56
-
57
- Once a component is removed from reactDOM we need to make sure everything it has added to the maplibre-gl instance is removed with it. The mapHook offers a convenient way to do this.
58
-
59
- **- Retrieve the maplibre instance using the useMap hook**
60
-
61
- Add `mapHook.map` to the dependency array of e.g. a useEffect hook to trigger it once the map instance becomes available.
62
-
63
- ```js
64
-
65
- const mapHook = useMap({
66
- mapId: props.mapId,
67
- waitForLayer: props.insertBeforeLayer,
68
- });
69
-
70
- useEffect(() => {
71
- if (!mapHook.map) return;
72
- // the MapLibre-gl instance (mapHook.map) is accessible here
73
- // initialize the layer and add it to the MapLibre-gl instance
74
-
75
- // optionally add layers, sources, event listeners, controls, images to the MapLibre instance that are required by this component
76
- mapHook.map.addLayer(
77
- {/*layer-config*/},
78
- props.insertBeforeLayer,
79
- mapHook.componentId)
80
-
81
- return () => {
82
- mapHook.cleanup();
83
- }
84
-
85
- }, [mapHook.map]);
86
-
54
+ ## Adding data to the map
55
+
56
+ The easiest way to add data to the map is using GeoJSON data and the MlGeoJsonLayer component. The MlGeoJsonLayer component is designed to require only the `geojson` property to be set, while it provides properties to fully customize it to your needs.
57
+
58
+ Insert the following code in your app below index.tsx to add a GeoJSON layer to your map.
59
+
60
+ ```JSX
61
+ import MlGeoJsonLayer from '@mapcomponents/react-maplibre';
62
+
63
+ const geojson_data = {
64
+ "type": "Feature",
65
+ "properties": {},
66
+ "geometry": {
67
+ "coordinates": [
68
+ [
69
+ [0.7415817264899545, 56.91203727013931],
70
+ [0.7743616447289128, 55.2757658775181],
71
+ [4.609612078732766, 55.23840364745948],
72
+ [4.642391996971725, 56.91203727013931],
73
+ [0.7415817264899545, 56.91203727013931]
74
+ ]
75
+ ],
76
+ "type": "Polygon"
77
+ }
78
+ }
87
79
  ```
88
- **- Component cleanup function**
89
-
90
- `mapHook.cleanup()` will remove all ressources from the maplibre-gl instance that have been added using `mapHook.componentId` as additional parameter in `map.addLayer`, `map.addSource`, `map.on`, `map.addImage` or `map.addControl` calls.
91
-
92
- ```js
93
-
94
- useEffect(() => {
95
80
 
96
- return () => {
97
- // This is the cleanup function, it is called when this react component is removed from react-dom
98
- mapHook.cleanup();
99
- };
100
- }, []);
81
+ And add the following within the JSX.
101
82
 
83
+ ```JSX
84
+ <MlGeoJsonLayer geojson={geojson_data} />
102
85
  ```
103
86
 
104
- **- addLayer, addSource, addImage, addControls, on**
87
+ <img src="docs_md/sample_screenshot.png" />
105
88
 
106
- The functions mentioned above have been overridden in the MapLibreGlWrapper instance that is referenced by mapHook.map.
107
- All five functions expect an additional optional parameter "component_id" (string) as their last or optional parameter (except for the beforeLayerId parameter of the addLayer function, which should be defined as props.beforeLayerId to make sure the parent component is able to control the layer order).
108
- A uuid `componentId` property is generated and available on the object returned by mapHook.
109
- MapLibreGlWrapper uses the component_id to keep track of everything that has been added by a specific component (including implicitly added sources), enabling a safe and simple cleanup by calling ```mapHook.cleanup()``` as shown in the cleanup function example above.
89
+ Please take a look at our storybooks and the code examples provided in the {ComponentName}.stories.tsx files next to the Components in the `./src/components/` folder.
110
90
 
111
91
  ## more links
112
92
 
93
+ - @mapcomponents/react-maplibre storybook: https://mapcomponents.github.io/react-map-components-maplibre
113
94
  - @mapcomponents/react-maplibre-lab storybook: https://mapcomponents.github.io/react-map-components-maplibre-lab
@@ -1,5 +1,6 @@
1
1
  import { FC } from 'react';
2
2
  import { MapOptions as MapOptionsType } from 'maplibre-gl';
3
+ import 'maplibre-gl/dist/maplibre-gl.css';
3
4
  export type MapLibreMapProps = {
4
5
  /**
5
6
  * Id of the MapLibreGl(Wrapper) instance in mapContext
@@ -1,6 +1,6 @@
1
1
  import { Map, IControl, MapOptions as MapOptionsType, MapEventType, MapLayerEventType, StyleImageInterface, LayerSpecification, CustomLayerInterface, SourceSpecification, ControlPosition, StyleImageMetadata } from 'maplibre-gl';
2
2
  import { Map as MapType, Style } from 'maplibre-gl';
3
- type WrapperEventArgArray = [string, (arg0: unknown) => void];
3
+ type WrapperEventArgArray = [MapLibreGlWrapperEventName, MapLibreGlWrapperEventHandlerType];
4
4
  type EventArgArray = [
5
5
  keyof MapLayerEventType | keyof MapEventType,
6
6
  string | ((arg0: unknown) => void),
@@ -41,7 +41,30 @@ interface MapLibreGlWrapper extends MapType {
41
41
  }), beforeId?: string | undefined, componentId?: string | undefined) => this;
42
42
  cancelled: boolean;
43
43
  }
44
+ interface MapLibreGlWrapperEventHandlers {
45
+ layerchange: {
46
+ handler: (ev: unknown) => void;
47
+ options?: object | string;
48
+ }[];
49
+ viewportchange: {
50
+ handler: (ev: unknown) => void;
51
+ options?: object | string;
52
+ }[];
53
+ addsource: {
54
+ handler: (ev: unknown, wrapper?: MapLibreGlWrapper, data?: {
55
+ [source_id: string]: string;
56
+ }) => void;
57
+ }[];
58
+ addlayer: {
59
+ handler: (ev: unknown) => void;
60
+ options?: object | string;
61
+ }[];
62
+ }
63
+ export type MapLibreGlWrapperEventHandlerType = MapLibreGlWrapperEventHandlers['layerchange'][number]['handler'] | MapLibreGlWrapperEventHandlers['viewportchange'][number]['handler'] | MapLibreGlWrapperEventHandlers['addsource'][number]['handler'] | MapLibreGlWrapperEventHandlers['addlayer'][number]['handler'];
64
+ export type MapLibreGlEventName = keyof MapLayerEventType | keyof MapEventType | string;
65
+ export type MapLibreGlWrapperEventName = keyof MapLibreGlWrapperEventHandlers;
44
66
  declare class MapLibreGlWrapper {
67
+ [key: string]: any;
45
68
  registeredElements: {
46
69
  [key: string]: {
47
70
  layers: [string?];
@@ -54,18 +77,10 @@ declare class MapLibreGlWrapper {
54
77
  };
55
78
  baseLayers: [string?];
56
79
  firstSymbolLayer: string | undefined;
57
- eventHandlers: {
58
- layerchange: {
59
- handler: (ev: unknown) => void;
60
- options?: object | string;
61
- }[];
62
- viewportchange: {
63
- handler: (ev: unknown) => void;
64
- }[];
65
- };
80
+ eventHandlers: MapLibreGlWrapperEventHandlers;
66
81
  wrapper: {
67
- on: (eventName: string, handler: (ev: unknown) => void, options?: object | string, componentId?: string) => void;
68
- off: (type: string, listener: (ev: unknown) => void) => void;
82
+ on: (eventName: MapLibreGlWrapperEventName, handler: MapLibreGlWrapperEventHandlerType, options?: object | string, componentId?: string) => void;
83
+ off: (type: string, handler: MapLibreGlWrapperEventHandlerType) => void;
69
84
  fire: (eventName: string, context?: unknown) => void;
70
85
  layerState: LayerState[];
71
86
  layerStateString: string;
@@ -94,7 +109,7 @@ declare class MapLibreGlWrapper {
94
109
  styleJson: object;
95
110
  addSource: (id: string, source: SourceSpecification, componentId?: string | undefined) => this;
96
111
  addControl: (control: IControl | unknown, position?: ControlPosition | undefined, componentId?: string | undefined) => this;
97
- on: (type: keyof MapLayerEventType | keyof MapEventType | string, layerId: string | ((ev: unknown) => void), handler?: ((ev: MapEventType & unknown) => Map | void) | string, componentId?: string | undefined) => this;
112
+ on: (type: MapLibreGlEventName, layerId: string | ((ev: unknown) => void), handler?: ((ev: MapEventType & unknown) => Map | void) | string, componentId?: string | undefined) => this;
98
113
  cleanup: (componentId: string) => void;
99
114
  constructor(props: {
100
115
  mapOptions: MapOptionsType;
@@ -0,0 +1,30 @@
1
+ import React from 'react';
2
+ import { SearchContextInterface } from './lib/searchContext';
3
+ interface MlClientSearchProps {
4
+ /**
5
+ * Search Engine: http://elasticlunr.com/
6
+ */
7
+ searchIndex: SearchContextInterface['searchIndex'];
8
+ /**
9
+ * Docs: http://elasticlunr.com/docs/configuration.js.html
10
+ */
11
+ fields: SearchContextInterface['fields'];
12
+ /**
13
+ * Docs: https://mui.com/material-ui/api/autocomplete/
14
+ */
15
+ renderOption?: SearchContextInterface['renderOption'];
16
+ /**
17
+ * Label search field
18
+ */
19
+ searchFieldLabel?: SearchContextInterface['searchFieldLabel'];
20
+ }
21
+ export type { MlClientSearchProps };
22
+ /**
23
+ * Component template
24
+ *
25
+ */
26
+ declare const MlClientSearch: {
27
+ ({ searchIndex, fields, renderOption, searchFieldLabel, }: MlClientSearchProps): React.JSX.Element;
28
+ defaultProps: {};
29
+ };
30
+ export default MlClientSearch;
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { SearchContextInterface } from './searchContext.js';
3
+ interface SearchContextProviderProps {
4
+ children: React.ReactNode;
5
+ searchIndex: SearchContextInterface['searchIndex'];
6
+ fields: SearchContextInterface['fields'];
7
+ renderOption?: SearchContextInterface['renderOption'];
8
+ searchFieldLabel?: SearchContextInterface['searchFieldLabel'];
9
+ }
10
+ declare const SearchContext: React.Context<SearchContextInterface>;
11
+ declare const SearchContextProvider: React.FC<SearchContextProviderProps>;
12
+ export { SearchContextProvider };
13
+ export default SearchContext;
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export default function SearchForm(): React.JSX.Element;
@@ -0,0 +1,3 @@
1
+ type ExcludedGeometryCollection = GeoJSON.Point | GeoJSON.MultiPoint | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.Polygon;
2
+ export declare function createGeoJSONFeature(coordinates: ExcludedGeometryCollection['coordinates']): GeoJSON.Feature<ExcludedGeometryCollection>;
3
+ export {};
@@ -1,28 +1,11 @@
1
1
  import React from 'react';
2
- import { createPdfResolverParams } from '../../hooks/useExportMap/lib';
3
- import { SxProps } from '@mui/material';
4
- interface MlCreatePdfFormProps {
5
- /**
6
- * Id of the target MapLibre instance in mapContext
7
- */
8
- mapId?: string;
9
- /**
10
- * sx props that will be applied to the form control components
11
- */
12
- formControlStyles?: SxProps;
13
- /**
14
- * Function that will be called before the PDF is created.
15
- * Allowing to access and manipulate the jspdf instance before the PDF is created.
16
- */
17
- onCreatePdf?: (options: createPdfResolverParams) => createPdfResolverParams;
18
- }
19
- export type { MlCreatePdfFormProps };
2
+ import { PdfFormProps } from './lib/PdfForm';
20
3
  /**
21
4
  * Create PDF Form Component
22
5
  *
23
6
  */
24
7
  declare const MlCreatePdfForm: {
25
- (props: MlCreatePdfFormProps): React.JSX.Element;
8
+ (props: PdfFormProps): React.JSX.Element;
26
9
  defaultProps: {
27
10
  mapId: undefined;
28
11
  };
@@ -1,11 +1,25 @@
1
1
  import React from 'react';
2
+ import { SxProps } from '@mui/material';
2
3
  import { createPdfResolverParams } from '../../../hooks/useExportMap/lib';
3
- interface PdfFormProps {
4
+ export interface PdfFormProps {
4
5
  /**
5
6
  * Id of the target MapLibre instance in mapContext
6
7
  */
7
8
  mapId?: string;
9
+ /**
10
+ * Function that will be called before the PDF is created.
11
+ * Allowing to access and manipulate the jspdf instance before the PDF is created.
12
+ */
8
13
  onCreatePdf?: (options: createPdfResolverParams) => createPdfResolverParams;
14
+ /**
15
+ * sx props that will be applied to the form control components
16
+ */
17
+ formControlStyles?: SxProps;
18
+ /**
19
+ * Define additional form fields that will be added to the bottom of the create-PDF form.
20
+ * Values of form elements added this way are accessible inside the function passed to the onCreatePdf property using options.formData.get('{form_el_name}').
21
+ * Make sure all form elements added this way have a name property defined to be able to access the value when rendering the PDF.
22
+ */
23
+ additionalFields?: React.ReactNode;
9
24
  }
10
25
  export default function PdfForm(props: PdfFormProps): React.JSX.Element;
11
- export {};
@@ -1,4 +1,7 @@
1
+ import { MapLibreMapProps } from '../components/MapLibreMap/MapLibreMap';
1
2
  import './style.css';
2
3
  import { Decorator } from '@storybook/react';
3
- declare const decorators: Decorator[];
4
- export default decorators;
4
+ declare const makeMapContextDecorators: (options: MapLibreMapProps['options']) => Decorator[];
5
+ declare const _default: Decorator[];
6
+ export default _default;
7
+ export { makeMapContextDecorators };
@@ -27,6 +27,7 @@ interface createJsPdfOptions extends createExportOptions {
27
27
  }
28
28
  export type { createJsPdfOptions };
29
29
  interface createPdfResolverParams extends createJsPdfOptions {
30
+ formData: FormData;
30
31
  pdf: jsPDF;
31
32
  downloadPdf: (_options?: downloadPdfOptions) => Promise<downloadPdfOptions>;
32
33
  }
@@ -1,4 +1,3 @@
1
- import PropTypes from "prop-types";
2
1
  import { LayerState, ViewportState } from "../components/MapLibreMap/lib/MapLibreGlWrapper";
3
2
  type useMapStateType = {
4
3
  layers: (LayerState | undefined)[];
@@ -34,29 +33,5 @@ declare namespace useMapState {
34
33
  includeBaseLayers: boolean;
35
34
  };
36
35
  };
37
- var propTypes: {
38
- /**
39
- * Id of the target MapLibre instance in mapContext
40
- */
41
- mapId: PropTypes.Requireable<string>;
42
- /**
43
- * Defines map Resources to watch
44
- */
45
- watch: PropTypes.Requireable<PropTypes.InferProps<{
46
- layers: PropTypes.Requireable<boolean>;
47
- sources: PropTypes.Requireable<boolean>;
48
- viewport: PropTypes.Requireable<boolean>;
49
- }>>;
50
- /**
51
- * Filter string or RegExp to more explicitly define the elements watched and increase performance
52
- * strings will be matched using layerId.includes(matchString)
53
- * RegExps will be matched using matchRegExp.test(layerId)
54
- */
55
- filter: PropTypes.Requireable<PropTypes.InferProps<{
56
- includeBaseLayers: PropTypes.Requireable<boolean>;
57
- matchLayerIds: PropTypes.Requireable<NonNullable<string | RegExp | null | undefined>>;
58
- matchSourceIds: PropTypes.Requireable<NonNullable<string | RegExp | null | undefined>>;
59
- }>>;
60
- };
61
36
  }
62
37
  export default useMapState;