diff --git a/packages/designer/src/components/components-popover.tsx b/packages/designer/src/components/components-popover.tsx index ed8f4f1..a66b96c 100644 --- a/packages/designer/src/components/components-popover.tsx +++ b/packages/designer/src/components/components-popover.tsx @@ -125,6 +125,7 @@ export const ComponentsPopover = observer( } footer={tipsTextMap[type]} width="330px" + maskClosable body={ = ({ open, overlay, + maskClosable = false, autoAdjustOverflow = true, left: controlledLeft, top: controlledTop, @@ -59,16 +64,35 @@ export const Popover: React.FC = ({ ); useEffect(() => { + if (typeof controlledTop === 'number') { + setTop(controlledTop); + } if (typeof controlledLeft === 'number') { setLeft(controlledLeft); } - }, [controlledLeft]); + }, [controlledTop, controlledLeft]); - useEffect(() => { - if (typeof controlledTop === 'number') { - setTop(controlledTop); + useLayoutEffect(() => { + const handleDocumentClick = (e: MouseEvent) => { + if ( + maskClosable && + visible && + popoverRef.current && + !popoverRef.current.contains(e.target as Node) + ) { + setVisible(false); + onOpenChange(false); + } + }; + + if (maskClosable && visible) { + document.addEventListener('click', handleDocumentClick, true); } - }, [controlledTop]); + + return () => { + document.removeEventListener('click', handleDocumentClick); + }; + }, [maskClosable, onOpenChange, visible]); const handleClick = useCallback( (e: React.MouseEvent) => {