File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import Mapbox from 'mapbox-gl';
66import { useMap } from '../../hooks/useMap' ;
77import { useEvents } from '../../hooks/useEvents' ;
88import { usePropsReactive } from '../../hooks/usePropsReactive' ;
9+ import { applyReactStyle } from '../../utils/applyReactStyle' ;
910import { allProps , setterMap , converterMap } from './config' ;
1011import { 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
Original file line number Diff line number Diff line change 11import type { PropKey } from './types' ;
2+ import { toLngLat } from '../../utils/toLngLat' ;
23
34/** 静态属性 */
45export const StaticProps : PropKey [ ] = [
@@ -15,12 +16,13 @@ export const NativeDynamicProps: PropKey[] = [
1516 'offset' ,
1617 'maxWidth' ,
1718 'altitude' ,
19+ 'lngLat' ,
1820] ;
1921
2022export const allProps = NativeDynamicProps . concat ( StaticProps ) ;
2123
2224export const setterMap = { } ;
2325
2426export const converterMap : Partial < Record < PropKey , ( ...value : any [ ] ) => any > > = {
25-
27+ lngLat : toLngLat ,
2628} ;
Original file line number Diff line number Diff line change 1+ import type React from 'react' ;
2+ import type { KeysOfUnion } from 'type-fest' ;
3+
4+ const unitlessNumber = / b o x | f l e x | g r i d | c o l u m n | l i n e H e i g h t | f o n t W e i g h t | o p a c i t y | o r d e r | t a b S i z e | z I n d e x / ;
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+ }
You can’t perform that action at this time.
0 commit comments