Skip to content
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

Fix Laggy Map Dragging on Mobile with Dark Mode Enabled #42

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions components/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import 'leaflet/dist/leaflet.css'
import type { PropsWithChildren } from 'react'
import { MapContainer, type MapContainerProps, TileLayer } from 'react-leaflet'
import { MapContainer, type MapContainerProps } from 'react-leaflet'
import TileLayer from './tile-layer'

export type MapProps = MapContainerProps & PropsWithChildren

Expand All @@ -15,16 +16,13 @@ export default function Map({
}: MapProps) {
return (
<MapContainer
{...mapProps}
maxZoom={16} // Maximum zoom level provided by Protomaps
center={center}
zoom={zoom}
className={className}
{...mapProps}
>
<TileLayer
attribution='<a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
className="dark:hue-rotate-180 dark:invert dark:contrast-[.90] dark:brightness-90"
/>
<TileLayer />
{children}
</MapContainer>
)
Expand Down
36 changes: 36 additions & 0 deletions components/tile-layer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import '@maplibre/maplibre-gl-leaflet'
import L from 'leaflet'
import { useTheme } from 'next-themes'
import { useEffect, useRef } from 'react'
import { useMap } from 'react-leaflet'

export default function TileLayer() {
const map = useMap()
const { resolvedTheme } = useTheme()
const glRef = useRef<L.MaplibreGL>(
L.maplibreGL({
style: `/map-styles/${resolvedTheme}.json`,
}),
)

useEffect(() => {
const maplibreMap = glRef.current.getMaplibreMap()

if (!maplibreMap) {
glRef.current.addTo(map)

map.attributionControl
.addAttribution(
'<a href="https://datawan.id">Datawan</a> | <a href="https://github.com/protomaps/basemaps">Protomaps</a> © <a href="https://openstreetmap.org">OpenStreetMap</a>',
)
.setPrefix('<a href="https://leafletjs.com">Leaflet</a>')

return
}

// Update the style when the theme changes
maplibreMap.setStyle(`/map-styles/${resolvedTheme}.json`)
}, [map, resolvedTheme])

return null
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"node": ">=18.17"
},
"dependencies": {
"@maplibre/maplibre-gl-leaflet": "^0.0.22",
"@next/env": "^15.0.2",
"@radix-ui/react-alert-dialog": "^1.1.2",
"@radix-ui/react-dialog": "^1.1.2",
Expand Down
Loading
Loading