Skip to content

Commit aad5eba

Browse files
committed
feat: modal resize and draggable to absolute position
Signed-off-by: Innei <i@innei.in>
1 parent 71712f4 commit aad5eba

File tree

5 files changed

+412
-329
lines changed

5 files changed

+412
-329
lines changed

src/renderer/src/components/ui/modal/stacked/hooks.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getUISettings } from "@renderer/atoms/settings/ui"
22
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"
46

57
import { modalStackAtom } from "./atom"
68
import { CurrentModalContext } from "./context"
@@ -73,3 +75,42 @@ const actions = {
7375
}
7476

7577
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

Comments
 (0)