|
1 | 1 | import { getUISettings } from "@renderer/atoms/settings/ui"
|
2 | 2 | import { jotaiStore } from "@renderer/lib/jotai"
|
3 |
| -import { useCallback, useContext, useId, useRef } from "react" |
| 3 | +import { useCallback, useContext, useId, useRef, useState } from "react" |
| 4 | +import { flushSync } from "react-dom" |
| 5 | +import { useEventCallback } from "usehooks-ts" |
4 | 6 |
|
5 | 7 | import { modalStackAtom } from "./atom"
|
6 | 8 | import { CurrentModalContext } from "./context"
|
@@ -73,3 +75,42 @@ const actions = {
|
73 | 75 | }
|
74 | 76 |
|
75 | 77 | export const useCurrentModal = () => useContext(CurrentModalContext)
|
| 78 | + |
| 79 | +export const useResizeableModal = ( |
| 80 | + modalElementRef: React.RefObject<HTMLDivElement>, |
| 81 | + { |
| 82 | + enableResizeable, |
| 83 | + }: { |
| 84 | + enableResizeable: boolean |
| 85 | + }, |
| 86 | +) => { |
| 87 | + const [resizeableStyle, setResizeableStyle] = useState( |
| 88 | + {} as React.CSSProperties, |
| 89 | + ) |
| 90 | + const [isResizeable, setIsResizeable] = useState(false) |
| 91 | + |
| 92 | + const handlePointDown = useEventCallback(() => { |
| 93 | + if (!enableResizeable) return |
| 94 | + if (isResizeable) return |
| 95 | + const $modalElement = modalElementRef.current |
| 96 | + if (!$modalElement) return |
| 97 | + |
| 98 | + const rect = $modalElement.getBoundingClientRect() |
| 99 | + const { x, y } = rect |
| 100 | + |
| 101 | + flushSync(() => { |
| 102 | + setIsResizeable(true) |
| 103 | + setResizeableStyle({ |
| 104 | + position: "fixed", |
| 105 | + top: `${y}px`, |
| 106 | + left: `${x}px`, |
| 107 | + }) |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + return { |
| 112 | + resizeableStyle, |
| 113 | + isResizeable, |
| 114 | + handlePointDown, |
| 115 | + } |
| 116 | +} |
0 commit comments