Skip to content

Commit 636b026

Browse files
committed
Added story to start go-to-bounds functionality
1 parent cb910e4 commit 636b026

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

packages/map-interface/stories/map-details-panel.stories.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
} from "../src";
1010
import h from "@macrostrat/hyper";
1111
import { DarkModeProvider } from "@macrostrat/ui-components";
12+
import { useMapRef, useMapStatus } from "@macrostrat/mapbox-react";
13+
import { useEffect } from "react";
1214

1315
const mapboxToken = import.meta.env.VITE_MAPBOX_API_TOKEN;
1416

1517
function DetailPanelMap(props) {
16-
const { mapPosition, children, ...rest } = props;
18+
const { mapPosition, children, bounds, ...rest } = props;
1719

1820
const style = useBasicStylePair();
1921

@@ -24,12 +26,12 @@ function DetailPanelMap(props) {
2426
contextPanel: null,
2527
...rest,
2628
},
27-
h(MapView, { style, mapPosition, mapboxToken }, children)
29+
h(MapView, { style, mapPosition, mapboxToken, bounds }, children)
2830
);
2931
}
3032

3133
export function PositionInformation(props) {
32-
const { position, onClose, title, ...rest } = props;
34+
const { position, onClose, title, children, ...rest } = props;
3335

3436
const detailPanel = h(
3537
LocationPanel,
@@ -48,9 +50,10 @@ export function PositionInformation(props) {
4850
detailPanel,
4951
},
5052
[
51-
h(MapMarker, {
53+
h.if(position != null)(MapMarker, {
5254
position,
5355
}),
56+
children,
5457
]
5558
);
5659
}
@@ -125,3 +128,61 @@ export const NotCloseable = {
125128
onClose: null,
126129
},
127130
};
131+
132+
const bounds = [-74.2591, 40.4774, -73.7004, 40.9176];
133+
134+
export const WithBounds: Story = {
135+
args: {
136+
bounds,
137+
position: null,
138+
mapPosition: null,
139+
title: "New York City",
140+
onClose() {
141+
console.log("Close");
142+
},
143+
children: [h(MapBoundsLayer, { bounds })],
144+
},
145+
};
146+
147+
function MapBoundsLayer(props) {
148+
const { bounds } = props;
149+
const isLoaded = useMapStatus((map) => map.isStyleLoaded);
150+
const ref = useMapRef();
151+
useEffect(() => {
152+
const map = ref.current;
153+
if (map == null) return;
154+
if (!isLoaded) return;
155+
156+
map.addSource("bounds", {
157+
type: "geojson",
158+
data: {
159+
type: "Feature",
160+
geometry: {
161+
type: "Polygon",
162+
coordinates: [
163+
[
164+
[bounds[0], bounds[1]],
165+
[bounds[2], bounds[1]],
166+
[bounds[2], bounds[3]],
167+
[bounds[0], bounds[3]],
168+
[bounds[0], bounds[1]],
169+
],
170+
],
171+
},
172+
},
173+
});
174+
175+
map.addLayer({
176+
id: "bounds",
177+
type: "line",
178+
source: "bounds",
179+
layout: {},
180+
paint: {
181+
"line-color": "red",
182+
"line-width": 2,
183+
},
184+
});
185+
}, [isLoaded]);
186+
187+
return null;
188+
}

0 commit comments

Comments
 (0)