Michael
Michael

Reputation: 421

React Leaflet popups not working on mobile devices

I'm using react-leaflet in an application and it works as intended unless it is viewed from a mobile device (both physical and through dev tools). On mobile devices, the popups will appear and immediately disappear after about 0.2 seconds.

Some points to note

Here is the issue replicated in CodeSandbox. If you click the button to pop out the browser window, switch it to view as a mobile device in dev tools, and then refresh the page, you can see what happens.

My component looks like this:

  export const MapView: React.FC<IMapViewProps> = ({
  ...
}) => {
 
  const Markers = data.map(({ location, name, id, events }) => (
    <Marker
      position={[location.lat, location.lon]}
      key={id}
      eventHandlers={{
        click: () => {
          console.log("clicked"); // THIS FIRES TWICE
        },
      }}
    >
      <Popup>
        <span>{name}</span>
      </Popup>
    </Marker>
  ));
    
  return (
    <div>
      <div>
        <br></br>
       IF I TAP HERE ONLY ONE CLICK EVENT IS RECORDED SO IT MUST BE IN THE MAPCONTAINER
        <br></br>
      </div>
      <MapContainer
        center={[55.9533, -3.1883]}
        zoom={10}
        scrollWheelZoom={false}
        style={{ height: "100vh" }}
        className={styles.container}
        doubleClickZoom
      >
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
        {Markers}
      </MapContainer>
    </div>
  );
};

If I can provide any other information please let me know. Thanks.

Upvotes: 5

Views: 2211

Answers (1)

Michael
Michael

Reputation: 421

As @SethLutske pointed out this issue seems to stem from leaflet.

Adding tap={false} to the MapContainer seems to have solved this issue.

 <MapContainer
    center={[55.9533, -3.1883]}
    zoom={10}
    scrollWheelZoom={false}
    style={{ height: "100vh" }}
    className={styles.container}
    doubleClickZoom
    tap={false}
  >
     ...
  </MapContainer>

Upvotes: 5

Related Questions