Skip to content

Commit

Permalink
mapLifecycleListener implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stassi committed Nov 15, 2024
1 parent 86e3bc0 commit 3a1e548
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/leaf/map/fullscreen/map/map-lifecycle-listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { DomEvent, DomUtil, type Map } from 'leaflet'

import { setControlTitle } from '../control/set-control-title'
import { type UseLink } from '../state/use-link'

export type MapLifecycleListenerOptions = {
documentFirstReady: boolean
getFullscreenState: () => boolean
linkAssign: UseLink['assign']
map: Map
mapFullscreenClassName: string
title: Record<string, string>
toggleFullscreenState: () => void
}

export function mapLifecycleListener({
documentFirstReady,
getFullscreenState,
linkAssign,
map,
mapFullscreenClassName,
title,
toggleFullscreenState,
}: MapLifecycleListenerOptions) {
return function handleFullscreenMapLifecycleEvent() {
DomEvent[documentFirstReady ? 'on' : 'off'](
// @ts-expect-error -- `Document` type is assignable to first DomEvent.on argument
document,
'fullscreenchange',
function handleFullscreenMapChange() {
;(getFullscreenState() ? DomUtil.removeClass : DomUtil.addClass)(
map.getContainer(),
mapFullscreenClassName,
)

map.invalidateSize()
toggleFullscreenState()
setControlTitle({
fullscreen: getFullscreenState(),
linkAssign,
title,
})
},
)
}
}

0 comments on commit 3a1e548

Please sign in to comment.