Skip to content

Commit 4e969f3

Browse files
fix(web): keep sidebar drag dividers clear and gestures smooth (#10453)
1 parent ecf3716 commit 4e969f3

6 files changed

Lines changed: 704 additions & 142 deletions

File tree

‎apps/web/src/components/Sidebar.drag.test.ts‎

Lines changed: 174 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
22
import { closestCenter, type CollisionDetection } from "@dnd-kit/core";
33
import { verticalListSortingStrategy, type SortingStrategy } from "@dnd-kit/sortable";
4-
import { createSidebarCollisionDetection, createSidebarSortingStrategy } from "./Sidebar.drag";
54
import {
5+
createSidebarCollisionDetection,
6+
createSidebarSortingStrategy,
7+
restrictBelowSidebarLabel,
8+
} from "./Sidebar.drag";
9+
import {
10+
resolveSidebarDropTarget,
611
sidebarListItemId,
712
sidebarMarkerId,
813
type SidebarListItem,
@@ -35,7 +40,7 @@ function layout(
3540
? (item.section === "pinned" || item.section === "active" ? cardHeight : 36) * scale
3641
: item.marker === "pinned-header" || item.marker === "pinned-divider"
3742
? 0
38-
: (item.marker.endsWith("placeholder") ? 36 : 32) * scale;
43+
: (item.marker.endsWith("placeholder") ? 0 : 32) * scale;
3944
const rect = { top, height, bottom: top + height, left: 0, right: 260, width: 260 };
4045
top += height + 1;
4146
return rect;
@@ -119,69 +124,92 @@ describe("sidebar collision detection", () => {
119124
expect(detector(collisionArgs())[0]?.id).toBe("blocked");
120125
});
121126

122-
function clampedArgs() {
123-
const args = collisionArgs();
124-
const pinned = args.droppableRects.get(sidebarMarkerId("pinned-header"))!;
125-
const source = args.droppableRects.get("source")!;
126-
const collisionRect = {
127-
...source,
128-
top: pinned.top - 8,
129-
bottom: pinned.top - 8 + source.height,
130-
};
131-
return {
132-
...args,
133-
active: {
134-
...args.active,
135-
rect: { current: { initial: source, translated: collisionRect } },
136-
},
137-
collisionRect,
138-
pointerCoordinates: { x: pinned.left + pinned.width / 2, y: pinned.top + 8 },
139-
};
140-
}
141-
142-
it("reaches empty Pins with an upward pointer while the card is clamped at the top", () => {
143-
const args = clampedArgs();
144-
const detector = createSidebarCollisionDetection(() => true, {
145-
emptyPins: true,
146-
activationY: args.pointerCoordinates.y + 6,
147-
});
148-
expect(args.droppableRects.get(sidebarMarkerId("pinned-header"))?.height).toBe(0);
149-
expect(closestCenter(args)[0]?.id).toBe("source");
150-
expect(detector(args)[0]?.id).toBe(sidebarMarkerId("pinned-header"));
151-
});
152-
153127
it.each([
154-
{ reason: "below the boundary cue", x: 130, y: 109, activationY: 140, emptyPins: true },
155-
{ reason: "left of the list", x: -1, y: 108, activationY: 140, emptyPins: true },
156-
{ reason: "right of the list", x: 261, y: 108, activationY: 140, emptyPins: true },
157-
{ reason: "less than 6px upward", x: 130, y: 108, activationY: 113, emptyPins: true },
158-
{ reason: "without an activation point", x: 130, y: 108, activationY: null, emptyPins: true },
159-
{ reason: "with populated Pins", x: 130, y: 108, activationY: 140, emptyPins: false },
160-
])("keeps ordinary collision behavior $reason", ({ x, y, activationY, emptyPins }) => {
161-
const detector = createSidebarCollisionDetection(() => true, { emptyPins, activationY });
162-
const args = { ...clampedArgs(), pointerCoordinates: { x, y } };
163-
expect(detector(args)[0]?.id).toBe("source");
164-
});
165-
166-
it("keeps ordinary collision behavior without pointer coordinates", () => {
167-
const detector = createSidebarCollisionDetection(() => true, {
168-
emptyPins: true,
169-
activationY: 140,
170-
});
171-
expect(detector({ ...clampedArgs(), pointerCoordinates: null })[0]?.id).toBe("source");
172-
});
173-
174-
it("validates the empty Pins override and caches an unsupported result", () => {
175-
const isValid = vi.fn(() => false);
176-
const detector = createSidebarCollisionDetection(isValid, {
177-
emptyPins: true,
178-
activationY: 140,
179-
});
180-
const args = clampedArgs();
181-
expect(detector(args).map((collision) => collision.id)).toEqual(["source"]);
182-
expect(detector(args).map((collision) => collision.id)).toEqual(["source"]);
183-
expect(isValid.mock.calls).toEqual([[sidebarMarkerId("pinned-header")]]);
184-
});
128+
{ sourceSection: "active", pins: 0 },
129+
{ sourceSection: "active", pins: 1 },
130+
{ sourceSection: "pinned", pins: 1 },
131+
{ sourceSection: "settled", pins: 1 },
132+
] as const)(
133+
"switches on crossing the divider row from $sourceSection with $pins pins",
134+
({ sourceSection, pins }) => {
135+
const items = [
136+
pinnedHeader,
137+
...(pins ? [thread("p", "pinned")] : []),
138+
...(sourceSection === "pinned" ? [thread("source", "pinned")] : []),
139+
divider,
140+
thread("a", "active"),
141+
...(sourceSection === "active" ? [thread("source", "active")] : []),
142+
settledHeader,
143+
...(sourceSection === "settled" ? [thread("source", "settled")] : []),
144+
];
145+
const { rects, activeIndex } = layout(items, "source", "a");
146+
const sourceRect = rects[activeIndex]!;
147+
let boundaryTop = 300;
148+
const boundaryNode = {
149+
querySelector: () => ({
150+
getBoundingClientRect: () => ({
151+
top: boundaryTop,
152+
bottom: boundaryTop + 16,
153+
left: 0,
154+
right: 260,
155+
}),
156+
}),
157+
} as unknown as HTMLElement;
158+
const detector = createSidebarCollisionDetection(() => true, {
159+
items,
160+
activationY: sourceSection === "pinned" ? 200 : 600,
161+
});
162+
const at = (center: number) => {
163+
const collisionRect = {
164+
...sourceRect,
165+
top: center - sourceRect.height / 2,
166+
bottom: center + sourceRect.height / 2,
167+
};
168+
const args = {
169+
...collisionArgs(),
170+
active: {
171+
id: "source",
172+
data: { current: {} },
173+
rect: { current: { initial: sourceRect, translated: collisionRect } },
174+
},
175+
collisionRect,
176+
pointerCoordinates: { x: 130, y: center },
177+
droppableRects: new Map(
178+
items.map((item, index) => [sidebarListItemId(item), rects[index]!]),
179+
),
180+
droppableContainers: items.map((item, index) => ({
181+
id: sidebarListItemId(item),
182+
key: sidebarListItemId(item),
183+
disabled: false,
184+
data: { current: {} },
185+
node: {
186+
current:
187+
item === divider
188+
? boundaryNode
189+
: item === settledHeader
190+
? ({ getBoundingClientRect: () => ({ top: 600 }) } as unknown as HTMLElement)
191+
: null,
192+
},
193+
rect: { current: rects[index]! },
194+
})),
195+
};
196+
const over = detector(args)[0];
197+
return over ? resolveSidebarDropTarget(items, "source", String(over.id))?.section : null;
198+
};
199+
expect(at(330)).toBe("active");
200+
expect(at(317)).toBe("active");
201+
expect(at(316)).toBe("pinned");
202+
// The preview moves the divider; a stationary pointer must not undo the drop target.
203+
boundaryTop = 400;
204+
expect(at(316)).toBe("pinned");
205+
expect(at(399)).toBe("pinned");
206+
expect(at(400)).toBe("active");
207+
boundaryTop = 300;
208+
expect(at(400)).toBe("active");
209+
expect(at(317)).toBe("active");
210+
expect(at(316)).toBe("pinned");
211+
},
212+
);
185213

186214
it("returns no collision if an unsupported target has no source fallback", () => {
187215
const args = collisionArgs();
@@ -289,7 +317,7 @@ describe("sidebar drag projection", () => {
289317
}
290318
});
291319

292-
it("leaves canonically sorted settled peers in place", () => {
320+
it("preserves settled order while opening the zero-height Active target", () => {
293321
const items = [
294322
pinnedHeader,
295323
divider,
@@ -303,7 +331,10 @@ describe("sidebar drag projection", () => {
303331
"second",
304332
"first",
305333
);
306-
expect([...result.values()]).toEqual(items.map(() => stationary));
334+
expect(result.get(sidebarMarkerId("active-placeholder"))).toEqual(stationary);
335+
expect(result.get(sidebarMarkerId("settled-header"))).toEqual({ ...stationary, y: 36 });
336+
expect(result.get("first")).toEqual({ ...stationary, y: 36 });
337+
expect(result.get("second")).toEqual(stationary);
307338
});
308339

309340
it.each([
@@ -357,6 +388,36 @@ describe("sidebar drag projection", () => {
357388
expect(result.get("s")?.y).toBe(32);
358389
});
359390

391+
it.each(["s1", "missing-target"])(
392+
"keeps label clearance when a settled drag is over %s",
393+
(over) => {
394+
const items = [
395+
pinnedHeader,
396+
thread("p", "pinned"),
397+
divider,
398+
thread("a", "active"),
399+
settledHeader,
400+
thread("s1", "settled"),
401+
thread("s2", "settled"),
402+
];
403+
const result = preview(
404+
{
405+
items,
406+
settledOrder: ["s1", "s2"],
407+
settledExpanded: true,
408+
boundaryLabelHeight: 24,
409+
},
410+
"s2",
411+
over,
412+
);
413+
expect(result.get("p")?.y).toBe(24);
414+
expect(result.get(sidebarMarkerId("pinned-divider"))?.y).toBe(24);
415+
expect(result.get("a")?.y).toBe(48);
416+
expect(result.get("s1")?.y).toBe(48);
417+
expect(result.get("s2")).toEqual(stationary);
418+
},
419+
);
420+
360421
it("stacks the labels with their gaps when the pinned section is empty", () => {
361422
const items = [
362423
pinnedHeader,
@@ -409,8 +470,8 @@ describe("sidebar drag projection", () => {
409470
});
410471

411472
it.each([
412-
["p", -83, -37],
413-
["s", 0, 46],
473+
["p", -83, -1],
474+
["s", 0, 82],
414475
] as const)(
415476
"replaces the empty Active target when %s enters",
416477
(active, dividerOffset, settledOffset) => {
@@ -561,7 +622,7 @@ describe("sidebar drag projection", () => {
561622
expect(strategy({ ...smaller, index: 2 })?.y).toBe(-62.5);
562623
});
563624

564-
it("uses measured placeholder sizing when card height differs from its default", () => {
625+
it("uses shelf height for empty target sizing when card height differs from its default", () => {
565626
const items = [
566627
pinnedHeader,
567628
thread("p", "pinned"),
@@ -674,3 +735,47 @@ describe("sidebar drag projection", () => {
674735
expect(result.get(sidebarMarkerId("settled-header"))?.y).toBe(46);
675736
});
676737
});
738+
739+
describe("lifted card clearance", () => {
740+
const rect = (top: number, height: number) => ({
741+
top,
742+
bottom: top + height,
743+
height,
744+
left: 0,
745+
right: 260,
746+
width: 260,
747+
});
748+
const apply = (cardTop: number, cardHeight: number, y: number, listTop = 136, offset = 32) =>
749+
restrictBelowSidebarLabel(
750+
{
751+
transform: { ...stationary, y },
752+
containerNodeRect: rect(listTop, 500),
753+
draggingNodeRect: rect(cardTop, cardHeight),
754+
activatorEvent: null,
755+
active: null,
756+
activeNodeRect: null,
757+
over: null,
758+
overlayNodeRect: null,
759+
scrollableAncestors: [],
760+
scrollableAncestorRects: [],
761+
windowRect: null,
762+
},
763+
offset,
764+
);
765+
766+
it.each([36, 82])("keeps a %ipx row below empty Pins even past the top edge", (height) => {
767+
for (const pointerY of [150, 136, 100, 0]) {
768+
const transform = apply(511, height, pointerY - 529);
769+
expect(511 + transform.y).toBe(168);
770+
}
771+
});
772+
773+
it("preserves pointer movement below the label", () => {
774+
expect(apply(511, 36, -200).y).toBe(-200);
775+
});
776+
777+
it("follows the list when it scrolls and includes content preceding Pins", () => {
778+
expect(511 + apply(511, 36, -500, 96).y).toBe(128);
779+
expect(511 + apply(511, 36, -500, 136, 114).y).toBe(250);
780+
});
781+
});

0 commit comments

Comments
 (0)