@classic-homes/maps-react 0.1.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.
- package/README.md +443 -0
- package/dist/index.cjs +2466 -0
- package/dist/index.d.cts +423 -0
- package/dist/index.d.ts +423 -0
- package/dist/index.js +2294 -0
- package/package.json +79 -0
package/README.md
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
# @classic-homes/maps-react
|
|
2
|
+
|
|
3
|
+
React components for MapLibre GL maps in the Classic Theme design system.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @classic-homes/maps-react maplibre-gl
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Declarative Components**: React-friendly API for MapLibre GL
|
|
14
|
+
- **7 Layer Types**: Fill, Line, Circle, Symbol, FillExtrusion, Heatmap, Raster
|
|
15
|
+
- **Controls**: Navigation, Scale, Geolocation, Legend, Coordinate Display
|
|
16
|
+
- **Clustering**: Built-in cluster support with expansion utilities
|
|
17
|
+
- **Deck.gl Integration**: Optional overlay for advanced visualizations
|
|
18
|
+
- **Accessibility**: ARIA labels, keyboard navigation, reduced motion support
|
|
19
|
+
- **Theme Integration**: Light/dark themes from design tokens
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { Map, MapContainer, Source, FillLayer } from '@classic-homes/maps-react';
|
|
25
|
+
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
26
|
+
|
|
27
|
+
function MyMap() {
|
|
28
|
+
return (
|
|
29
|
+
<MapContainer className="h-[500px]">
|
|
30
|
+
<Map
|
|
31
|
+
initialCenter={[-122.4, 37.8]}
|
|
32
|
+
initialZoom={12}
|
|
33
|
+
onClick={(e) => console.log('Clicked:', e.lngLat)}
|
|
34
|
+
>
|
|
35
|
+
<Source id="lots" data={geojsonData}>
|
|
36
|
+
<FillLayer id="lots-fill" fillColor="#22c55e" fillOpacity={0.7} />
|
|
37
|
+
</Source>
|
|
38
|
+
</Map>
|
|
39
|
+
</MapContainer>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Components
|
|
45
|
+
|
|
46
|
+
### Map Container
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { MapContainer, MapSkeleton, MapError, MapEmpty } from '@classic-homes/maps-react';
|
|
50
|
+
|
|
51
|
+
// Responsive container with loading states
|
|
52
|
+
<MapContainer className="h-[500px]">
|
|
53
|
+
{isLoading && <MapSkeleton />}
|
|
54
|
+
{error && <MapError message={error} onRetry={retry} />}
|
|
55
|
+
{isEmpty && <MapEmpty message="No data available" />}
|
|
56
|
+
{data && <Map>...</Map>}
|
|
57
|
+
</MapContainer>;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Core Map
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { Map, useMap, useMapInstance } from '@classic-homes/maps-react';
|
|
64
|
+
|
|
65
|
+
<Map
|
|
66
|
+
// Initial viewport
|
|
67
|
+
initialCenter={[-122.4, 37.8]}
|
|
68
|
+
initialZoom={12}
|
|
69
|
+
initialBearing={0}
|
|
70
|
+
initialPitch={0}
|
|
71
|
+
// Bounds fitting
|
|
72
|
+
bounds={bounds}
|
|
73
|
+
fitBoundsOptions={{ padding: 50 }}
|
|
74
|
+
// Style
|
|
75
|
+
style={lightBasicStyle}
|
|
76
|
+
theme="light"
|
|
77
|
+
// Interactivity
|
|
78
|
+
interactive={true}
|
|
79
|
+
scrollZoom={true}
|
|
80
|
+
dragPan={true}
|
|
81
|
+
// Events (see Callback Memoization section)
|
|
82
|
+
onClick={handleClick}
|
|
83
|
+
onHover={handleHover}
|
|
84
|
+
onMove={handleMove}
|
|
85
|
+
onLoad={handleLoad}
|
|
86
|
+
/>;
|
|
87
|
+
|
|
88
|
+
// Access map instance in child components
|
|
89
|
+
function MapChild() {
|
|
90
|
+
const { map, loaded } = useMap();
|
|
91
|
+
// or
|
|
92
|
+
const map = useMapInstance();
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Data Sources
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { Source } from '@classic-homes/maps-react';
|
|
100
|
+
|
|
101
|
+
// GeoJSON data
|
|
102
|
+
<Source id="features" data={geojsonFeatureCollection}>
|
|
103
|
+
{/* Layers go here */}
|
|
104
|
+
</Source>
|
|
105
|
+
|
|
106
|
+
// URL source
|
|
107
|
+
<Source id="remote" data="https://example.com/data.geojson">
|
|
108
|
+
{/* Layers go here */}
|
|
109
|
+
</Source>
|
|
110
|
+
|
|
111
|
+
// With clustering
|
|
112
|
+
<Source
|
|
113
|
+
id="points"
|
|
114
|
+
data={pointData}
|
|
115
|
+
cluster={true}
|
|
116
|
+
clusterRadius={50}
|
|
117
|
+
clusterMaxZoom={14}
|
|
118
|
+
>
|
|
119
|
+
{/* Layers go here */}
|
|
120
|
+
</Source>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Layer Components
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import {
|
|
127
|
+
FillLayer,
|
|
128
|
+
LineLayer,
|
|
129
|
+
CircleLayer,
|
|
130
|
+
SymbolLayer,
|
|
131
|
+
FillExtrusionLayer,
|
|
132
|
+
HeatmapLayer,
|
|
133
|
+
} from '@classic-homes/maps-react';
|
|
134
|
+
|
|
135
|
+
// Fill layer (polygons)
|
|
136
|
+
<FillLayer
|
|
137
|
+
id="polygons"
|
|
138
|
+
source="features"
|
|
139
|
+
fillColor="#22c55e"
|
|
140
|
+
fillOpacity={0.7}
|
|
141
|
+
fillOutlineColor="#166534"
|
|
142
|
+
onClick={handleClick}
|
|
143
|
+
onHover={handleHover}
|
|
144
|
+
/>
|
|
145
|
+
|
|
146
|
+
// Line layer
|
|
147
|
+
<LineLayer
|
|
148
|
+
id="lines"
|
|
149
|
+
source="features"
|
|
150
|
+
lineColor="#3b82f6"
|
|
151
|
+
lineWidth={2}
|
|
152
|
+
lineCap="round"
|
|
153
|
+
/>
|
|
154
|
+
|
|
155
|
+
// Circle layer (points)
|
|
156
|
+
<CircleLayer
|
|
157
|
+
id="points"
|
|
158
|
+
source="features"
|
|
159
|
+
circleRadius={6}
|
|
160
|
+
circleColor="#ef4444"
|
|
161
|
+
circleStrokeWidth={2}
|
|
162
|
+
circleStrokeColor="#ffffff"
|
|
163
|
+
/>
|
|
164
|
+
|
|
165
|
+
// Symbol layer (labels/icons)
|
|
166
|
+
<SymbolLayer
|
|
167
|
+
id="labels"
|
|
168
|
+
source="features"
|
|
169
|
+
textField={['get', 'name']}
|
|
170
|
+
textSize={12}
|
|
171
|
+
iconImage="marker"
|
|
172
|
+
/>
|
|
173
|
+
|
|
174
|
+
// 3D extrusions
|
|
175
|
+
<FillExtrusionLayer
|
|
176
|
+
id="buildings"
|
|
177
|
+
source="features"
|
|
178
|
+
fillExtrusionHeight={['get', 'height']}
|
|
179
|
+
fillExtrusionColor="#94a3b8"
|
|
180
|
+
fillExtrusionOpacity={0.9}
|
|
181
|
+
/>
|
|
182
|
+
|
|
183
|
+
// Heatmap
|
|
184
|
+
<HeatmapLayer
|
|
185
|
+
id="heat"
|
|
186
|
+
source="features"
|
|
187
|
+
heatmapWeight={1}
|
|
188
|
+
heatmapIntensity={0.5}
|
|
189
|
+
heatmapRadius={30}
|
|
190
|
+
/>
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Markers and Popups
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
import { Marker, Popup, PopupCard, PopupHeader, PopupContent, PopupFooter } from '@classic-homes/maps-react';
|
|
197
|
+
|
|
198
|
+
// Custom marker with popup
|
|
199
|
+
<Marker
|
|
200
|
+
lngLat={[-122.4, 37.8]}
|
|
201
|
+
anchor="bottom"
|
|
202
|
+
draggable={true}
|
|
203
|
+
onClick={handleClick}
|
|
204
|
+
onDragEnd={handleDrag}
|
|
205
|
+
>
|
|
206
|
+
<div className="custom-marker">
|
|
207
|
+
<MapPinIcon />
|
|
208
|
+
</div>
|
|
209
|
+
</Marker>
|
|
210
|
+
|
|
211
|
+
// Popup with card styling
|
|
212
|
+
<Popup lngLat={[-122.4, 37.8]} anchor="bottom" closeOnClick={false}>
|
|
213
|
+
<PopupCard>
|
|
214
|
+
<PopupHeader>
|
|
215
|
+
<h3>Location Name</h3>
|
|
216
|
+
</PopupHeader>
|
|
217
|
+
<PopupContent>
|
|
218
|
+
<p>Description goes here</p>
|
|
219
|
+
</PopupContent>
|
|
220
|
+
<PopupFooter>
|
|
221
|
+
<button>View Details</button>
|
|
222
|
+
</PopupFooter>
|
|
223
|
+
</PopupCard>
|
|
224
|
+
</Popup>
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Controls
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { Legend, LegendGroup, CoordinateDisplay } from '@classic-homes/maps-react';
|
|
231
|
+
|
|
232
|
+
// Legend
|
|
233
|
+
<Legend
|
|
234
|
+
position="bottom-left"
|
|
235
|
+
title="Status"
|
|
236
|
+
items={[
|
|
237
|
+
{ label: 'Available', color: '#22c55e' },
|
|
238
|
+
{ label: 'Pending', color: '#f59e0b' },
|
|
239
|
+
{ label: 'Sold', color: '#ef4444' },
|
|
240
|
+
]}
|
|
241
|
+
/>
|
|
242
|
+
|
|
243
|
+
// Legend group with collapsible sections
|
|
244
|
+
<LegendGroup
|
|
245
|
+
position="bottom-left"
|
|
246
|
+
legends={[
|
|
247
|
+
{
|
|
248
|
+
title: 'Status',
|
|
249
|
+
items: [{ label: 'Available', color: '#22c55e' }],
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
title: 'Price',
|
|
253
|
+
gradient: {
|
|
254
|
+
stops: [
|
|
255
|
+
{ value: 0, color: '#eff6ff', label: '$0' },
|
|
256
|
+
{ value: 100, color: '#1e40af', label: '$1M+' },
|
|
257
|
+
],
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
]}
|
|
261
|
+
/>
|
|
262
|
+
|
|
263
|
+
// Coordinate display
|
|
264
|
+
<CoordinateDisplay position="bottom-right" precision={4} />
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Deck.gl Overlay
|
|
268
|
+
|
|
269
|
+
```tsx
|
|
270
|
+
import { DeckOverlay } from '@classic-homes/maps-react';
|
|
271
|
+
import { ScatterplotLayer } from '@deck.gl/layers';
|
|
272
|
+
|
|
273
|
+
<Map>
|
|
274
|
+
<DeckOverlay
|
|
275
|
+
layers={[
|
|
276
|
+
new ScatterplotLayer({
|
|
277
|
+
id: 'scatter',
|
|
278
|
+
data: points,
|
|
279
|
+
getPosition: (d) => d.coordinates,
|
|
280
|
+
getRadius: 100,
|
|
281
|
+
getFillColor: [255, 0, 0],
|
|
282
|
+
}),
|
|
283
|
+
]}
|
|
284
|
+
/>
|
|
285
|
+
</Map>;
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Callback Memoization
|
|
289
|
+
|
|
290
|
+
**Important**: Event callbacks passed to map components should be memoized to prevent unnecessary effect re-runs. Unmemoized callbacks can cause event handlers to be detached and reattached on every render.
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
import { useCallback, useMemo } from 'react';
|
|
294
|
+
|
|
295
|
+
function MyMap() {
|
|
296
|
+
// GOOD: Memoize callbacks
|
|
297
|
+
const handleClick = useCallback((e: MapClickEventParams) => {
|
|
298
|
+
console.log('Clicked:', e.lngLat);
|
|
299
|
+
}, []);
|
|
300
|
+
|
|
301
|
+
const handleHover = useCallback((e: MapHoverEventParams) => {
|
|
302
|
+
setHoveredFeature(e.features?.[0] ?? null);
|
|
303
|
+
}, []);
|
|
304
|
+
|
|
305
|
+
// GOOD: Memoize layer event handlers
|
|
306
|
+
const handleFeatureClick = useCallback((e: FeatureClickEventParams) => {
|
|
307
|
+
setSelectedId(e.features[0]?.id);
|
|
308
|
+
}, []);
|
|
309
|
+
|
|
310
|
+
return (
|
|
311
|
+
<Map onClick={handleClick} onHover={handleHover}>
|
|
312
|
+
<Source id="lots" data={data}>
|
|
313
|
+
<FillLayer id="lots-fill" fillColor="#22c55e" onClick={handleFeatureClick} />
|
|
314
|
+
</Source>
|
|
315
|
+
</Map>
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// BAD: Inline callbacks cause re-attachment
|
|
320
|
+
function BadExample() {
|
|
321
|
+
return (
|
|
322
|
+
<Map
|
|
323
|
+
// This creates a new function every render!
|
|
324
|
+
onClick={(e) => console.log(e)}
|
|
325
|
+
>
|
|
326
|
+
...
|
|
327
|
+
</Map>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## Hooks
|
|
333
|
+
|
|
334
|
+
### useMapTheme
|
|
335
|
+
|
|
336
|
+
```tsx
|
|
337
|
+
import { useMapTheme } from '@classic-homes/maps-react';
|
|
338
|
+
|
|
339
|
+
function ThemedMap() {
|
|
340
|
+
const { theme, style, isDark, toggleTheme } = useMapTheme();
|
|
341
|
+
|
|
342
|
+
return (
|
|
343
|
+
<>
|
|
344
|
+
<Map style={style} theme={theme} />
|
|
345
|
+
<button onClick={toggleTheme}>{isDark ? 'Light Mode' : 'Dark Mode'}</button>
|
|
346
|
+
</>
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### useReducedMotion
|
|
352
|
+
|
|
353
|
+
```tsx
|
|
354
|
+
import { useReducedMotion } from '@classic-homes/maps-react';
|
|
355
|
+
|
|
356
|
+
function AnimatedMap() {
|
|
357
|
+
const prefersReducedMotion = useReducedMotion();
|
|
358
|
+
|
|
359
|
+
const flyToLocation = (lngLat) => {
|
|
360
|
+
map.flyTo({
|
|
361
|
+
center: lngLat,
|
|
362
|
+
duration: prefersReducedMotion ? 0 : 1000,
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### useLayer
|
|
369
|
+
|
|
370
|
+
```tsx
|
|
371
|
+
import { useLayer } from '@classic-homes/maps-react';
|
|
372
|
+
|
|
373
|
+
// Create custom layer components
|
|
374
|
+
function CustomLayer({ id, source, ...props }) {
|
|
375
|
+
const { map, loaded } = useMap();
|
|
376
|
+
|
|
377
|
+
useLayer({
|
|
378
|
+
map,
|
|
379
|
+
loaded,
|
|
380
|
+
id,
|
|
381
|
+
source,
|
|
382
|
+
type: 'fill',
|
|
383
|
+
paint: {
|
|
384
|
+
'fill-color': props.color,
|
|
385
|
+
},
|
|
386
|
+
onClick: props.onClick,
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
## Cluster Utilities
|
|
394
|
+
|
|
395
|
+
```tsx
|
|
396
|
+
import {
|
|
397
|
+
isClusterFeature,
|
|
398
|
+
expandCluster,
|
|
399
|
+
getClusterBounds,
|
|
400
|
+
expandClusterToBounds,
|
|
401
|
+
} from '@classic-homes/maps-react';
|
|
402
|
+
|
|
403
|
+
function handleClusterClick(e: FeatureClickEventParams) {
|
|
404
|
+
const feature = e.features[0];
|
|
405
|
+
|
|
406
|
+
if (isClusterFeature(feature)) {
|
|
407
|
+
// Option 1: Zoom to expand
|
|
408
|
+
expandCluster(map, 'source-id', feature.id, (zoom) => {
|
|
409
|
+
map.flyTo({ center: e.lngLat, zoom });
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// Option 2: Fit to cluster bounds
|
|
413
|
+
expandClusterToBounds(map, 'source-id', feature, (bounds) => {
|
|
414
|
+
map.fitBounds(bounds, { padding: 50 });
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
## TypeScript
|
|
421
|
+
|
|
422
|
+
All components are fully typed:
|
|
423
|
+
|
|
424
|
+
```tsx
|
|
425
|
+
import type {
|
|
426
|
+
MapComponentProps,
|
|
427
|
+
SourceComponentProps,
|
|
428
|
+
FillLayerComponentProps,
|
|
429
|
+
MapClickEventParams,
|
|
430
|
+
FeatureClickEventParams,
|
|
431
|
+
FeatureHoverEventParams,
|
|
432
|
+
} from '@classic-homes/maps-react';
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
## Peer Dependencies
|
|
436
|
+
|
|
437
|
+
- `react` >= 18.0.0
|
|
438
|
+
- `react-dom` >= 18.0.0
|
|
439
|
+
- `maplibre-gl` >= 4.0.0 || >= 5.0.0
|
|
440
|
+
|
|
441
|
+
## License
|
|
442
|
+
|
|
443
|
+
MIT
|