Skip to content

Commit 30644e0

Browse files
committed
Update some internal logic
1 parent b0c3b86 commit 30644e0

File tree

14 files changed

+1271
-1094
lines changed

14 files changed

+1271
-1094
lines changed

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"devDependencies": {
1515
"@types/react": "catalog:",
1616
"@types/react-dom": "catalog:",
17-
"@vitejs/plugin-react": "^5.0.0",
17+
"@vitejs/plugin-react": "^5.1.2",
1818
"typescript": "catalog:",
19-
"vite": "^7.1.2"
19+
"vite": "^7.3.0"
2020
}
2121
}

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"private": true,
55
"type": "module",
6-
"packageManager": "pnpm@10.14.0",
6+
"packageManager": "pnpm@10.27.0",
77
"devEngines": {
88
"runtime": {
99
"name": "node",
@@ -18,22 +18,22 @@
1818
"build": "turbo run build:clean && pnpm run -r build:types && turbo run build:js"
1919
},
2020
"devDependencies": {
21-
"@biomejs/biome": "^2.2.0",
21+
"@biomejs/biome": "^2.3.10",
2222
"@skypack/package-check": "^0.2.2",
23-
"@swc/cli": "^0.7.8",
24-
"@swc/core": "^1.13.3",
23+
"@swc/cli": "^0.7.9",
24+
"@swc/core": "^1.15.8",
2525
"@swc/jest": "^0.2.39",
26-
"@testing-library/react": "^16.3.0",
26+
"@testing-library/react": "^16.3.1",
2727
"@types/jest": "^30.0.0",
2828
"@types/leaflet": "catalog:",
2929
"@types/warning": "^3.0.3",
30-
"del-cli": "^6.0.0",
31-
"jest": "^30.0.5",
32-
"jest-environment-jsdom": "^30.0.5",
30+
"del-cli": "^7.0.0",
31+
"jest": "^30.2.0",
32+
"jest-environment-jsdom": "^30.2.0",
3333
"leaflet": "catalog:",
3434
"react": "catalog:",
3535
"react-dom": "catalog:",
36-
"turbo": "^2.5.6",
36+
"turbo": "^2.7.2",
3737
"typescript": "catalog:"
3838
},
3939
"jest": {

packages/core/src/attribution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Map as LeafletMap } from 'leaflet'
2-
import { useEffect, useRef } from 'react'
2+
import { useLayoutEffect, useRef } from 'react'
33

44
export function useAttribution(
55
map: LeafletMap,
66
attribution: string | null | undefined,
77
) {
88
const attributionRef = useRef(attribution)
99

10-
useEffect(
10+
useLayoutEffect(
1111
function updateAttribution() {
1212
if (
1313
attribution !== attributionRef.current &&

packages/core/src/component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
type ReactNode,
55
type Ref,
66
type RefObject,
7-
useEffect,
87
useImperativeHandle,
8+
useLayoutEffect,
99
useState,
1010
} from 'react'
1111
import { createPortal } from 'react-dom'
@@ -46,7 +46,7 @@ export function createDivOverlayComponent<
4646

4747
useImperativeHandle(forwardedRef, () => instance)
4848
// biome-ignore lint/correctness/useExhaustiveDependencies: update overlay when children change
49-
useEffect(
49+
useLayoutEffect(
5050
function updateOverlay() {
5151
if (isOpen) {
5252
instance.update()

packages/core/src/control.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Control, ControlOptions } from 'leaflet'
2-
import { useEffect, useRef } from 'react'
2+
import { useLayoutEffect, useRef } from 'react'
33

44
import { useLeafletContext } from './context.js'
55
import type { ElementHook } from './element.js'
@@ -14,7 +14,7 @@ export function createControlHook<E extends Control, P extends ControlOptions>(
1414
const positionRef = useRef(props.position)
1515
const { position } = props
1616

17-
useEffect(
17+
useLayoutEffect(
1818
function addControl() {
1919
instance.addTo(context.map)
2020

@@ -25,7 +25,7 @@ export function createControlHook<E extends Control, P extends ControlOptions>(
2525
[context.map, instance],
2626
)
2727

28-
useEffect(
28+
useLayoutEffect(
2929
function updateControl() {
3030
if (position != null && position !== positionRef.current) {
3131
instance.setPosition(position)

packages/core/src/element.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type RefObject, useEffect, useRef } from 'react'
1+
import { type RefObject, useLayoutEffect, useRef } from 'react'
22

33
import type { LeafletContextInterface } from './context.js'
44

@@ -53,7 +53,7 @@ export function createElementHook<E, P, C = unknown>(
5353
const propsRef = useRef<P>(props)
5454
const { instance } = elementRef.current
5555

56-
useEffect(
56+
useLayoutEffect(
5757
function updateElementProps() {
5858
if (propsRef.current !== props) {
5959
updateElement(instance, props, propsRef.current)

packages/core/src/layer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { InteractiveLayerOptions, Layer, LayerOptions } from 'leaflet'
2-
import { useEffect } from 'react'
2+
import { useLayoutEffect } from 'react'
33

44
import { useAttribution } from './attribution.js'
55
import { type LeafletContextInterface, useLeafletContext } from './context.js'
@@ -16,7 +16,7 @@ export function useLayerLifecycle(
1616
element: LeafletElement<Layer>,
1717
context: LeafletContextInterface,
1818
) {
19-
useEffect(
19+
useLayoutEffect(
2020
function addLayer() {
2121
const container = context.layerContainer ?? context.map
2222
container.addLayer(element.instance)

packages/react-leaflet/src/MapContainer.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
type ReactNode,
1616
type Ref,
1717
useCallback,
18-
useEffect,
1918
useImperativeHandle,
19+
useLayoutEffect,
2020
useRef,
2121
useState,
2222
} from 'react'
@@ -31,7 +31,6 @@ export interface MapContainerProps extends MapOptions {
3131
id?: string
3232
placeholder?: ReactNode
3333
style?: CSSProperties
34-
whenReady?: () => void
3534
}
3635

3736
function MapContainerComponent<
@@ -46,7 +45,6 @@ function MapContainerComponent<
4645
id,
4746
placeholder,
4847
style,
49-
whenReady,
5048
zoom,
5149
...options
5250
}: Props,
@@ -71,16 +69,24 @@ function MapContainerComponent<
7169
} else if (bounds != null) {
7270
map.fitBounds(bounds, boundsOptions)
7371
}
74-
if (whenReady != null) {
75-
map.whenReady(whenReady)
76-
}
7772
setContext(createLeafletContext(map))
7873
}
7974
}, [])
8075

81-
useEffect(() => {
76+
useLayoutEffect(() => {
8277
return () => {
83-
context?.map.remove()
78+
const map = context?.map
79+
// @ts-expect-error map internals
80+
const containerId = map?._containerId
81+
// @ts-expect-error map internals
82+
const containerLeafletId = map?._container?._leaflet_id
83+
if (
84+
map != null &&
85+
containerId != null &&
86+
containerId === containerLeafletId
87+
) {
88+
map.remove()
89+
}
8490
}
8591
}, [context])
8692

packages/react-leaflet/src/Pane.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
forwardRef,
1010
type ReactNode,
1111
type Ref,
12-
useEffect,
1312
useImperativeHandle,
13+
useLayoutEffect,
1414
useMemo,
1515
useState,
1616
} from 'react'
@@ -85,7 +85,7 @@ function PaneComponent(props: PaneProps, forwardedRef: Ref<PaneRef>) {
8585
const newContext = useMemo(() => ({ ...context, pane: paneName }), [context])
8686

8787
// biome-ignore lint/correctness/useExhaustiveDependencies: lifecycle-only effect
88-
useEffect(() => {
88+
useLayoutEffect(() => {
8989
setPaneElement(createPane(paneName, props, context))
9090

9191
return function removeCreatedPane() {

packages/react-leaflet/src/Popup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type PopupEvent,
1313
type PopupOptions,
1414
} from 'leaflet'
15-
import { type ReactNode, useEffect } from 'react'
15+
import { type ReactNode, useLayoutEffect } from 'react'
1616

1717
export interface PopupProps extends PopupOptions, EventedProps {
1818
children?: ReactNode
@@ -30,7 +30,7 @@ export const Popup = createOverlayComponent<LeafletPopup, PopupProps>(
3030
{ position }: PopupProps,
3131
setOpen: SetOpenFunc,
3232
) {
33-
useEffect(
33+
useLayoutEffect(
3434
function addPopup() {
3535
const { instance } = element
3636

0 commit comments

Comments
 (0)