Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Désactive le scroll zoom dans une iframe #75

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions components/react-map-gl/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState, useCallback, useContext} from 'react'
import ReactMapGL, {Source, Layer, Popup} from 'react-map-gl'
import ReactMapGL, {NavigationControl, Source, Layer, Popup} from 'react-map-gl'
import {Maximize2} from 'react-feather'

import {AppContext} from '../../pages'
Expand All @@ -16,8 +16,6 @@ const settings = {
}

const Map = () => {
const [selectedMapIdx, setSelectedMapIdx] = useState(1)

const {
selectedLocationReport,
setSelectedLocation,
Expand All @@ -30,6 +28,11 @@ const Map = () => {

const [map, setMap] = useState()
const [hovered, setHovered] = useState(null)
const [selectedMapIdx, setSelectedMapIdx] = useState(1)
const [showZoomMessage, setShowZoomMessage] = useState(false)
const [zoomMessageCoolDown, setZoomMessageCoolDown] = useState(true)

const enableZoom = isIframe && isMobileDevice

const mapRef = useCallback(ref => {
if (ref) {
Expand Down Expand Up @@ -68,33 +71,56 @@ const Map = () => {
setHovered(null)
}

const displayAlert = () => {
if (!showZoomMessage && zoomMessageCoolDown) {
setShowZoomMessage(true)
setZoomMessageCoolDown(false)
setTimeout(() => setShowZoomMessage(null), 5000)
setTimeout(() => setZoomMessageCoolDown(true), 60000)
}
}

return (
<div className='map-container'>
<div className='controls'>
<div className='control'>
<div className='custom-control'>
<MapSelector mapIdx={selectedMapIdx} selectMap={setSelectedMapIdx} />
</div>

{isIframe && (
<div className='control maximize'>
<div className='custom-control maximize'>
<a href={SITE_URL} target='_top'><Maximize2 style={{verticalAlign: 'middle'}} /></a>
</div>
)}
</div>

{showZoomMessage && (
<div className='zoom-message'>
<p>Le zoom à la souris est désactivé.</p>
<p>Merci d’utiliser les contrôles +/- situés en haut à droite.</p>
</div>
)}

<ReactMapGL
reuseMaps
ref={mapRef}
{...viewport}
width='100%'
height='100%'
mapStyle='https://etalab-tiles.fr/styles/osm-bright/style.json'
scrollZoom={enableZoom}
{...settings}
interactiveLayerIds={maps[selectedMapIdx].layers.map(layer => layer.id)}
onViewportChange={setViewport}
onHover={isMobileDevice ? null : onHover}
onClick={onClick}
onWheel={enableZoom ? null : displayAlert}
>
{!enableZoom && (
<div className='control navigation'>
<NavigationControl showCompass={false} />
</div>
)}

<Source
type='geojson'
Expand Down Expand Up @@ -146,13 +172,39 @@ const Map = () => {
padding: 0.5em;
}

.control {
.custom-control {
background-color: #000000aa;
color: #fff;
border-radius: 4px;
margin: 0;
}

.control {
position: absolute;
margin: 0.5em;
}

.navigation {
top: 40px;
right: 0;
}

.zoom-message {
z-index: 999;
position: absolute;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background-color: #000000aa;
color: #fff;
padding: 30%;
font-size: xx-large;
text-align: center;
}

.maximize {
display: flex;
right: 0;
Expand Down