@@ -38,6 +38,15 @@ const NO_TOKEN_WARNING = 'A valid API access token is required to use Mapbox dat
38
38
39
39
function noop ( ) { }
40
40
41
+ export function getViewport ( { props, width, height} ) {
42
+ return new WebMercatorViewport ( {
43
+ ...props ,
44
+ ...props . viewState ,
45
+ width,
46
+ height
47
+ } ) ;
48
+ }
49
+
41
50
const UNAUTHORIZED_ERROR_CODE = 401 ;
42
51
43
52
const CONTAINER_STYLE = {
@@ -112,7 +121,7 @@ function getRefHandles(mapboxRef) {
112
121
113
122
const StaticMap = forwardRef ( ( props , ref ) => {
114
123
const [ accessTokenValid , setTokenState ] = useState ( true ) ;
115
- const [ size , setSize ] = useState ( [ 0 , 0 ] ) ;
124
+ const [ size , setSize ] = useState ( { width : 0 , height : 0 } ) ;
116
125
const mapboxRef = useRef ( null ) ;
117
126
const mapDivRef = useRef ( null ) ;
118
127
const containerRef = useRef ( null ) ;
@@ -127,9 +136,8 @@ const StaticMap = forwardRef((props, ref) => {
127
136
// Initialize
128
137
const mapbox = new Mapbox ( {
129
138
...props ,
139
+ ...size ,
130
140
mapboxgl, // Handle to mapbox-gl library
131
- width : size [ 0 ] ,
132
- height : size [ 1 ] ,
133
141
container : mapDivRef . current ,
134
142
onError : evt => {
135
143
const statusCode = ( evt . error && evt . error . status ) || evt . status ;
@@ -150,7 +158,7 @@ const StaticMap = forwardRef((props, ref) => {
150
158
const resizeObserver = new ResizeObserver ( entries => {
151
159
if ( entries [ 0 ] . contentRect ) {
152
160
const { width, height} = entries [ 0 ] . contentRect ;
153
- setSize ( [ width , height ] ) ;
161
+ setSize ( { width, height} ) ;
154
162
props . onResize ( { width, height} ) ;
155
163
}
156
164
} ) ;
@@ -166,12 +174,7 @@ const StaticMap = forwardRef((props, ref) => {
166
174
167
175
useIsomorphicLayoutEffect ( ( ) => {
168
176
if ( mapboxRef . current ) {
169
- mapboxRef . current . setProps (
170
- Object . assign ( { } , props , {
171
- width : size [ 0 ] ,
172
- height : size [ 1 ]
173
- } )
174
- ) ;
177
+ mapboxRef . current . setProps ( { ...props , ...size } ) ;
175
178
}
176
179
} ) ;
177
180
@@ -191,14 +194,7 @@ const StaticMap = forwardRef((props, ref) => {
191
194
< MapContextProvider
192
195
value = { {
193
196
...context ,
194
- viewport :
195
- context . viewport ||
196
- new WebMercatorViewport ( {
197
- ...props ,
198
- ...props . viewState ,
199
- width : size [ 0 ] ,
200
- height : size [ 1 ]
201
- } ) ,
197
+ viewport : context . viewport || getViewport ( { map, props, ...size } ) ,
202
198
map,
203
199
container : context . container || containerRef . current
204
200
} }
0 commit comments