Skip to content

Commit e6bb687

Browse files
fix: laggy map dragging on mobile with dark mode enabled (#42)
1 parent 83bcfff commit e6bb687

File tree

6 files changed

+8589
-16
lines changed

6 files changed

+8589
-16
lines changed

components/map.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import 'leaflet/dist/leaflet.css'
44
import type { PropsWithChildren } from 'react'
5-
import { MapContainer, type MapContainerProps, TileLayer } from 'react-leaflet'
5+
import { MapContainer, type MapContainerProps } from 'react-leaflet'
6+
import TileLayer from './tile-layer'
67

78
export type MapProps = MapContainerProps & PropsWithChildren
89

@@ -15,16 +16,13 @@ export default function Map({
1516
}: MapProps) {
1617
return (
1718
<MapContainer
19+
{...mapProps}
20+
maxZoom={16} // Maximum zoom level provided by Protomaps
1821
center={center}
1922
zoom={zoom}
2023
className={className}
21-
{...mapProps}
2224
>
23-
<TileLayer
24-
attribution='<a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
25-
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
26-
className="dark:hue-rotate-180 dark:invert dark:contrast-[.90] dark:brightness-90"
27-
/>
25+
<TileLayer />
2826
{children}
2927
</MapContainer>
3028
)

components/tile-layer.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import '@maplibre/maplibre-gl-leaflet'
2+
import L from 'leaflet'
3+
import { useTheme } from 'next-themes'
4+
import { useEffect, useRef } from 'react'
5+
import { useMap } from 'react-leaflet'
6+
7+
export default function TileLayer() {
8+
const map = useMap()
9+
const { resolvedTheme } = useTheme()
10+
const glRef = useRef<L.MaplibreGL>(
11+
L.maplibreGL({
12+
style: `/map-styles/${resolvedTheme}.json`,
13+
}),
14+
)
15+
16+
useEffect(() => {
17+
const maplibreMap = glRef.current.getMaplibreMap()
18+
19+
if (!maplibreMap) {
20+
glRef.current.addTo(map)
21+
22+
map.attributionControl
23+
.addAttribution(
24+
'<a href="https://datawan.id">Datawan</a> | <a href="https://github.com/protomaps/basemaps">Protomaps</a> © <a href="https://openstreetmap.org">OpenStreetMap</a>',
25+
)
26+
.setPrefix('<a href="https://leafletjs.com">Leaflet</a>')
27+
28+
return
29+
}
30+
31+
// Update the style when the theme changes
32+
maplibreMap.setStyle(`/map-styles/${resolvedTheme}.json`)
33+
}, [map, resolvedTheme])
34+
35+
return null
36+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"node": ">=18.17"
3030
},
3131
"dependencies": {
32+
"@maplibre/maplibre-gl-leaflet": "^0.0.22",
3233
"@next/env": "^15.0.2",
3334
"@radix-ui/react-alert-dialog": "^1.1.2",
3435
"@radix-ui/react-dialog": "^1.1.2",

0 commit comments

Comments
 (0)