Skip to content

Commit

Permalink
Fix issue with sortable handle
Browse files Browse the repository at this point in the history
  • Loading branch information
wilgaboury committed Oct 17, 2024
1 parent 4ca4fa8 commit ee899d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 28 additions & 0 deletions dev/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
defaultMutationEventListeners,
Sortable,
SortableGroupContext,
sortableHandle,
} from "../src";

sortableHandle;

export function randomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
Expand Down Expand Up @@ -87,6 +90,7 @@ export const GridPage: Component = () => {
<div
draggable={false}
style={{
position: "relative",
height: "100px",
"background-color": color,
color: "black",
Expand All @@ -96,6 +100,30 @@ export const GridPage: Component = () => {
}}
>
{item}
<div
use:sortableHandle
style={{
position: "absolute",
height: "15px",
width: "15px",
top: "25px",
left: "25px",
"background-color": "black",
cursor: "grab",
}}
/>
<div
use:sortableHandle
style={{
position: "absolute",
height: "15px",
width: "15px",
top: "50px",
left: "50px",
"background-color": "black",
cursor: "grab",
}}
/>
</div>
);
}}
Expand Down
12 changes: 8 additions & 4 deletions src/Sortable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ export function sortableHandle(el: Element, _accessor: () => any) {
}
}

function calculateRelativePercentPosition(e: MouseEvent): Position {
const target = e.currentTarget as HTMLElement;
function calculateRelativePercentPosition(
e: MouseEvent,
target: HTMLElement,
): Position {
const clientRect = elemClientRect(target);
const relativeMousePos = clientToRelative({ x: e.x, y: e.y }, target);
return {
Expand All @@ -98,8 +100,10 @@ function handleDrag<T>(
let cleanupGlobalCursorGrabbingStyle: (() => void) | undefined;
const getItemRef = () => sortable().itemEntries.get(item)!.state.ref();

const relativeMouseDownPercentPosition =
calculateRelativePercentPosition(initialMouseEvent);
const relativeMouseDownPercentPosition = calculateRelativePercentPosition(
initialMouseEvent,
getItemRef(),
);

const startSortable = sortable;
const startIdx = sortable().itemEntries.get(item)!.idx();
Expand Down

0 comments on commit ee899d7

Please sign in to comment.