Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make resizing more natural #771

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions apps/renderer/src/components/ui/modal/stacked/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DragControls } from "framer-motion"
import type { ResizeCallback, ResizeStartCallback } from "re-resizable"
import { useCallback, useContext, useId, useRef, useState } from "react"
import { flushSync } from "react-dom"
import { useEventCallback } from "usehooks-ts"
Expand Down Expand Up @@ -87,14 +89,17 @@ export const useResizeableModal = (
modalElementRef: React.RefObject<HTMLDivElement>,
{
enableResizeable,
dragControls,
}: {
enableResizeable: boolean
dragControls?: DragControls
},
) => {
const [resizeableStyle, setResizeableStyle] = useState({} as React.CSSProperties)
const [isResizeable, setIsResizeable] = useState(false)
const [preferDragDir, setPreferDragDir] = useState<"x" | "y" | null>(null)

const handlePointDown = useEventCallback(() => {
const relocateModal = useEventCallback(() => {
if (!enableResizeable) return
if (isResizeable) return
const $modalElement = modalElementRef.current
Expand All @@ -112,10 +117,33 @@ export const useResizeableModal = (
})
})
})
const handleResizeStart = useEventCallback(((e, dir) => {
if (!enableResizeable) return
relocateModal()

const hasTop = /top/i.test(dir)
const hasLeft = /left/i.test(dir)
if (hasTop || hasLeft) {
dragControls?.start(e as any)
if (hasTop && hasLeft) {
setPreferDragDir(null)
} else if (hasTop) {
setPreferDragDir("y")
} else if (hasLeft) {
setPreferDragDir("x")
}
}
}) satisfies ResizeStartCallback)
const handleResizeStop = useEventCallback((() => {
setPreferDragDir(null)
}) satisfies ResizeCallback)

return {
resizeableStyle,
isResizeable,
handlePointDown,
relocateModal,
handleResizeStart,
handleResizeStop,
preferDragDir,
}
}
15 changes: 10 additions & 5 deletions apps/renderer/src/components/ui/modal/stacked/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ export const ModalInternal = memo(

const modalElementRef = useRef<HTMLDivElement>(null)

const dragController = useDragControls()
const {
handlePointDown: handleResizeEnable,
handleResizeStop,
handleResizeStart,
relocateModal,
preferDragDir,
isResizeable,
resizeableStyle,
} = useResizeableModal(modalElementRef, {
enableResizeable: resizeable,
dragControls: dragController,
})
const animateController = useAnimationControls()
useEffect(() => {
Expand All @@ -137,7 +142,6 @@ export const ModalInternal = memo(
})
}, [animateController])

const dragController = useDragControls()
const handleDrag: PointerEventHandler<HTMLDivElement> = useCallback(
(e) => {
if (draggable) {
Expand Down Expand Up @@ -347,7 +351,7 @@ export const ModalInternal = memo(
onClick={stopPropagation}
onSelect={handleSelectStart}
onKeyUp={handleDetectSelectEnd}
drag
drag={draggable && (preferDragDir || draggable)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
drag={draggable && (preferDragDir || draggable)}
drag={draggable && preferDragDir}

dragControls={dragController}
dragElastic={0}
dragListener={false}
Expand All @@ -360,14 +364,15 @@ export const ModalInternal = memo(
>
<ResizeSwitch
enable={resizableOnly("bottomRight")}
onResizeStart={handleResizeEnable}
onResizeStart={handleResizeStart}
onResizeStop={handleResizeStop}
defaultSize={resizeDefaultSize}
className="flex grow flex-col"
>
<div
className={"relative flex items-center"}
onPointerDownCapture={handleDrag}
onPointerDown={handleResizeEnable}
onPointerDown={relocateModal}
>
<Dialog.Title className="flex w-0 max-w-full grow items-center gap-2 px-4 py-1 text-lg font-semibold">
{icon && <span className="size-4">{icon}</span>}
Expand Down
22 changes: 15 additions & 7 deletions apps/renderer/src/modules/settings/modal/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ export function SettingModalLayout(
const tab = useSettingTab()
const elementRef = useRef<HTMLDivElement>(null)
const edgeElementRef = useRef<HTMLDivElement>(null)
const { handlePointDown, isResizeable, resizeableStyle } = useResizeableModal(elementRef, {
const dragController = useDragControls()
const {
handleResizeStart,
handleResizeStop,
preferDragDir,
relocateModal,
isResizeable,
resizeableStyle,
} = useResizeableModal(elementRef, {
enableResizeable: true,
dragControls: dragController,
})

useEffect(() => {
Expand All @@ -47,15 +56,14 @@ export function SettingModalLayout(
draggable: state.modalDraggable,
overlay: state.modalOverlay,
}))
const dragController = useDragControls()
const handleDrag: PointerEventHandler<HTMLDivElement> = useCallback(
(e) => {
if (draggable) {
dragController.start(e)
handlePointDown()
relocateModal()
}
},
[dragController, draggable, handlePointDown],
[dragController, draggable, relocateModal],
)
const measureDragConstraints = useCallback((constraints: BoundingBox) => {
if (getOS() === "Windows") {
Expand All @@ -80,7 +88,7 @@ export function SettingModalLayout(
)}
style={resizeableStyle}
onContextMenu={preventDefault}
drag={draggable}
drag={draggable && (preferDragDir || draggable)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
drag={draggable && (preferDragDir || draggable)}
drag={draggable && preferDragDir}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's in case of draggable is "x" or "y".

preferDragDir is only available on resizing, when resizing is over, preferDragDir will be back to null.

So draggable && (preferDragDir || draggable) here means when the box is draggable, the direction is preferDragDir or value of draggable

dragControls={dragController}
dragListener={false}
dragMomentum={false}
Expand All @@ -93,9 +101,9 @@ export function SettingModalLayout(
>
<SettingContext.Provider value={defaultCtx}>
<Resizable
onResizeStart={handlePointDown}
onResizeStart={handleResizeStart}
onResizeStop={handleResizeStop}
enable={resizableOnly("bottomRight")}
style={{ ...resizeableStyle, position: "static" }}
defaultSize={{
width: 800,
height: 700,
Expand Down
Loading