Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/sortable card: improve touch handling

Adorian Doran9d865ebd5608
diff --git a/apps/client/src/widgets/react/SortableCard.css b/apps/client/src/widgets/react/SortableCard.css
index bc78796faf..9e666d0111 100644
--- a/apps/client/src/widgets/react/SortableCard.css
+++ b/apps/client/src/widgets/react/SortableCard.css
@@ -25,6 +25,16 @@
transition: transform 200ms ease;
}
+ /* A finger takes hold of a segment by resting anywhere on it, and a rest that long is what
+ raises the callout and the text magnifier on iOS. A mouse is left alone: it carries a segment
+ by the grip, and a caption is still text to read and select. */
+ @media (pointer: coarse) {
+ .tn-sortable-segment {
+ -webkit-touch-callout: none;
+ user-select: none;
+ }
+ }
+
.tn-sortable-segment:focus-visible {
outline: 2px solid var(--sortable-selected-color);
outline-offset: -2px;
diff --git a/apps/client/src/widgets/react/SortableCard.spec.tsx b/apps/client/src/widgets/react/SortableCard.spec.tsx
index f910e79b0a..eee01ca1d1 100644
--- a/apps/client/src/widgets/react/SortableCard.spec.tsx
+++ b/apps/client/src/widgets/react/SortableCard.spec.tsx
@@ -296,6 +296,49 @@ describe("SortableCard", () => {
expect(scrolls()).toBe(true);
});
+ /**
+ * A thumb reaching across a phone lands on the near edge of a row far more easily than on
+ * one particular end of it, so the rest is kept wherever the finger fell.
+ */
+ it("is carried by a finger resting anywhere on the segment", () => {
+ draw();
+
+ touch(segmentOf("a"), { onGrip: false });
+ rest();
+ expect(segmentOf("a")?.className).toContain("tn-sortable-dragging");
+
+ moveTo(SEGMENT_HEIGHT + SEGMENT_GAP + 1, { pointerType: "touch" });
+ drop();
+ expect(changed.at(-1)?.map((item) => item.key)).toEqual([ "b", "a", "c" ]);
+ });
+
+ /** A mouse has no trouble reaching a mark, and a press elsewhere is for reading. */
+ it("is not carried by a mouse pressed anywhere but the grip", () => {
+ draw();
+
+ act(() => {
+ segmentOf("a")?.dispatchEvent(pointerEvent("pointerdown", 0));
+ });
+ moveTo(SEGMENT_HEIGHT + SEGMENT_GAP + 1);
+
+ expect(segmentOf("a")?.className).not.toContain("tn-sortable-dragging");
+ expect(captions()).toEqual([ "Alpha", "Beta", "Gamma" ]);
+ });
+
+ /** Whatever a control does with a press, carrying the row off is not it. */
+ it("leaves a press that began on a control to the control", () => {
+ draw({ renderItem: (item) => <button type="button">{item.caption}</button> });
+
+ const control = segmentOf("a")?.querySelector("button");
+ act(() => { control?.dispatchEvent(pointerEvent("pointerdown", 0, {
+ pointerType: "touch", button: -1
+ })); });
+ rest();
+
+ expect(segmentOf("a")?.className).not.toContain("tn-sortable-dragging");
+ expect(changed).toEqual([]);
+ });
+
it("takes nothing up for a finger that strays before it has rested", () => {
draw();
@@ -701,9 +744,9 @@ describe("SortableCard", () => {
return (segment?.offsetTop ?? 0) + (Number.isNaN(by) ? 0 : by);
}
- /** A finger going down on a grip, which takes nothing up until it has rested there. */
- function touch(segment: HTMLElement | null) {
- grab(segment, 0, { pointerType: "touch", button: -1 });
+ /** A finger going down on a segment, which takes nothing up until it has rested there. */
+ function touch(segment: HTMLElement | null, { onGrip = true } = {}) {
+ grab(segment, 0, { pointerType: "touch", button: -1 }, onGrip);
}
/** Whether a finger moving now would still scroll the page rather than carry a segment. */
@@ -719,13 +762,13 @@ describe("SortableCard", () => {
lay();
}
- function grab(segment: HTMLElement | null, at = 0, options: Record<string, unknown> = {}) {
+ function grab(
+ segment: HTMLElement | null, at = 0, options: Record<string, unknown> = {}, onGrip = true
+ ) {
grabbedAt = at;
grabbedFrom = segment?.offsetTop ?? 0;
- act(() => {
- segment?.querySelector(".tn-sortable-grip")
- ?.dispatchEvent(pointerEvent("pointerdown", at, options));
- });
+ const pressed = onGrip ? segment?.querySelector(".tn-sortable-grip") : segment;
+ act(() => { pressed?.dispatchEvent(pointerEvent("pointerdown", at, options)); });
lay();
}
diff --git a/apps/client/src/widgets/react/SortableCard.tsx b/apps/client/src/widgets/react/SortableCard.tsx
index 529ba8e94f..f2f2be51c6 100644
--- a/apps/client/src/widgets/react/SortableCard.tsx
+++ b/apps/client/src/widgets/react/SortableCard.tsx
@@ -25,6 +25,9 @@ const TOUCH_HOLD_MS = 400;
/** How far it may stray in that time and still be resting rather than scrolling. */
const TOUCH_SLACK_PX = 8;
+/** What a press belongs to rather than to the segment holding it. */
+const CONTROLS = "button, a, input, select, textarea, label";
+
/** One entry of a {@link SortableCard}, named by a key that outlives the order it stands in. */
export interface SortableItem {
key: string;
@@ -210,6 +213,8 @@ export function SortableCard<T extends SortableItem>({
return;
}
+ listRef.current?.setPointerCapture?.(pointerId);
+
dragRef.current = {
key,
pointerId,
@@ -231,23 +236,34 @@ export function SortableCard<T extends SortableItem>({
}, []);
const beginDrag = useCallback((event: PointerEvent, key: string) => {
- const list = listRef.current;
- if (!list || !segmentOf(key) || (event.pointerType === "mouse" && event.button !== 0)) {
+ if (!listRef.current || !segmentOf(key)) {
return;
}
- // The gesture is the grip's alone: it must not also scroll the page under a finger, nor
- // start a selection under a mouse.
- event.preventDefault();
- list.setPointerCapture?.(event.pointerId);
+ const target = event.target as HTMLElement | null;
+ const onGrip = !!target?.closest(".tn-sortable-grip");
- // A mouse on a grip means only one thing, so it is taken up at once. A finger has to rest
- // first, since it is also how the page is scrolled.
+ // A mouse takes hold of the grip and nothing else: a press anywhere on a segment is for
+ // reading what it says or working a control the caller drew, and a mouse has no trouble
+ // reaching a mark. A finger rests wherever it lands, a thumb across a phone reaching the
+ // near edge of a row far more easily than one particular end of it.
if (event.pointerType === "mouse") {
+ if (!onGrip || event.button !== 0) {
+ return;
+ }
+
+ // Stops the press from starting a selection instead.
+ event.preventDefault();
startDrag(key, event.pointerId, event.clientY);
return;
}
+ // A press that began on a control is the control's, whatever it does with it.
+ if (!onGrip && target?.closest(CONTROLS)) {
+ return;
+ }
+
+
cancelHold();
holdRef.current = {
pointerId: event.pointerId,
@@ -365,13 +381,17 @@ export function SortableCard<T extends SortableItem>({
focusEntry(to);
}, [ focusEntry, shown ]);
- /** What a segment is carried by, drawn on whichever edge the caller asked for. */
- const grip = (key: string) => (
+ /**
+ * What a segment is carried by, drawn on whichever edge the caller asked for.
+ *
+ * A mark rather than the gesture's own: the segment answers for the press, so a finger takes
+ * hold anywhere on it and only a mouse has to find the grip.
+ */
+ const grip = () => (
<span
className="tn-sortable-grip"
title={t("sortable_card.reorder")}
aria-hidden="true"
- onPointerDown={(event) => beginDrag(event, key)}
>
<svg viewBox="0 0 16 16">
<line x1="3" y1="6" x2="13" y2="6" />
@@ -434,10 +454,11 @@ export function SortableCard<T extends SortableItem>({
"tn-sortable-appearing": item.key === addedKey
})}
onFocus={() => select(item.key)}
+ onPointerDown={(event) => beginDrag(event, item.key)}
onKeyDown={(event) => onSegmentKeyDown(event, index)}
onAnimationEnd={() => setAddedKey(undefined)}
>
- {gripPlacement === "start" && grip(item.key)}
+ {gripPlacement === "start" && grip()}
<span className="tn-sortable-content">
{renderItem
@@ -448,7 +469,7 @@ export function SortableCard<T extends SortableItem>({
</>}
</span>
- {gripPlacement === "end" && grip(item.key)}
+ {gripPlacement === "end" && grip()}
</section>
))}