@geolonia/maps-react 0.7.2 → 0.8.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.
- package/README.md +20 -1
- package/dist/index.d.ts +19 -6
- package/dist/index.js +31 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,6 +99,22 @@ Root component. Creates a Geolonia map instance and provides it to children via
|
|
|
99
99
|
<Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={spots} circle={{ size: 'weight', color: 'category' }} />
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
+
#### Event handlers
|
|
103
|
+
|
|
104
|
+
`<Map>` takes a handler prop for each MapLibre event: `onClick`, `onDblClick`, `onMouseDown`, `onMouseUp`, `onMouseMove`, `onMouseEnter`, `onMouseLeave`, `onMouseOver`, `onMouseOut`, `onContextMenu`, `onWheel`, `onTouchStart`, `onTouchMove`, `onTouchEnd`, `onTouchCancel`, `onMoveStart`, `onMove`, `onMoveEnd`, `onZoomStart`, `onZoom`, `onZoomEnd`, `onRotateStart`, `onRotate`, `onRotateEnd`, `onPitchStart`, `onPitch`, `onPitchEnd`, `onDragStart`, `onDrag`, `onDragEnd`, `onError`, `onIdle`, `onRender`, `onResize`, `onData`, `onStyleData` and `onSourceData`. Each handler receives the MapLibre event of the same name. The pointer handlers (`onClick` to `onContextMenu`) receive a `MapLayerMouseEvent`. When `interactiveLayerIds` is set, its `features` lists the rendered features of those layers under the pointer (an empty array if there are none), and `onMouseEnter` / `onMouseLeave` fire when the pointer enters or leaves a feature of those layers. When `interactiveLayerIds` changes while the pointer is over a feature, `onMouseLeave` is called at once with the last pointer event and an empty `features`.
|
|
105
|
+
|
|
106
|
+
To define a handler apart from the JSX, type it with the exported event types: `MapLayerMouseEvent`, `MapLayerTouchEvent`, `MapMouseEvent`, `MapTouchEvent`, `MapWheelEvent`, `MapSourceDataEvent`, `MapStyleDataEvent` and `MapGeoJSONFeature` (re-exported from `maplibre-gl`), and `MapEventOf<'onClick'>` for the event a given handler prop receives.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { type MapLayerMouseEvent, Map } from '@geolonia/maps-react';
|
|
110
|
+
|
|
111
|
+
const onClick = (e: MapLayerMouseEvent) => {
|
|
112
|
+
console.log(e.lngLat.lng, e.lngLat.lat, e.features?.[0]?.properties);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
<Map apiKey="YOUR-API-KEY" interactiveLayerIds={['places']} onClick={onClick} />
|
|
116
|
+
```
|
|
117
|
+
|
|
102
118
|
### `<Marker>`
|
|
103
119
|
|
|
104
120
|
Displays a marker at the given coordinates. Children are rendered as a popup.
|
|
@@ -115,6 +131,8 @@ Displays a marker at the given coordinates. Children are rendered as a popup.
|
|
|
115
131
|
| `lng` | `number` | Longitude (required) |
|
|
116
132
|
| `color` | `string` | Marker color (changing recreates the marker) |
|
|
117
133
|
| `openPopup` | `boolean` | Show the popup on mount / toggle open state when changed |
|
|
134
|
+
| `onClick` | `(e: MouseEvent) => void` | Called when the marker is clicked |
|
|
135
|
+
| `onDragStart` / `onDrag` / `onDragEnd` | `(e: MarkerDragEvent) => void` | Called while the marker is dragged (`draggable`). `e.target` is the marker, so `e.target.getLngLat()` is its position |
|
|
118
136
|
| `children` | `ReactNode` | Popup content |
|
|
119
137
|
|
|
120
138
|
### `<Popup>`
|
|
@@ -231,10 +249,11 @@ A search box on the map for Japanese addresses. Submitting an address moves the
|
|
|
231
249
|
| `placeholder` | `string` | Placeholder and label of the input. Default `'住所を入力'` |
|
|
232
250
|
| `marker` | `boolean` | Put a marker at the result. Default `true` |
|
|
233
251
|
| `fitBounds` | `boolean` | Move the map to the bounds of the result. Default `true` |
|
|
252
|
+
| `onSearch` | `(address: string) => void` | Called when a search starts, for every submit (an empty input too, which then ends in `onError`). The first search can take seconds while the address data loads, so use it to show a "searching" state |
|
|
234
253
|
| `onResult` | `(result: GeocodeResult, address: string) => void` | Called when an address is found. `address` is what was typed |
|
|
235
254
|
| `onError` | `(error: GeocodeError) => void` | Called when a search fails. `error.code` is `ZERO_RESULTS` (no position found), `INVALID_REQUEST` (empty input) or `ERROR` (the address data could not be loaded) |
|
|
236
255
|
|
|
237
|
-
The input has `aria-invalid` after a failed search, until it is edited. When searches overlap, only the latest one changes the map. The control and the marker are removed when the component unmounts.
|
|
256
|
+
The input has `aria-invalid` after a failed search, until it is edited, and the form has `aria-busy="true"` from submitting until the latest search ends. When searches overlap, only the latest one changes the map and ends the busy state. The control and the marker are removed when the component unmounts.
|
|
238
257
|
|
|
239
258
|
To build your own search UI, call `geocode(address)`. It returns a `GeocodeResult` and rejects with a `GeocodeError`.
|
|
240
259
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { GeoloniaMapOptions, GeoloniaMap, MaskOptions } from '@geolonia/maps-core';
|
|
2
2
|
export { ChoroplethOptions, CircleOptions, ColorMap, ExtrusionColorOptions, ExtrusionHeightOptions, ExtrusionOptions, HeatmapOptions, MaskOptions, RasterTilesOptions } from '@geolonia/maps-core';
|
|
3
|
+
import { Map as Map$1, AttributionControlOptions, FullscreenControlOptions, GeolocateControlOptions, NavigationControlOptions, ScaleControlOptions, IControl, LayerSpecification, MapLayerEventType, MapEventType, PointLike, PositionAnchor, Alignment, MarkerDragEvent, SourceSpecification, CanvasSourceSpecification } from 'maplibre-gl';
|
|
4
|
+
export { MapGeoJSONFeature, MapLayerMouseEvent, MapLayerTouchEvent, MapMouseEvent, MapSourceDataEvent, MapStyleDataEvent, MapTouchEvent, MapWheelEvent, MarkerDragEvent } from 'maplibre-gl';
|
|
3
5
|
import * as react from 'react';
|
|
4
6
|
import { ReactNode, CSSProperties } from 'react';
|
|
5
|
-
import { Map as Map$1, AttributionControlOptions, FullscreenControlOptions, GeolocateControlOptions, NavigationControlOptions, ScaleControlOptions, IControl, LayerSpecification, PointLike, PositionAnchor, Alignment, SourceSpecification, CanvasSourceSpecification } from 'maplibre-gl';
|
|
6
7
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
7
8
|
|
|
8
9
|
type ControlPosition = "top-left" | "top-right" | "bottom-left" | "bottom-right";
|
|
@@ -74,12 +75,17 @@ type GeocoderControlProps = {
|
|
|
74
75
|
marker?: boolean;
|
|
75
76
|
/** 結果の範囲へ地図を動かす。既定は true。 */
|
|
76
77
|
fitBounds?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 検索を始めたときに呼ばれる。`address` は入力された文字列。空の入力でも呼ばれ、
|
|
80
|
+
* その場合は続けて `onError` が `INVALID_REQUEST` で呼ばれる。
|
|
81
|
+
*/
|
|
82
|
+
onSearch?: (address: string) => void;
|
|
77
83
|
/** 住所が見つかったときに呼ばれる。`address` は入力された文字列。 */
|
|
78
84
|
onResult?: (result: GeocodeResult, address: string) => void;
|
|
79
85
|
/** 検索に失敗したときに呼ばれる。`error.code` で原因を区別できる。 */
|
|
80
86
|
onError?: (error: GeocodeError) => void;
|
|
81
87
|
};
|
|
82
|
-
declare function GeocoderControl({ position, placeholder, marker, fitBounds, onResult, onError, }?: GeocoderControlProps): react_jsx_runtime.JSX.Element;
|
|
88
|
+
declare function GeocoderControl({ position, placeholder, marker, fitBounds, onSearch, onResult, onError, }?: GeocoderControlProps): react_jsx_runtime.JSX.Element;
|
|
83
89
|
|
|
84
90
|
type GeolocateControlProps = GeolocateControlOptions & {
|
|
85
91
|
position?: ControlPosition;
|
|
@@ -156,9 +162,16 @@ declare const HANDLER_NAMES: {
|
|
|
156
162
|
readonly onSourceData: "sourcedata";
|
|
157
163
|
};
|
|
158
164
|
type HandlerKey = keyof typeof HANDLER_NAMES;
|
|
159
|
-
type
|
|
165
|
+
type HandlerEventName<K extends HandlerKey> = (typeof HANDLER_NAMES)[K];
|
|
166
|
+
/**
|
|
167
|
+
* The event a handler prop receives: the MapLibre event of the same name. The
|
|
168
|
+
* pointer events (`onClick`, `onMouseMove`, ...) are `MapLayerMouseEvent`. When
|
|
169
|
+
* `interactiveLayerIds` is set, their `features` lists the rendered features of
|
|
170
|
+
* those layers under the pointer (an empty array if there are none).
|
|
171
|
+
*/
|
|
172
|
+
type MapEventOf<K extends HandlerKey> = HandlerEventName<K> extends keyof MapLayerEventType ? MapLayerEventType[HandlerEventName<K>] : MapEventType[Extract<HandlerEventName<K>, keyof MapEventType>];
|
|
160
173
|
type MapEventProps = {
|
|
161
|
-
[K in HandlerKey]?:
|
|
174
|
+
[K in HandlerKey]?: (e: MapEventOf<K>) => void;
|
|
162
175
|
};
|
|
163
176
|
type MapProps = Omit<GeoloniaMapOptions, "container"> & MapEventProps & {
|
|
164
177
|
id?: string;
|
|
@@ -188,7 +201,7 @@ type MapProviderProps = {
|
|
|
188
201
|
};
|
|
189
202
|
declare function MapProvider({ children }: MapProviderProps): react_jsx_runtime.JSX.Element;
|
|
190
203
|
|
|
191
|
-
type MarkerEventHandler = (e:
|
|
204
|
+
type MarkerEventHandler = (e: MarkerDragEvent) => void;
|
|
192
205
|
type MarkerProps = {
|
|
193
206
|
lat: number;
|
|
194
207
|
lng: number;
|
|
@@ -272,4 +285,4 @@ declare const SourceContext: react.Context<string | null>;
|
|
|
272
285
|
declare function useSourceId(): string | null;
|
|
273
286
|
|
|
274
287
|
export { AttributionControl, Control, FullscreenControl, GeocodeError, GeocoderControl, GeolocateControl, GlobeControl, Layer, Map, MapContext, MapProvider, MapProviderContext, Marker, Mask, NavigationControl, Popup, ScaleControl, SimpleStyle, Source, SourceContext, geocode, useControl, useMap, useMaps, useSourceId };
|
|
275
|
-
export type { AttributionControlProps, ControlPosition, ControlProps, FullscreenControlProps, GeocodeErrorCode, GeocodeResult, GeocoderControlProps, GeolocateControlProps, GlobeControlProps, LayerProps, MapCollection, MapEventProps, MapProps, MapProviderProps, MapRegistry, MarkerProps, MaskProps, NavigationControlProps, PopupProps, ScaleControlProps, SimpleStyleProps, SourceProps };
|
|
288
|
+
export type { AttributionControlProps, ControlPosition, ControlProps, FullscreenControlProps, GeocodeErrorCode, GeocodeResult, GeocoderControlProps, GeolocateControlProps, GlobeControlProps, LayerProps, MapCollection, MapEventOf, MapEventProps, MapProps, MapProviderProps, MapRegistry, MarkerProps, MaskProps, NavigationControlProps, PopupProps, ScaleControlProps, SimpleStyleProps, SourceProps };
|
package/dist/index.js
CHANGED
|
@@ -329,6 +329,7 @@ function GeocoderControl({
|
|
|
329
329
|
placeholder = DEFAULT_LABEL,
|
|
330
330
|
marker = true,
|
|
331
331
|
fitBounds = true,
|
|
332
|
+
onSearch,
|
|
332
333
|
onResult,
|
|
333
334
|
onError
|
|
334
335
|
} = {}) {
|
|
@@ -336,6 +337,7 @@ function GeocoderControl({
|
|
|
336
337
|
const [input, setInput] = useState2("");
|
|
337
338
|
const [found, setFound] = useState2(null);
|
|
338
339
|
const [failed, setFailed] = useState2(false);
|
|
340
|
+
const [searching, setSearching] = useState2(false);
|
|
339
341
|
const seqRef = useRef2(0);
|
|
340
342
|
const aliveRef = useRef2(true);
|
|
341
343
|
useEffect5(() => {
|
|
@@ -344,19 +346,27 @@ function GeocoderControl({
|
|
|
344
346
|
aliveRef.current = false;
|
|
345
347
|
};
|
|
346
348
|
}, []);
|
|
347
|
-
const latest = useRef2({ fitBounds, onResult, onError });
|
|
349
|
+
const latest = useRef2({ fitBounds, onSearch, onResult, onError });
|
|
348
350
|
useEffect5(() => {
|
|
349
|
-
latest.current = { fitBounds, onResult, onError };
|
|
351
|
+
latest.current = { fitBounds, onSearch, onResult, onError };
|
|
350
352
|
});
|
|
351
353
|
async function search(e) {
|
|
352
354
|
e.preventDefault();
|
|
353
355
|
const address = input;
|
|
354
356
|
const seq = ++seqRef.current;
|
|
357
|
+
try {
|
|
358
|
+
latest.current.onSearch?.(address);
|
|
359
|
+
} catch (err) {
|
|
360
|
+
setSearching(false);
|
|
361
|
+
throw err;
|
|
362
|
+
}
|
|
363
|
+
setSearching(true);
|
|
355
364
|
let result;
|
|
356
365
|
try {
|
|
357
366
|
result = await geocode(address);
|
|
358
367
|
} catch (err) {
|
|
359
368
|
if (!aliveRef.current || seq !== seqRef.current) return;
|
|
369
|
+
setSearching(false);
|
|
360
370
|
setFailed(true);
|
|
361
371
|
latest.current.onError?.(
|
|
362
372
|
err instanceof GeocodeError ? err : new GeocodeError(String(err), "ERROR")
|
|
@@ -364,6 +374,7 @@ function GeocoderControl({
|
|
|
364
374
|
return;
|
|
365
375
|
}
|
|
366
376
|
if (!aliveRef.current || seq !== seqRef.current) return;
|
|
377
|
+
setSearching(false);
|
|
367
378
|
setFailed(false);
|
|
368
379
|
setFound(result);
|
|
369
380
|
if (latest.current.fitBounds) map.fitBounds(result.bounds);
|
|
@@ -374,6 +385,7 @@ function GeocoderControl({
|
|
|
374
385
|
"form",
|
|
375
386
|
{
|
|
376
387
|
onSubmit: search,
|
|
388
|
+
"aria-busy": searching,
|
|
377
389
|
style: { display: "flex", gap: 4, padding: 4, background: "#fff" },
|
|
378
390
|
children: [
|
|
379
391
|
/* @__PURE__ */ jsx(
|
|
@@ -672,7 +684,9 @@ import {
|
|
|
672
684
|
DEFAULT_STAGE,
|
|
673
685
|
GeoloniaMap
|
|
674
686
|
} from "@geolonia/maps-core";
|
|
675
|
-
import {
|
|
687
|
+
import {
|
|
688
|
+
MapMouseEvent
|
|
689
|
+
} from "maplibre-gl";
|
|
676
690
|
import {
|
|
677
691
|
forwardRef,
|
|
678
692
|
useContext as useContext3,
|
|
@@ -823,12 +837,15 @@ var Map = forwardRef((props, ref) => {
|
|
|
823
837
|
if (!interactiveLayerIdsRef.current?.length) return;
|
|
824
838
|
const features = e.features ?? queryFeatures(e.point);
|
|
825
839
|
const nowOver = features.length > 0;
|
|
840
|
+
e.features = features;
|
|
841
|
+
if (nowOver) hoverState.last = e;
|
|
826
842
|
if (nowOver && !hoverState.over) {
|
|
827
843
|
hoverState.over = true;
|
|
828
|
-
handlersRef.current.onMouseEnter?.(
|
|
844
|
+
handlersRef.current.onMouseEnter?.(e);
|
|
829
845
|
} else if (!nowOver && hoverState.over) {
|
|
830
846
|
hoverState.over = false;
|
|
831
|
-
|
|
847
|
+
hoverState.last = void 0;
|
|
848
|
+
handlersRef.current.onMouseLeave?.(e);
|
|
832
849
|
}
|
|
833
850
|
};
|
|
834
851
|
instance.on("mousemove", hoverMove);
|
|
@@ -844,6 +861,7 @@ var Map = forwardRef((props, ref) => {
|
|
|
844
861
|
if (interactiveLayerIdsRef.current?.length) {
|
|
845
862
|
if (!hoverState.over) return;
|
|
846
863
|
hoverState.over = false;
|
|
864
|
+
hoverState.last = void 0;
|
|
847
865
|
handlersRef.current.onMouseLeave?.(
|
|
848
866
|
new MapMouseEvent("mouseleave", instance, ev, { features: [] })
|
|
849
867
|
);
|
|
@@ -868,8 +886,15 @@ var Map = forwardRef((props, ref) => {
|
|
|
868
886
|
}, []);
|
|
869
887
|
useEffect11(() => {
|
|
870
888
|
if (!hoverStateRef.current.over) return;
|
|
889
|
+
const last = hoverStateRef.current.last;
|
|
871
890
|
hoverStateRef.current.over = false;
|
|
872
|
-
|
|
891
|
+
hoverStateRef.current.last = void 0;
|
|
892
|
+
if (!last) return;
|
|
893
|
+
handlersRef.current.onMouseLeave?.(
|
|
894
|
+
Object.assign(Object.create(Object.getPrototypeOf(last)), last, {
|
|
895
|
+
features: []
|
|
896
|
+
})
|
|
897
|
+
);
|
|
873
898
|
}, [layerIdsKey]);
|
|
874
899
|
useEffect11(() => {
|
|
875
900
|
if (!map || cursor === void 0) return;
|