|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { Rect } from "../../hooks/useDragToResize"; |
| 3 | +import useDetectMouseDownOutside from "../../hooks/useDetectMouseDownOutside"; |
| 4 | +import useSystemSettings from "../../stores/systemSettingsStore"; |
| 5 | + |
| 6 | +import "./AppPopup.scss"; |
| 7 | + |
| 8 | +interface AppPopupProps<Element extends HTMLElement> { |
| 9 | + appRef: React.RefObject<Element>; |
| 10 | + close: () => void; |
| 11 | +} |
| 12 | + |
| 13 | +function AppPopup<Element extends HTMLElement>({ |
| 14 | + appRef, |
| 15 | + children, |
| 16 | + close, |
| 17 | +}: React.PropsWithChildren<AppPopupProps<Element>>) { |
| 18 | + const thisRef = useRef<HTMLDivElement>(null); |
| 19 | + const settings = useSystemSettings(); |
| 20 | + const [rect, setRect] = useState<Rect>({ |
| 21 | + height: 0, |
| 22 | + left: 0, |
| 23 | + top: 0, |
| 24 | + width: 0, |
| 25 | + }); |
| 26 | + |
| 27 | + useDetectMouseDownOutside({ elementRef: thisRef, onMouseDown: close }); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (appRef.current) { |
| 31 | + const bounds = appRef.current.getBoundingClientRect(); |
| 32 | + setRect({ |
| 33 | + height: bounds.height ?? 0, |
| 34 | + left: bounds.left ?? 0, |
| 35 | + top: bounds.top ?? 0, |
| 36 | + width: bounds.width ?? 0, |
| 37 | + }); |
| 38 | + } |
| 39 | + }, [appRef]); |
| 40 | + |
| 41 | + function handleContextMenu(e: React.MouseEvent) { |
| 42 | + e.stopPropagation(); |
| 43 | + e.preventDefault(); |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div |
| 48 | + className="app-popup" |
| 49 | + ref={thisRef} |
| 50 | + style={{ ...rect }} |
| 51 | + onContextMenu={handleContextMenu} |
| 52 | + onClick={(e) => e.stopPropagation()} |
| 53 | + onDoubleClick={(e) => e.stopPropagation()} |
| 54 | + > |
| 55 | + <div |
| 56 | + className="app-popup__content" |
| 57 | + style={{ backgroundColor: settings.mainColor }} |
| 58 | + > |
| 59 | + {children} |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export default AppPopup; |
0 commit comments