Skip to content

Commit

Permalink
updated names
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-defi committed Dec 17, 2024
1 parent a95a8b7 commit 91761c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/internal/components/Draggable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Draggable', () => {
<Draggable
gridSize={10}
startingPosition={{ x: 0, y: 0 }}
enableSnapToGrid={true}
snapToGrid={true}
>
<div>Drag me</div>
</Draggable>,
Expand All @@ -80,7 +80,7 @@ describe('Draggable', () => {
<Draggable
gridSize={10}
startingPosition={{ x: 0, y: 0 }}
enableSnapToGrid={false}
snapToGrid={false}
>
<div>Drag me</div>
</Draggable>,
Expand Down
12 changes: 6 additions & 6 deletions src/internal/components/Draggable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ type DraggableProps = {
children: React.ReactNode;
gridSize?: number;
startingPosition?: { x: number; y: number };
enableSnapToGrid?: boolean;
snapToGrid?: boolean;
};

export default function Draggable({
children,
gridSize = 1,
startingPosition = { x: 20, y: 20 },
enableSnapToGrid = false,
snapToGrid = false,
}: DraggableProps) {
const [position, setPosition] = useState(startingPosition);
const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 });
const [isDragging, setIsDragging] = useState(false);

const snapToGrid = useCallback(
const calculateSnapToGrid = useCallback(
(positionValue: number) => {
return Math.round(positionValue / gridSize) * gridSize;
},
Expand Down Expand Up @@ -49,8 +49,8 @@ export default function Draggable({

const handleGlobalEnd = () => {
setPosition((prev) => ({
x: enableSnapToGrid ? snapToGrid(prev.x) : prev.x,
y: enableSnapToGrid ? snapToGrid(prev.y) : prev.y,
x: snapToGrid ? calculateSnapToGrid(prev.x) : prev.x,
y: snapToGrid ? calculateSnapToGrid(prev.y) : prev.y,
}));
setIsDragging(false);
};
Expand All @@ -62,7 +62,7 @@ export default function Draggable({
document.removeEventListener('pointermove', handleGlobalMove);
document.removeEventListener('pointerup', handleGlobalEnd);
};
}, [isDragging, dragOffset, snapToGrid, enableSnapToGrid]);
}, [isDragging, dragOffset, snapToGrid, calculateSnapToGrid]);

return (
<div
Expand Down

0 comments on commit 91761c4

Please sign in to comment.