18
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
// THE SOFTWARE.
20
20
import * as React from 'react' ;
21
- import { useState , useRef , useContext , useImperativeHandle , forwardRef } from 'react' ;
21
+ import { useState , useRef , useCallback , useContext , useImperativeHandle , forwardRef } from 'react' ;
22
22
import * as PropTypes from 'prop-types' ;
23
23
24
24
import WebMercatorViewport from 'viewport-mercator-project' ;
@@ -110,16 +110,13 @@ function getRefHandles(mapboxRef) {
110
110
} ;
111
111
}
112
112
113
- function preventScroll ( event ) {
114
- event . target . scrollTo ( 0 , 0 ) ;
115
- }
116
-
117
113
const StaticMap = forwardRef ( ( props , ref ) => {
118
114
const [ accessTokenValid , setTokenState ] = useState ( true ) ;
119
115
const [ size , setSize ] = useState ( [ 0 , 0 ] ) ;
120
116
const mapboxRef = useRef ( null ) ;
121
117
const mapDivRef = useRef ( null ) ;
122
118
const containerRef = useRef ( null ) ;
119
+ const overlayRef = useRef ( null ) ;
123
120
const context = useContext ( MapContext ) ;
124
121
125
122
useIsomorphicLayoutEffect ( ( ) => {
@@ -184,6 +181,12 @@ const StaticMap = forwardRef((props, ref) => {
184
181
// Note: this is not a recommended pattern in React FC - Keeping for backward compatibility
185
182
useImperativeHandle ( ref , ( ) => getRefHandles ( mapboxRef ) , [ ] ) ;
186
183
184
+ const preventScroll = useCallback ( ( { target} ) => {
185
+ if ( target === overlayRef . current ) {
186
+ target . scrollTo ( 0 , 0 ) ;
187
+ }
188
+ } , [ ] ) ;
189
+
187
190
const overlays = map && (
188
191
< MapContextProvider
189
192
value = { {
@@ -203,6 +206,7 @@ const StaticMap = forwardRef((props, ref) => {
203
206
< div
204
207
key = "map-overlays"
205
208
className = "overlays"
209
+ ref = { overlayRef }
206
210
// @ts -ignore
207
211
style = { CONTAINER_STYLE }
208
212
onScroll = { preventScroll }
0 commit comments