Popup size does not change with the content in react Leaflet
problem
<Popup> {this.state.fullBtnClicked ? bigContent : smallContent} </Popup>
<Popup maxWidth={this.state.fullBtnClicked? bigMaxSize: smallMaxSize} minWidth={this.state.fullBtnClicked? bigMinSize : smallMinSize} > {this.state.fullBtnClicked ? bigContent : smallContent} </Popup>
reason
style.width = ''; style.whiteSpace = 'nowrap'; var width = container.offsetWidth; width = Math.min(width, this.options.maxWidth); width = Math.max(width, this.options.minWidth); style.width = (width + 1) + 'px'; style.whiteSpace = ''; style.height = ''; var height = container.offsetHeight, maxHeight = this.options.maxHeight, scrolledClass = 'leaflet-popup-scrolled'; if (maxHeight && height > maxHeight) { style.height = maxHeight + 'px'; DomUtil.addClass(container, scrolledClass); } else { DomUtil.removeClass(container, scrolledClass); }
-
[@react-leaflet]SomeWork -
[react-leaflet]createDivOverlayComponent.createOverlayComponent -
[leaflet]new Popup(props,ref) -
[leaflet]popup.initialize(props,ref) -
[leaflet]options = props
-
-
-
-
-
Leaflet is a traditional js package, not a React component. -
React leaflet sets a React empty shell for leaflet. The contents of the shell are actually leaflet and its Dom. -
When props change, react laflet only listens for and updates position, but does not update className, minWidth and other attributes.
resolvent
Method 0: Use my modified react leaf
"dependencies":{ "react-leaflet": "npm:@azimiao/react-leaflet" }
Method 1: Change the react leaf
function usePopupLifecycle( element: LeafletElement<LeafletPopup>, context: LeafletContextInterface, //Zimiao haunts (azimiao. com), modify 1, add width/height { position, minWidth, maxWidth, maxHeight }, setOpen: SetOpenFunc, ) { //The previous code is omitted //Zimiao haunts (azimiao. com) Modify 2, monitor changes const firstUpdate = useRef(true) useLayoutEffect(() => { if (firstUpdate.current) { firstUpdate.current = false return } const { instance } = element if (minWidth != null) { instance.options.minWidth = minWidth } if (maxWidth != null) { instance.options.maxWidth = maxWidth } if (maxHeight != null) { instance.options.maxHeight = maxHeight } instance.update() }, [element, minWidth, maxWidth, maxHeight]) }
Method 2: Bugs are features
Method 3: Change leaflet