Skip to content

Commit 7cea725

Browse files
committed
Only block scrolling on the overlay container itself (#1465)
1 parent c13b4a0 commit 7cea725

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/components/static-map.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020
import * as React from 'react';
21-
import {useState, useRef, useContext, useImperativeHandle, forwardRef} from 'react';
21+
import {useState, useRef, useCallback, useContext, useImperativeHandle, forwardRef} from 'react';
2222
import * as PropTypes from 'prop-types';
2323

2424
import WebMercatorViewport from 'viewport-mercator-project';
@@ -110,16 +110,13 @@ function getRefHandles(mapboxRef) {
110110
};
111111
}
112112

113-
function preventScroll(event) {
114-
event.target.scrollTo(0, 0);
115-
}
116-
117113
const StaticMap = forwardRef((props, ref) => {
118114
const [accessTokenValid, setTokenState] = useState(true);
119115
const [size, setSize] = useState([0, 0]);
120116
const mapboxRef = useRef(null);
121117
const mapDivRef = useRef(null);
122118
const containerRef = useRef(null);
119+
const overlayRef = useRef(null);
123120
const context = useContext(MapContext);
124121

125122
useIsomorphicLayoutEffect(() => {
@@ -184,6 +181,12 @@ const StaticMap = forwardRef((props, ref) => {
184181
// Note: this is not a recommended pattern in React FC - Keeping for backward compatibility
185182
useImperativeHandle(ref, () => getRefHandles(mapboxRef), []);
186183

184+
const preventScroll = useCallback(({target}) => {
185+
if (target === overlayRef.current) {
186+
target.scrollTo(0, 0);
187+
}
188+
}, []);
189+
187190
const overlays = map && (
188191
<MapContextProvider
189192
value={{
@@ -203,6 +206,7 @@ const StaticMap = forwardRef((props, ref) => {
203206
<div
204207
key="map-overlays"
205208
className="overlays"
209+
ref={overlayRef}
206210
// @ts-ignore
207211
style={CONTAINER_STYLE}
208212
onScroll={preventScroll}

0 commit comments

Comments
 (0)