Skip to content

Commit 2cc0e1f

Browse files
fix: resize the map when the sidebar panel is resized (#23)
1 parent afe2510 commit 2cc0e1f

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

components/map-dashboard.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Query, getData } from '@/lib/data'
55
import { Cross2Icon, ExternalLinkIcon, ReloadIcon } from '@radix-ui/react-icons'
66
import dynamic from 'next/dynamic'
77
import Link from 'next/link'
8-
import { useEffect, useLayoutEffect, useState } from 'react'
8+
import { forwardRef, useEffect, useLayoutEffect, useRef, useState } from 'react'
99
import { Button } from './ui/button'
1010
import {
1111
ResizableHandle,
@@ -27,7 +27,7 @@ import {
2727
parentArea,
2828
} from '@/lib/const'
2929
import { Switch } from './ui/switch'
30-
import { LatLngBounds } from 'leaflet'
30+
import { LatLngBounds, Map as LeafletMap } from 'leaflet'
3131
import { useMap } from 'react-leaflet'
3232

3333
const Map = dynamic(() => import('@/components/map'), {
@@ -69,6 +69,19 @@ function MapFlyToBounds({ bounds }: { bounds: LatLngBounds }) {
6969
return null
7070
}
7171

72+
const MapRefSetter = forwardRef<LeafletMap | null>((props, ref) => {
73+
const map = useMap()
74+
75+
useEffect(() => {
76+
if (map && ref && 'current' in ref) {
77+
ref.current = map // Set the ref to the map instance
78+
}
79+
}, [map, ref])
80+
81+
return null
82+
})
83+
MapRefSetter.displayName = 'MapRefSetter'
84+
7285
type Props = {
7386
defaultSelected?: Selected
7487
}
@@ -90,6 +103,7 @@ export default function MapDashboard({ defaultSelected }: Props) {
90103
const [panelDirection, setPanelDirection] = useState<
91104
'horizontal' | 'vertical'
92105
>('horizontal')
106+
const mapRef = useRef<LeafletMap | null>(null)
93107

94108
useEffect(() => {
95109
if (defaultSelected) {
@@ -161,6 +175,12 @@ export default function MapDashboard({ defaultSelected }: Props) {
161175
return () => window.removeEventListener('resize', handleResize)
162176
}, [])
163177

178+
const handleResizeMap = () => {
179+
if (mapRef.current) {
180+
mapRef.current.invalidateSize({ animate: true })
181+
}
182+
}
183+
164184
return (
165185
<ResizablePanelGroup
166186
direction={panelDirection}
@@ -342,10 +362,15 @@ export default function MapDashboard({ defaultSelected }: Props) {
342362

343363
<ResizableHandle withHandle />
344364

345-
<ResizablePanel defaultSize={75}>
365+
<ResizablePanel
366+
defaultSize={75}
367+
onResize={debounce(handleResizeMap, 100)}
368+
>
346369
<Map className="h-full z-0">
347370
{areaBounds && <MapFlyToBounds bounds={areaBounds} />}
348371

372+
<MapRefSetter ref={mapRef} />
373+
349374
{Object.entries(featureConfig).map(([area, config]) => {
350375
const selectedArea =
351376
selected?.[singletonArea[area as FeatureAreas] as keyof Selected]

0 commit comments

Comments
 (0)