Skip to content

Commit

Permalink
Only block scrolling on the overlay container itself (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed May 12, 2021
1 parent 0b27376 commit 5dc8b23
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/components/static-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import * as React from 'react';
import {useState, useRef, useContext, useImperativeHandle, forwardRef} from 'react';
import {useState, useRef, useCallback, useContext, useImperativeHandle, forwardRef} from 'react';
import * as PropTypes from 'prop-types';

import WebMercatorViewport from 'viewport-mercator-project';
Expand Down Expand Up @@ -110,16 +110,13 @@ function getRefHandles(mapboxRef) {
};
}

function preventScroll(event) {
event.target.scrollTo(0, 0);
}

const StaticMap = forwardRef((props, ref) => {
const [accessTokenValid, setTokenState] = useState(true);
const [size, setSize] = useState([0, 0]);
const mapboxRef = useRef(null);
const mapDivRef = useRef(null);
const containerRef = useRef(null);
const overlayRef = useRef(null);
const context = useContext(MapContext);

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

const preventScroll = useCallback(({target}) => {
if (target === overlayRef.current) {
target.scrollTo(0, 0);
}
}, []);

const overlays = map && (
<MapContextProvider
value={{
Expand All @@ -203,6 +206,7 @@ const StaticMap = forwardRef((props, ref) => {
<div
key="map-overlays"
className="overlays"
ref={overlayRef}
// @ts-ignore
style={CONTAINER_STYLE}
onScroll={preventScroll}
Expand Down

0 comments on commit 5dc8b23

Please sign in to comment.