Skip to content

Commit

Permalink
Addes draggable component
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneypattison committed Feb 10, 2024
1 parent b3e3b72 commit 49a33ff
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/_components/draggable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Matrix } from "ml-matrix";
import {
Dispatch,
MouseEvent,
ReactNode,
SetStateAction,
useState,
} from "react";

import { transformPoint, translate } from "@/_lib/get-perspective-transform";
import Point from "@/_lib/interfaces/point";

export default function Draggable({
children,
localTransform,
setLocalTransform,
perspective,
}: {
children: ReactNode;
localTransform: Matrix;
setLocalTransform: Dispatch<SetStateAction<Matrix>>;
perspective: Matrix;
}) {
const [dragStart, setDragStart] = useState<Point | null>(null);
const [transformStart, setTransformStart] = useState<Matrix | null>(null);

function handleOnMouseUp(e: MouseEvent<HTMLDivElement>): void {
setDragStart(null);
setTransformStart(null);
}

function handleOnMouseMove(e: MouseEvent<HTMLDivElement>): void {
if (e.buttons & 1 && dragStart === null) {
handleOnMouseDown(e);
return;
}

if ((e.buttons & 1) === 0 && dragStart !== null) {
handleOnMouseUp(e);
return;
}

if (transformStart !== null && dragStart !== null) {
var dest = transformPoint({ x: e.clientX, y: e.clientY }, perspective);
var tx = dest.x - dragStart.x;
var ty = dest.y - dragStart.y;
let m = translate({ x: tx, y: ty });
setLocalTransform(transformStart.mmul(m));
}
}

function handleOnMouseDown(e: MouseEvent<HTMLDivElement>): void {
var pt = transformPoint({ x: e.clientX, y: e.clientY }, perspective);
setDragStart(pt);
setTransformStart(localTransform.clone());
}
return (
<div
// https://github.com/atlassian/react-beautiful-dnd/blob/master/docs/guides/preset-styles.md#phase-dragging-droppable-element
className="cursor-grabbing select-none"
onMouseMove={handleOnMouseMove}
>
{children}
</div>
);
}

0 comments on commit 49a33ff

Please sign in to comment.