
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-leaflet-custom-control
Advanced tools
A React wrapper to create a custom control for react-leaflet using ReactDOM's Portal capabilities
The current version of this package supports React Leaflet v3
NOTE
Version ^1.2.3 (which adds this note to the README) has updated peer dependencies for React v18. This may be a breaking change depending on your environment. If you are still running React v17 then install version 1.2.2. |
Version ^1.4.0 now has a dependency on react-leaflet@^4.2.1 . This allows for the useMap() hook. This also requires that this Control component MUST be a child of your MapContainer |
Version ^1.5.0 has updated react dependencies to include ^react@19.0.0 |
#npm
npm install --save react-leaflet-custom-control
#yarn
yarn add react-leaflet-custom-control
import { MapContainer, TileLayer } from 'react-leaflet'
import Control from 'react-leaflet-custom-control'
import { Button } from '@mui/material'
import { Search as SearchIcon } from '@mui/icons-material'
import 'leaflet.css'
<MapContainer center={[35.77, -93.34]} zoom={5}>
<TileLayer
attribution="Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community"
className="basemap"
maxNativeZoom={19}
maxZoom={19}
subdomains={["clarity"]}
url="https://{s}.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
/>
<Control prepend position='topright'>
<Button color='inherit'>
<SearchIcon />
</Button>
</Control>
</MapContainer>
Because this uses React.createPortal
which inherently appends the portal, DOM manipulation is used to append or prepend a container element to the portal target. Because of this, the order of your custom controls matter! The last Control
element to be prepended to a control position will be at the very top while the last Control
element to be appended to a control position will be at the very bottom. If mixing with default React Leaflet
controls, they will be in between your custom controls.
However, because of the way that the portal works and re-renders, multiple control elements will shift order after renders, so it's recommended to have a wrapping element be the child of the Control
to prevent re-ordering each render
import { MapContainer, TileLayer, ZoomControl } from 'react-leaflet'
import Control from 'react-leaflet-custom-control'
import { Button, Stack } from '@mui/material'
import {
Add as AddIcon,
Delete as DeleteIcon,
Search as SearchIcon
} from '@mui/icons-material'
import 'leaflet.css'
<MapContainer center={[35.77, -93.34]} zoom={5} zoomControl={false}>
<TileLayer
attribution="Esri, DigitalGlobe, GeoEye, Earthstar Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community"
className="basemap"
maxNativeZoom={19}
maxZoom={19}
subdomains={["clarity"]}
url="https://{s}.maptiles.arcgis.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
/>
{/* Search control is the very top right control */}
<Control prepend position='topright'>
<Button color='inherit'>
<SearchIcon />
</Button>
</Control>
<ZoomControl position='topright' />
{/* This control will be below the default zoom control. Note the wrapping Stack component */}
<Control position='topright'>
<Stack direction='column' spacing={2} >
<Button color='inherit'>
<AddIcon />
</Button>
<Button color='inherit'>
<DeleteIcon />
</Button>
</Stack>
</Control>
</MapContainer>
Name | Type | Default | Description |
---|---|---|---|
position | ControlOptions | required | The position of the control |
children? | any | undefined | Child element to the control |
React.CSSProperties | |||
container? | React.HTMLAttributes<HTMLDivElement> | undefined | The target root container for the portal |
prepend? | boolean | undefined | Whether the control should be prepended or appended to the position |
Huge thanks to @davetapley for contributing to @1.3.0
and helping to work some of the issues.
Thanks to @samiamlabs for contributing to @1.3.2
for fixing the infinite div
issue.
FAQs
Creates a control wrapper around a React element
The npm package react-leaflet-custom-control receives a total of 6,338 weekly downloads. As such, react-leaflet-custom-control popularity was classified as popular.
We found that react-leaflet-custom-control demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.