@@ -4,14 +4,15 @@ import { MouseLocation } from "../core/service/controlService/MouseLocation";
44import { cn } from "../utils/cn" ;
55import once from "lodash/once" ;
66import Box from "./Box" ;
7+ import { CircleX } from "lucide-react" ;
78
89export namespace Popup {
910 /**
10- * 弹出一个弹窗
11+ * 弹出一个弹窗,这个弹窗面板不能是 fixed 布局
1112 * @param children
1213 * @returns
1314 */
14- export function show ( children : React . ReactNode ) : Promise < void > {
15+ export function show ( children : React . ReactNode , closeWhenClickOutside : boolean = true ) : Promise < void > {
1516 return new Promise ( ( resolve ) => {
1617 // 启动一个新的React实例
1718 const container = document . createElement ( "div" ) ;
@@ -21,6 +22,7 @@ export namespace Popup {
2122 < Component
2223 x = { MouseLocation . x }
2324 y = { MouseLocation . y }
25+ closeWhenClickOutside = { closeWhenClickOutside }
2426 onClose = { ( ) => {
2527 resolve ( ) ;
2628 setTimeout (
@@ -52,11 +54,13 @@ export namespace Popup {
5254 y,
5355 onClose,
5456 children,
57+ closeWhenClickOutside,
5558 } : {
5659 x : number ;
5760 y : number ;
5861 onClose : ( ) => void ;
5962 children : React . ReactNode ;
63+ closeWhenClickOutside : boolean ;
6064 } ) {
6165 const [ adjustedX , setAdjustedX ] = React . useState ( 0 ) ;
6266 const [ adjustedY , setAdjustedY ] = React . useState ( 0 ) ;
@@ -69,7 +73,6 @@ export namespace Popup {
6973 if ( ref . current ) {
7074 // 调整弹窗位置,确保不会超出屏幕边界
7175 const { width, height } = ref . current . getBoundingClientRect ( ) ;
72- console . log ( width , height ) ;
7376 setAdjustedX ( x ) ;
7477 setAdjustedY ( y ) ;
7578 if ( x + width > window . innerWidth ) {
@@ -94,8 +97,10 @@ export namespace Popup {
9497 // 监听在外面点击事件
9598 const handleClickOutside = ( event : MouseEvent ) => {
9699 if ( ref . current && ! ref . current . contains ( event . target as Node ) ) {
97- setShow ( false ) ;
98- onClose ( ) ;
100+ if ( closeWhenClickOutside ) {
101+ setShow ( false ) ;
102+ onClose ( ) ;
103+ }
99104 }
100105 } ;
101106 document . addEventListener ( "mousedown" , handleClickOutside ) ;
@@ -112,6 +117,7 @@ export namespace Popup {
112117 className = { cn ( "border-icon-button-border fixed z-[102] opacity-0" , {
113118 "opacity-100" : show ,
114119 "opacity-0 transition-none" : adjusting ,
120+ // absolute: closeWhenClickOutside,
115121 } ) }
116122 style = { {
117123 left : adjustedX ,
@@ -120,6 +126,17 @@ export namespace Popup {
120126 } }
121127 >
122128 { children }
129+ { ! closeWhenClickOutside && (
130+ < span
131+ onClick = { ( ) => {
132+ setShow ( false ) ;
133+ onClose ( ) ;
134+ } }
135+ className = "bg-panel-bg text-panel-text border-panel-details-text absolute -right-4 -top-4 cursor-pointer rounded-full p-1 text-sm hover:scale-105"
136+ >
137+ < CircleX className = "cursor-pointer" />
138+ </ span >
139+ ) }
123140 </ Box >
124141 ) ;
125142 }
0 commit comments