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
Make popup clickable and interactable #11
Comments
Hi, Instead of having to patch react-leaflet, I think another way you could do it is to instantiate a Leaflet Popup and attach it to the Marker in your application instead of using the The best solution may be to render an empty element with a specific ID as the content of the Popup so that Leaflet inserts it in the DOM, and then call |
Thanks Paul, I will now look into your suggestion and let you know. |
-Creating a Mixin which allows to use React content in leaflet popups
Hi Pau, I came up with a Mixin which is capable of what you describe in the earlier comment, you may want to check the pull request. |
Hi, |
ok, I understand |
Hm. I must admit that I have not understood how @scherler achieved this. Is there some example of how to do this? |
+1 for this feature |
+1 |
@barbalex @prabuvenkat @RoryShively you can find the PR #13 I have to admit that I did not looked at this since back then and I guess that would be better implemented as |
Im trying yo add a D3 graph into the pop up. Is this possible ? |
@ayanez17 should work, since the popup is a DOM element:
|
Perhaps this example will help codesandbox import React, { Component } from "react";
import ReactDOM from "react-dom";
import ReactDOMServer from "react-dom/server";
import * as L from "leaflet";
import "./styles.css";
const GEO_POSITION = [51.5, 0];
const CustomReactPopup = () => {
return (
<div style={{ fontSize: "14px" }}>
<p>CustomReactPopup</p>
<button id="popup-button-id">Click</button>
</div>
);
};
class App extends Component {
state = {
center: GEO_POSITION,
zoom: 13
};
clickHandler = () => {
console.count("click");
};
gtt = () => {
const buttonEl = document.getElementById("popup-button-id");
if (buttonEl) {
buttonEl.addEventListener("click", this.clickHandler);
}
};
gtt2 = () => {
const buttonEl = document.getElementById("popup-button-id");
if (buttonEl) {
buttonEl.removeEventListener("click", this.clickHandler);
}
};
componentDidMount() {
var map = L.map("map").setView(GEO_POSITION, 13);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker(GEO_POSITION)
.addEventListener("popupopen", this.gtt)
.addEventListener("popupclose", this.gtt2)
.addTo(map)
.bindPopup(ReactDOMServer.renderToString(<CustomReactPopup />))
.openPopup();
}
render() {
return (
<div>
<div id="map" />
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement); |
This really helped me out! Thanks :D |
Try this method const popupNode = document.createElement('div');
ReactDOM.render(<div onClick={()=>{console.log("test")}}>Test</div>, popupNode)
L.popup()
.setLatLng(latlng)
.setContent(popupNode)
.openOn(map); |
anyone know how we do this in angular? |
With this implementation, if there are multiple markers, one has to close popup via the x button for the event listener to be bounded to the next click. I modified your code to show this below:
Figured it out! |
Hi Paul,
I am trying to use clickable elements in popups, but without success.
For example imagine the following:
Now if you click on the button there is nothing happening (at least there should be a complain about "bus" not defined. On a side note I tried as well with "onclick" and as well with onClick="this.bus()"
Now when I patch the marker like:
And do
Then it is working as expected but I have no popup.
To bring back the popup and not having React.renderToStaticMarkup (which is in the Popup.js) remove all onClicks or logic I use, I came up with the following:
and in my calling code I have:
This finally works, but it really feels like a workaround more then the way to go.
Do you know a better way?
The text was updated successfully, but these errors were encountered: