Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions apps/web/src/lib/selectionActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
});
const element = Object.assign(new EventTarget(), {
ownerDocument: document,
contains: (target: unknown): boolean => target === element,
contains: (target: unknown): boolean => target === element || target === buttonIcon,
closest: () => null,
});
const button = new EventTarget();
const buttonIcon = Object.assign(new EventTarget(), {
closest: (selector: string) => (selector === "button, [role=button]" ? button : null),
});
const onSelection = vi.fn();
const onDismiss = vi.fn();
Expand Down Expand Up @@ -58,7 +63,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
const press = event("pointerdown", { button, isPrimary });
Object.defineProperty(press, "target", { value: target });
document.dispatchEvent(press);
if (target === element && !consumed) element.dispatchEvent(press);
if (element.contains(target) && !consumed) element.dispatchEvent(press);
};
const focus = (target: EventTarget) => {
document.activeElement = target;
Expand Down Expand Up @@ -92,6 +97,7 @@ function createSelectionSurface({ interactiveActions = false } = {}) {
view,
document,
element,
buttonIcon,
actionElement,
field,
onSelection,
Expand Down Expand Up @@ -177,6 +183,31 @@ describe("selection action gestures", () => {
expect(surface.onSelection).not.toHaveBeenCalled();
});

it.each([1, 2])(
"dismisses selection actions when a button is pressed after %i clicks",
(detail) => {
const surface = createSelectionSurface();
surface.down();
surface.up({ detail });
surface.flush();
surface.onSelection.mockClear();
surface.onDismiss.mockClear();

surface.down({ target: surface.buttonIcon });
surface.up();
surface.change();
surface.flush(1000);
expect(surface.onDismiss).toHaveBeenCalledWith("interaction");
expect(surface.onSelection).not.toHaveBeenCalled();

surface.down();
surface.change();
surface.up();
surface.flush();
expect(surface.onSelection).toHaveBeenCalledOnce();
},
);

it("ignores nonprimary pointers and non-left mouseup without ending a left drag", () => {
const surface = createSelectionSurface();
surface.down({ isPrimary: false });
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/lib/selectionActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export function observeSelectionActions({
};
const onSelectionStart = (event: PointerEvent) => {
if (!event.isPrimary) return;
const target = event.target as Element | null;
// Buttons can preserve an existing text selection while opening a menu.
// Their release must not reopen selection actions beside the button.
if (target?.closest("button, [role=button]")) return;
// A surface may consume a press for a link or terminal mouse reporting.
gestureActive = event.button === 0 && !event.defaultPrevented;
dismissed = !gestureActive;
Expand Down
Loading