Skip to content

Commit 2d03bdb

Browse files
fix(Select): select the pressed item when the list scrolls before release (#2232)
Co-authored-by: Hunter Johnston <[email protected]>
1 parent 08b1549 commit 2d03bdb

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

‎.changeset/pressed-items-stay.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bits-ui": patch
3+
---
4+
5+
fix(Select, Combobox): select the pressed item when the list scrolls under a still pointer before release

‎packages/bits-ui/src/lib/bits/select/select.svelte.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ abstract class SelectBaseRootState {
124124
});
125125
contentIsPositioned = $state(false);
126126
isUsingKeyboard = false;
127+
/**
128+
* The item a press started on, and where. The list can scroll under a still
129+
* pointer before the release, which then lands on a neighbouring item.
130+
*/
131+
pressedItem: { item: SelectItemState; x: number; y: number } | null = null;
127132
isCombobox = false;
128133
domContext = new DOMContext(() => null);
129134

@@ -142,6 +147,7 @@ abstract class SelectBaseRootState {
142147
$effect.pre(() => {
143148
if (!this.opts.open.current) {
144149
this.setHighlightedNode(null);
150+
this.pressedItem = null;
145151
}
146152
});
147153
}
@@ -1152,6 +1158,11 @@ export class SelectContentState {
11521158
this.root.isUsingKeyboard = false;
11531159
}
11541160

1161+
// every press starts fresh, so a press that ended off an item isn't reused
1162+
onpointerdowncapture = () => {
1163+
this.root.pressedItem = null;
1164+
};
1165+
11551166
readonly #styles = $derived.by(() => {
11561167
return getFloatingContentCSSVars(this.root.isCombobox ? "combobox" : "select");
11571168
});
@@ -1204,6 +1215,7 @@ export class SelectContentState {
12041215
...this.#styles,
12051216
},
12061217
onpointermove: this.onpointermove,
1218+
onpointerdowncapture: this.onpointerdowncapture,
12071219
...this.attachment,
12081220
}) as const
12091221
);
@@ -1334,6 +1346,7 @@ export class SelectItemState {
13341346
onpointerdown(e: BitsPointerEvent) {
13351347
// prevent focus from leaving the input/select trigger
13361348
e.preventDefault();
1349+
this.root.pressedItem = { item: this, x: e.clientX, y: e.clientY };
13371350
}
13381351

13391352
/**
@@ -1343,6 +1356,18 @@ export class SelectItemState {
13431356
*/
13441357
onpointerup(e: BitsPointerEvent) {
13451358
if (e.defaultPrevented || !this.opts.ref.current) return;
1359+
const press = this.root.pressedItem;
1360+
this.root.pressedItem = null;
1361+
// released on another item without the pointer moving: the list moved under it
1362+
if (
1363+
press &&
1364+
press.item !== this &&
1365+
Math.hypot(e.clientX - press.x, e.clientY - press.y) < 4
1366+
) {
1367+
e.preventDefault();
1368+
press.item.handleSelect();
1369+
return;
1370+
}
13461371
/**
13471372
* For one reason or another, when it's a touch pointer and _not_ on IOS,
13481373
* we need to listen for the immediate click event to handle the selection,

‎tests/src/tests/select/select.browser.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,81 @@ describe("select - single", () => {
860860
expect(itemRect.bottom).toBeLessThanOrEqual(downButton.getBoundingClientRect().top + 1);
861861
});
862862

863+
describe("mouse press on an item", () => {
864+
async function pressItem(testId: string) {
865+
render(SelectScrollButtonsTest);
866+
await page.getByTestId("trigger").click();
867+
await expectExists(page.getByTestId("content"));
868+
await sleep(100);
869+
const rect = page.getByTestId(testId).element().getBoundingClientRect();
870+
const point = {
871+
clientX: rect.left + rect.width / 2,
872+
clientY: rect.top + rect.height / 2,
873+
};
874+
page.getByTestId(testId).element().dispatchEvent(mousePointer("pointerdown", point));
875+
return point;
876+
}
877+
878+
function mousePointer(type: string, point: { clientX: number; clientY: number }) {
879+
return new PointerEvent(type, {
880+
...point,
881+
bubbles: true,
882+
cancelable: true,
883+
pointerType: "mouse",
884+
isPrimary: true,
885+
button: 0,
886+
});
887+
}
888+
889+
function itemAt({ clientX, clientY }: { clientX: number; clientY: number }) {
890+
return document
891+
.elementFromPoint(clientX, clientY)!
892+
.closest<HTMLElement>("[role=option]")!;
893+
}
894+
895+
it("should select the pressed item when the list scrolls under a still pointer", async () => {
896+
const point = await pressItem("item-3");
897+
const viewport = page.getByTestId("viewport").element() as HTMLElement;
898+
viewport.dispatchEvent(new WheelEvent("wheel", { deltaY: 60, bubbles: true }));
899+
viewport.scrollTop = 60;
900+
await expectExists(page.getByTestId("scroll-up-button"));
901+
await sleep(100);
902+
903+
const released = itemAt(point);
904+
expect(released.dataset.testid).not.toBe("item-3");
905+
released.dispatchEvent(mousePointer("pointerup", point));
906+
await expect.element(page.getByTestId("trigger")).toHaveTextContent(/^3$/);
907+
});
908+
909+
it("should select the item the pointer was moved to before release", async () => {
910+
const from = await pressItem("item-1");
911+
const rect = page.getByTestId("item-2").element().getBoundingClientRect();
912+
const to = { clientX: from.clientX, clientY: rect.top + rect.height / 2 };
913+
itemAt(to).dispatchEvent(mousePointer("pointerup", to));
914+
await expect.element(page.getByTestId("trigger")).toHaveTextContent(/^2$/);
915+
});
916+
917+
it("should not reuse a press that was released off an item", async () => {
918+
// press item-3, release off any item: that press is over
919+
const point = await pressItem("item-3");
920+
const content = page.getByTestId("content").element() as HTMLElement;
921+
content.dispatchEvent(mousePointer("pointerup", point));
922+
const viewport = page.getByTestId("viewport").element() as HTMLElement;
923+
viewport.dispatchEvent(new WheelEvent("wheel", { deltaY: 60, bubbles: true }));
924+
viewport.scrollTop = 60;
925+
await expectExists(page.getByTestId("scroll-up-button"));
926+
await sleep(100);
927+
// a new press that starts off any item and is released on one at the same point
928+
content.dispatchEvent(mousePointer("pointerdown", point));
929+
const released = itemAt(point);
930+
expect(released.dataset.testid).not.toBe("item-3");
931+
released.dispatchEvent(mousePointer("pointerup", point));
932+
await expect
933+
.element(page.getByTestId("trigger"))
934+
.toHaveTextContent(released.dataset.testid!.replace("item-", ""));
935+
});
936+
});
937+
863938
it("should highlight the selected item on reopen when its value is an empty string", async () => {
864939
const items: Item[] = [
865940
{ value: "1", label: "A" },

0 commit comments

Comments
 (0)