Skip to content

Commit 13ae2f3

Browse files
committed
fix: 修复 Popup 设置 lngLat 不生效
1 parent 686da6c commit 13ae2f3

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/components/Popup/Popup.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Mapbox from 'mapbox-gl';
66
import { useMap } from '../../hooks/useMap';
77
import { useEvents } from '../../hooks/useEvents';
88
import { usePropsReactive } from '../../hooks/usePropsReactive';
9+
import { applyReactStyle } from '../../utils/applyReactStyle';
910
import { allProps, setterMap, converterMap } from './config';
1011
import { MarkerEventMap, MarkerEventList } from './constant';
1112

@@ -41,6 +42,17 @@ export const Popup = forwardRef<MapboxPopup, PopupProps>(
4142
[map]
4243
);
4344

45+
useEffect(
46+
() => {
47+
const popupElement = popup?.getElement();
48+
49+
if (popupElement) {
50+
applyReactStyle(popupElement, props.style);
51+
}
52+
},
53+
[props.style, popup]
54+
)
55+
4456
const createInstance = () => {
4557
const options = getCreateOptions(props);
4658

src/components/Popup/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PropKey } from './types';
2+
import { toLngLat } from '../../utils/toLngLat';
23

34
/** 静态属性 */
45
export const StaticProps: PropKey[] = [
@@ -15,12 +16,13 @@ export const NativeDynamicProps: PropKey[] = [
1516
'offset',
1617
'maxWidth',
1718
'altitude',
19+
'lngLat',
1820
];
1921

2022
export const allProps = NativeDynamicProps.concat(StaticProps);
2123

2224
export const setterMap = {};
2325

2426
export const converterMap: Partial<Record<PropKey, (...value: any[]) => any>> = {
25-
27+
lngLat: toLngLat,
2628
};

src/utils/applyReactStyle.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type React from 'react';
2+
import type { KeysOfUnion } from 'type-fest';
3+
4+
const unitlessNumber = /box|flex|grid|column|lineHeight|fontWeight|opacity|order|tabSize|zIndex/;
5+
6+
// https://github.com/visgl/react-map-gl/blob/master/modules/react-mapbox/src/utils/apply-react-style.ts
7+
export function applyReactStyle(
8+
element: HTMLElement,
9+
styles: React.CSSProperties = {}
10+
) {
11+
if (!element || !styles) {
12+
return;
13+
}
14+
15+
const style = element.style;
16+
17+
for (const key in styles) {
18+
const val = styles[key as KeysOfUnion<React.CSSProperties>];
19+
20+
if (Number.isFinite(val) && !unitlessNumber.test(key)) {
21+
// @ts-expect-error ignore error
22+
style[key] = `${val}px`;
23+
} else {
24+
// @ts-expect-error ignore error
25+
style[key] = val;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)