Skip to content

Commit 7541b26

Browse files
committed
Updated focus button for bounds
1 parent 636b026 commit 7541b26

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

packages/map-interface/src/location-panel/header.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
LocationFocusButton,
88
useFocusState,
99
isCentered,
10+
BoundsFocusButton,
1011
} from "@macrostrat/mapbox-react";
1112

1213
const h = hyper.styled(styles);
@@ -66,28 +67,37 @@ function CopyLinkButton({ itemName, children, onClick, ...rest }) {
6667
}
6768

6869
export interface InfoDrawerHeaderProps {
69-
onClose: () => void;
70-
position: mapboxgl.LngLat;
70+
onClose?: () => void;
71+
position?: mapboxgl.LngLat;
7172
zoom?: number;
7273
elevation?: number;
7374
showCopyPositionButton?: boolean;
75+
bounds?: mapboxgl.LngLatBounds;
7476
}
7577

7678
export function InfoDrawerHeader(props: InfoDrawerHeaderProps) {
7779
const {
7880
onClose,
7981
position,
82+
bounds,
8083
zoom = 7,
8184
elevation,
8285
showCopyPositionButton,
8386
children,
8487
} = props;
8588

86-
return h("header.location-panel-header", [
87-
h.if(position != null)(PositionButton, {
89+
let leftButton = null;
90+
if (bounds != null) {
91+
leftButton = h(BoundsFocusButton, { bounds });
92+
} else if (position != null) {
93+
leftButton = h(PositionButton, {
8894
position,
8995
showCopyLink: showCopyPositionButton,
90-
}),
96+
});
97+
}
98+
99+
return h("header.location-panel-header", [
100+
leftButton,
91101
children,
92102
h("div.spacer"),
93103
h.if(position != null)(LngLatCoords, {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ function DetailPanelMap(props) {
3131
}
3232

3333
export function PositionInformation(props) {
34-
const { position, onClose, title, children, ...rest } = props;
34+
const { position, bounds, onClose, title, children, ...rest } = props;
3535

3636
const detailPanel = h(
3737
LocationPanel,
3838
{
3939
position,
40+
bounds,
4041
title,
4142
onClose,
4243
},
@@ -48,6 +49,7 @@ export function PositionInformation(props) {
4849
{
4950
...rest,
5051
detailPanel,
52+
bounds,
5153
},
5254
[
5355
h.if(position != null)(MapMarker, {
@@ -136,7 +138,7 @@ export const WithBounds: Story = {
136138
bounds,
137139
position: null,
138140
mapPosition: null,
139-
title: "New York City",
141+
title: "Where it all happens",
140142
onClose() {
141143
console.log("Close");
142144
},

packages/mapbox-react/src/focus-state.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,43 @@ export function LocationFocusButton({
451451
[_isCentered ? null : h("span.recenter-label", "Recenter")]
452452
);
453453
}
454+
455+
export function BoundsFocusButton({
456+
bounds,
457+
className,
458+
easeDuration = 800,
459+
focusState = null,
460+
...rest
461+
}) {
462+
const map = useMapRef();
463+
464+
const center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2];
465+
466+
const defaultFocusState = useFocusState({ lat: center[1], lng: center[0] });
467+
focusState ??= defaultFocusState;
468+
const _isCentered = isCentered(focusState);
469+
470+
return h(
471+
Button,
472+
{
473+
minimal: true,
474+
icon: "detection",
475+
onClick() {
476+
if (focusState == PositionFocusState.CENTERED) {
477+
map.current?.resetNorth();
478+
} else {
479+
let opts = { duration: easeDuration };
480+
map.current?.fitBounds(bounds, opts);
481+
}
482+
},
483+
className: classNames(
484+
"recenter-button",
485+
className,
486+
classNameForFocusState(focusState)
487+
),
488+
intent: intentForFocusState(focusState),
489+
...rest,
490+
},
491+
[_isCentered ? null : h("span.recenter-label", "Recenter")]
492+
);
493+
}

0 commit comments

Comments
 (0)