Skip to content

Commit 0bdfc3c

Browse files
committed
Fix size issues when not using border-box
1 parent 05df994 commit 0bdfc3c

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/eventHandlers/mousedown.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,37 @@ function createGhost(
7979
node: VNode
8080
): VNode {
8181
const rect = item.getBoundingClientRect();
82+
const style = getComputedStyle(item);
83+
const padding = {
84+
top: parseFloat(style.paddingTop) + parseFloat(style.borderTop),
85+
left: parseFloat(style.paddingLeft) + parseFloat(style.borderLeft),
86+
bottom:
87+
parseFloat(style.paddingBottom) + parseFloat(style.borderBottom),
88+
right: parseFloat(style.paddingRight) + parseFloat(style.borderRight)
89+
};
8290
const parentRect = item.parentElement.getBoundingClientRect();
83-
const offsetX = ev.clientX - rect.left + parentRect.left;
84-
const offsetY = ev.clientY - rect.top + parentRect.top;
91+
const offsetX =
92+
ev.clientX - rect.left + parentRect.left + parseFloat(style.marginLeft);
93+
const offsetY =
94+
ev.clientY - rect.top + parentRect.top + parseFloat(style.marginTop);
95+
96+
const sub = style.boxSizing !== 'border-box';
8597

8698
return addDataEntry(
8799
addDataEntry(node, 'dataset', {
88100
offsetX,
89101
offsetY,
90-
item
102+
item,
103+
ghost: true
91104
}),
92105
'style',
93106
{
94107
position: 'absolute',
95108
left: ev.clientX - offsetX + 'px',
96109
top: ev.clientY - offsetY + 'px',
97-
width: rect.width + 'px',
98-
height: rect.height + 'px',
110+
width: rect.width - (sub ? padding.left - padding.right : 0) + 'px',
111+
height:
112+
rect.height - (sub ? padding.top - padding.bottom : 0) + 'px',
99113
'pointer-events': 'none'
100114
}
101115
);

src/eventHandlers/mousemove.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ export function mousemoveHandler(
1212
.map(n => n.data.dataset.item)
1313
.filter(n => !!n)[0];
1414

15-
const siblings = Array.prototype.slice.call(item.parentElement.children);
15+
const siblings: Element[] = Array.prototype.slice.call(
16+
item.parentElement.children
17+
);
1618
const index = siblings.indexOf(item);
17-
const ghost = siblings[siblings.length - 1];
19+
const ghost = siblings.filter(el => (el as any).dataset.ghost)[0];
1820
const itemArea = getArea(ghost);
1921
let swapIndex = index;
2022

0 commit comments

Comments
 (0)