react-leaflet-editable
v1.0.0
Published
Leaflet.Editable for React-Leaflet
Maintainers
Readme
react-leaflet-editable
Leaflet.Editable for React-Leaflet (>=4.0.0)
Demo
Installation
npm install react-leaflet-editableQuick Start
Notes
- Import
leaflet-editablebefore rendering. - Ensure
MapContainerhaseditable={true}. - Must be rendered inside a
MapContainer. - The forwarded ref (
editToolsRef.current) is a proxy to theLeaflet.Editableinstance'seditTools, exposing all its methods and properties.
import "leaflet/dist/leaflet.css";
import "leaflet-editable";
import { MapContainer, TileLayer } from "react-leaflet";
import { LeafletEditable, type LeafletEditableHandleProps } from "react-leaflet-editable";
import { useRef } from "react";
function Demo() {
const editToolsRef = useRef<LeafletEditableHandleProps>(null);
const startPolygon = () => {
editToolsRef.current.startPolygon();
};
return (
<MapContainer center={[35, 105]} zoom={4} style={{ height: "100vh" }} editable={true}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<LeafletEditable ref={editToolsRef} />
<button onClick={startPolygon}>Start Polygon</button>
</MapContainer>
);
}API
Props
Pass event callback props to listen for Leaflet.Editable events (e.g., onDrawingStart → editable:drawing:start).
Methods
editToolsRef.current exposes all Leaflet.Editable editTools methods and properties.
