close popup react-leaflet after user click on button in popup
Asked Answered
S

5

6

So basically want to make custom close for react-leaflet Popup component, seams that is not a big problem to do with native API leaflet but with react component from react-leaflet I can't find the solution.

Severn answered 29/8, 2018 at 20:41 Comment(1)
I also want to have onClick property in <Tooltip /> component, but seems not present currently. Any suggestions?Unrest
S
3

at the moment, the only way I found to close the popup is the following:

constructor(props){
    super(props);
    this.popup = React.createRef();
}

// the magic
closePopusOnClick(){
    this.popup.current.leafletElement.options.leaflet.map.closePopup();
}

render(){
    return <Marker position={[this.props.lat, this.props.lng]}>
        <Popup ref={this.popup}>
            <Button onClick={this.closePopusOnClick}>Close popup</Button>
        </Popup>
    </Marker>;
}

Hope it helps!

Stearns answered 25/2, 2019 at 20:59 Comment(0)
M
3

In "react-leaflet": "^3.0.2" I managed to close the popup with:

popupRef.current._closeButton.click()

Not very nice comparing to a future Popup.close() method which MUST work out-of-box, but gets the job done...

Majestic answered 26/11, 2020 at 10:6 Comment(1)
Do you have any way to do it with RL4 and Typescript?Janellajanelle
C
2

I ended up with a similar solution to Luca's Answer, so I thought I'd add it as an answer too. I needed to close all popups when moving or zooming the map and ended up with the following:

import React, { useRef } from "react";
import { Map } from "react-leaflet"

export default () => {
  const mapRef = useRef(null);

  const closePopups = () => {
    mapRef.current.leafletElement.closePopup();
  };

  const handleOnDragend = e => {
    closePopups();
  };

  const handleOnZoomend = e => {
    closePopups();
  };

  if (typeof window === 'undefined') {
    return null;
  }

  return (
      <Map
        ref={mapRef}
        onDragend={handleOnDragend}
        onZoomend={handleOnZoomend}
      >
      </Map>
  )
}

This can, however, be extended so that anything can call the closePopups method.

Colossus answered 3/6, 2019 at 10:8 Comment(0)
H
0

I found the working solution for react-leaflet v3 by modifying these two links codesandbox https://codesandbox.io/s/4ws0i and https://mcmap.net/q/1774849/-react-leaflet-close-popup-on-button-click

here is the function to hide the Popup component

const hideElement = () => {
  if (!popupElRef.current || !map) return;
  map.closePopup();
};

here is the Popup component

<Popup ref={popupElRef} closeButton={false}>
  <button onClick={hideElement}>Close popup</button>
</Popup>
Hyperphysical answered 11/4, 2022 at 5:2 Comment(0)
C
0

In the latest react-leaflet Map simply has a closePopup() method. Here is a working example with MapContainer.

const [map, setMap] = useState<Map|null>(null);
return (
  <MapContainer ref={setMap} center={[44,44]} zoom={7}>
    <TileLayer
      attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    />
    <Marker position={[44,44]}>
      <Popup>
        <button
          onClick={() => {
            map && map.closePopup();
          }}
        >
          Close me!
        </button>
      </Popup>
    </Marker>
  </MapContainer>
);
Colfin answered 15/5, 2023 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.