Skip to content

Commit

Permalink
Improve and use grabbing cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Jan 10, 2024
1 parent 62fb480 commit 3de2023
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/components/designer/DraggableCanvasContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const DraggableCanvasContainer = ({ node, children }: DraggableCanvasCont
ref={draggable.setNodeRef}
{...draggable.listeners}
{...draggable.attributes}
style={{ cursor: "grab" }}
onClick={(ev) => {
ev.stopPropagation()
dispatch(setSelectedItem(node.nodeId))
Expand Down
1 change: 1 addition & 0 deletions src/components/designer/DraggableCanvasItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const DraggableCanvasItem = ({ node }: DraggableCanvasItemProps) => {
ref={draggable.setNodeRef}
{...draggable.listeners}
{...draggable.attributes}
style={{ cursor: "grab" }}
onClick={(ev) => {
ev.stopPropagation()
dispatch(setSelectedItem(node.nodeId))
Expand Down
11 changes: 10 additions & 1 deletion src/components/designer/TemplateDesigner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { snapCenterToCursor } from "@dnd-kit/modifiers"
import { Flex, Transition } from "@mantine/core"
import invariant from "tiny-invariant"
import { DesignerContextProvider } from "~/contexts/DesignerContext"
import { useGrabbingCursor } from "~/hooks/useGrabbingCursor"
import { useMounted } from "~/hooks/useMounted"
import { refreshMenu, selectPreview, setSelectedItem } from "~/state/designerSlice"
import { useAppDispatch, useAppSelector } from "~/state/store"
Expand All @@ -35,8 +36,16 @@ export const TemplateDesigner = () => {
const dispatch = useAppDispatch()
const template = useAppSelector(selectTemplate)
const preview = useAppSelector(selectPreview)
const [grabbingCursorOn, grabbingCursorOff] = useGrabbingCursor()

const handleDragStart = () => {
grabbingCursorOn()
dispatch(setSelectedItem(null))
}

const handleDragEnd = (event: DragEndEvent) => {
grabbingCursorOff()

if (!event.over) return

const { node: activeNode, origin } = event.active.data.current as DraggableData
Expand Down Expand Up @@ -168,7 +177,7 @@ export const TemplateDesigner = () => {
sensors={sensors}
modifiers={[snapCenterToCursor]}
collisionDetection={customCollisionDetection}
onDragStart={() => dispatch(setSelectedItem(null))}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<Flex h="100%" align="stretch" gap="xs">
Expand Down
2 changes: 1 addition & 1 deletion src/components/designer/menuItems/DraggableMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const DraggableMenuItem = ({ node, children }: DraggableMenuItemProps) =>
})

return (
<div ref={setNodeRef} {...attributes} {...listeners}>
<div ref={setNodeRef} {...attributes} {...listeners} style={{ cursor: "grab" }}>
{children}
</div>
)
Expand Down
9 changes: 5 additions & 4 deletions src/components/designer/optionsEditor/OptionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { restrictToParentElement, restrictToVerticalAxis } from "@dnd-kit/modifi
import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable"
import { Table } from "@mantine/core"
import { Control, FieldArrayWithId, UseFieldArrayMove, UseFieldArrayRemove } from "react-hook-form"
import { useGrabbingCursor } from "~/hooks/useGrabbingCursor"
import { useSiteTranslation } from "~/hooks/useSiteTranslation"
import { Option } from "~/schemas/structure"
import { DraggableOptionRow } from "./DraggableOptionRow"
Expand All @@ -16,16 +17,16 @@ interface OptionsTableProps {

export const OptionsTable = ({ control, fields, move, remove }: OptionsTableProps) => {
const { t } = useSiteTranslation()
const [grabbingCursorOn, grabbingCursorOff] = useGrabbingCursor()

const handleDragStart = () => {
document.body.classList.add("grabbing")
grabbingCursorOn()
}

const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event

document.body.classList.remove("grabbing")
grabbingCursorOff()

const { active, over } = event
if (over && active.id !== over.id) {
const oldIndex = fields.findIndex((item) => item.id === active.id)
const newIndex = fields.findIndex((item) => item.id === over.id)
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useGrabbingCursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This hook works together with the CSS in global.css
*/
const GRABBING_CLASS = "grabbing"

const on = () => {
document.body.classList.add(GRABBING_CLASS)
}

const off = () => {
document.body.classList.remove(GRABBING_CLASS)
}

export const useGrabbingCursor = () => [on, off]

0 comments on commit 3de2023

Please sign in to comment.