@@ -7,7 +7,7 @@ import { Button } from '@atb/components/button';
7
7
import { MonoIcon } from '@atb/components/icon' ;
8
8
import { ComponentText , useTranslation } from '@atb/translations' ;
9
9
import { FocusScope } from '@react-aria/focus' ;
10
- import { ZOOM_LEVEL , defaultPosition } from './utils' ;
10
+ import { ZOOM_LEVEL , defaultPosition , getMapBounds } from './utils' ;
11
11
import { useMapInteractions } from './use-map-interactions' ;
12
12
import { useFullscreenMap } from './use-fullscreen-map' ;
13
13
import { useMapPin } from './use-map-pin' ;
@@ -17,25 +17,31 @@ import { MapLegType, Position } from './types';
17
17
import { useMapTariffZones } from './use-map-tariff-zones' ;
18
18
19
19
export type MapProps = {
20
- position ?: Position ;
21
20
layer ?: string ;
22
21
onSelectStopPlace ?: ( id : string ) => void ;
23
- mapLegs ?: MapLegType [ ] ;
24
- initialZoom ?: number ;
25
- } ;
22
+ } & (
23
+ | {
24
+ position : Position ;
25
+ initialZoom : number ;
26
+ }
27
+ | {
28
+ mapLegs : MapLegType [ ] ;
29
+ }
30
+ | { }
31
+ ) ;
26
32
27
- export function Map ( {
28
- position = defaultPosition ,
29
- layer,
30
- onSelectStopPlace,
31
- mapLegs,
32
- initialZoom = ZOOM_LEVEL ,
33
- } : MapProps ) {
33
+ export function Map ( { layer, onSelectStopPlace, ...props } : MapProps ) {
34
34
const mapWrapper = useRef < HTMLDivElement > ( null ) ;
35
35
const mapContainer = useRef < HTMLDivElement > ( null ) ;
36
36
const map = useRef < mapboxgl . Map > ( ) ;
37
37
const { t } = useTranslation ( ) ;
38
38
39
+ const mapLegs = hasMapLegs ( props ) ? props . mapLegs : undefined ;
40
+ const { position, initialZoom } = hasInitialPosition ( props )
41
+ ? props
42
+ : { position : defaultPosition , initialZoom : ZOOM_LEVEL } ;
43
+ const bounds = mapLegs ? getMapBounds ( mapLegs ) : undefined ;
44
+
39
45
useEffect ( ( ) => {
40
46
if ( ! mapContainer . current ) return ;
41
47
@@ -48,6 +54,7 @@ export function Map({
48
54
style : mapboxData . style ,
49
55
center : position ,
50
56
zoom : initialZoom ,
57
+ bounds, // If bounds is specified, it overrides center and zoom constructor options.
51
58
} ) ;
52
59
53
60
return ( ) => map . current ?. remove ( ) ;
@@ -62,16 +69,6 @@ export function Map({
62
69
useMapLegs ( map , mapLegs ) ;
63
70
useMapTariffZones ( map ) ;
64
71
65
- useEffect ( ( ) => {
66
- if ( map . current ) {
67
- map . current . flyTo ( {
68
- center : position ,
69
- zoom : initialZoom ,
70
- speed : 2 ,
71
- } ) ;
72
- }
73
- } , [ position , initialZoom ] ) ;
74
-
75
72
return (
76
73
< div className = { style . map } aria-hidden = "true" >
77
74
< Button
@@ -122,3 +119,13 @@ export function Map({
122
119
</ div >
123
120
) ;
124
121
}
122
+
123
+ function hasInitialPosition (
124
+ a : any ,
125
+ ) : a is { position : Position ; initialZoom : number } {
126
+ return a . position && a . initialZoom ;
127
+ }
128
+
129
+ function hasMapLegs ( a : any ) : a is { mapLegs : MapLegType [ ] } {
130
+ return a . mapLegs ;
131
+ }
0 commit comments