Skip to content

Build your personal knowledge base with Trilium Notes

client/ui/sortable card: prevent accidental sorting on touch screens

Adorian Doranb28ce351c08d
diff --git a/apps/client/src/widgets/react/SortableCard.css b/apps/client/src/widgets/react/SortableCard.css
index 5f4e0daa59..1a84135e7d 100644
--- a/apps/client/src/widgets/react/SortableCard.css
+++ b/apps/client/src/widgets/react/SortableCard.css
@@ -58,8 +58,11 @@
padding: 0.2em 0.1em;
color: var(--sortable-grip-color);
cursor: grab;
- /* The gesture is this element's own: without it a finger scrolls the page instead. */
- touch-action: none;
+ /* The page scrolls from here as it does anywhere else. What stops it once a segment is being
+ carried is the move being refused, which leaves the gesture with the card. */
+ touch-action: pan-y;
+ -webkit-touch-callout: none;
+ user-select: none;
& svg {
width: 1.1em;
@@ -74,6 +77,16 @@
}
}
+ /* A thumb reaches for more than the mark it aims at. What the padding adds here the margin takes
+ back, so the grip is easier to land on without the segments standing any taller. */
+ @media (pointer: coarse) {
+ .tn-sortable-grip {
+ margin-block: -0.5em;
+ margin-inline-end: calc(var(--card-padding-inline) / -2);
+ padding: 0.7em calc(var(--card-padding-inline) / 2);
+ }
+ }
+
/* Under the pointer rather than in the flow: it follows the finger, and the transition the other
segments slide by would drag it a frame behind. */
.tn-sortable-dragging {
diff --git a/apps/client/src/widgets/react/SortableCard.spec.tsx b/apps/client/src/widgets/react/SortableCard.spec.tsx
index bde1ef62e9..e2a4971aea 100644
--- a/apps/client/src/widgets/react/SortableCard.spec.tsx
+++ b/apps/client/src/widgets/react/SortableCard.spec.tsx
@@ -14,12 +14,18 @@ vi.mock("../../services/i18n", () => ({
const SEGMENT_HEIGHT = 40;
const SEGMENT_GAP = 4;
+/** What the card asks of a finger before it carries a segment: how long, and how still. */
+const TOUCH_HOLD = 400;
+const TOUCH_SLACK = 8;
+
describe("SortableCard", () => {
let container: HTMLElement;
let items: SortableItem[];
let changed: SortableItem[][];
beforeEach(() => {
+ // The rest a finger keeps is waited out rather than lived through.
+ vi.useFakeTimers();
items = [
{ key: "a", caption: "Alpha" },
{ key: "b", caption: "Beta" },
@@ -29,6 +35,7 @@ describe("SortableCard", () => {
});
afterEach(() => {
+ vi.useRealTimers();
render(null, container);
container.remove();
});
@@ -248,18 +255,82 @@ describe("SortableCard", () => {
expect(changed).toEqual([]);
});
- /** A finger carries it the same way a mouse does, the grip answering for the gesture. */
- it("is carried by a finger as well as by a pointer", () => {
+ /**
+ * A finger carries it the same way a mouse does, once it has rested on the grip. The grip
+ * answers for the gesture as soon as it is touched, so a finger that meant to scroll the
+ * page scrolls nothing wherever it lands on one: resting first is what tells them apart.
+ */
+ it("is carried by a finger, once it has rested on the grip", () => {
draw();
- grab(segmentOf("a"), 0, { pointerType: "touch", button: -1 });
- moveTo(SEGMENT_HEIGHT + SEGMENT_GAP + 1, { pointerType: "touch" });
+ touch(segmentOf("a"));
+ // Nothing is taken up by the press itself, however long the finger is there for.
+ expect(segmentOf("a")?.className).not.toContain("tn-sortable-dragging");
+
+ rest();
+ // Held now, which the segment says for itself before it has gone anywhere.
+ expect(segmentOf("a")?.className).toContain("tn-sortable-dragging");
+ moveTo(SEGMENT_HEIGHT + SEGMENT_GAP + 1, { pointerType: "touch" });
expect(captions()).toEqual([ "Beta", "Alpha", "Gamma" ]);
+
drop();
expect(changed.at(-1)?.map((item) => item.key)).toEqual([ "b", "a", "c" ]);
});
+ /**
+ * The page is the gesture's until a segment is taken up: a finger that came to scroll
+ * scrolls, wherever on the card it landed. Once one is being carried the move is refused,
+ * which is what holds the page still under it.
+ */
+ it("leaves the page to scroll until a segment is being carried", () => {
+ draw();
+
+ touch(segmentOf("a"));
+ expect(scrolls()).toBe(true);
+
+ rest();
+ expect(scrolls()).toBe(false);
+
+ drop();
+ expect(scrolls()).toBe(true);
+ });
+
+ it("takes nothing up for a finger that strays before it has rested", () => {
+ draw();
+
+ touch(segmentOf("a"));
+ moveTo(SEGMENT_HEIGHT, { pointerType: "touch" });
+ rest();
+
+ expect(segmentOf("a")?.className).not.toContain("tn-sortable-dragging");
+ expect(captions()).toEqual([ "Alpha", "Beta", "Gamma" ]);
+ expect(changed).toEqual([]);
+ });
+
+ /** A finger settling by a pixel or two is still resting, and carries nothing off by it. */
+ it("takes it up for a finger that barely moves, from where the finger now is", () => {
+ draw();
+
+ touch(segmentOf("a"));
+ moveTo(TOUCH_SLACK, { pointerType: "touch" });
+ rest();
+
+ expect(segmentOf("a")?.className).toContain("tn-sortable-dragging");
+ expect(shift(segmentOf("a"))).toBe(0);
+ });
+
+ it("takes nothing up for a finger lifted before it has rested", () => {
+ draw();
+
+ touch(segmentOf("a"));
+ drop();
+ rest();
+
+ expect(segmentOf("a")?.className).not.toContain("tn-sortable-dragging");
+ expect(changed).toEqual([]);
+ });
+
it("carries a card of one entry nowhere, and says nothing about it", () => {
items = [ { key: "a", caption: "Alpha" } ];
draw();
@@ -615,6 +686,24 @@ 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 });
+ }
+
+ /** Whether a finger moving now would still scroll the page rather than carry a segment. */
+ function scrolls() {
+ const move = new Event("touchmove", { bubbles: true, cancelable: true });
+ act(() => { list()?.dispatchEvent(move); });
+ return !move.defaultPrevented;
+ }
+
+ /** Waits out the rest a finger has to keep before the segment follows it. */
+ function rest() {
+ act(() => { vi.advanceTimersByTime(TOUCH_HOLD + 10); });
+ lay();
+ }
+
function grab(segment: HTMLElement | null, at = 0, options: Record<string, unknown> = {}) {
grabbedAt = at;
grabbedFrom = segment?.offsetTop ?? 0;
diff --git a/apps/client/src/widgets/react/SortableCard.tsx b/apps/client/src/widgets/react/SortableCard.tsx
index bdd2e0aa67..ca2568ee4e 100644
--- a/apps/client/src/widgets/react/SortableCard.tsx
+++ b/apps/client/src/widgets/react/SortableCard.tsx
@@ -13,6 +13,18 @@ import Icon from "./Icon";
/** How many buttons the foot of a card holds before they are too narrow to read. */
const MAX_CREATION_BUTTONS = 3;
+/**
+ * How long a finger rests on the grip before the segment follows it.
+ *
+ * A finger on a grip is as likely to be scrolling the page as carrying a segment, and resting is
+ * what tells the two apart: one that moves on scrolls as it would anywhere else, and one that
+ * stays takes the segment up. Matches the board's own cards.
+ */
+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;
+
/** One entry of a {@link SortableCard}, named by a key that outlives the order it stands in. */
export interface SortableItem {
key: string;
@@ -78,6 +90,8 @@ export function SortableCard<T extends SortableItem>({
const listRef = useRef<HTMLDivElement>(null);
const adderRef = useRef<HTMLElement>(null);
const dragRef = useRef<Drag<T>>();
+ /** A finger resting on a grip, which becomes a carry once it has rested long enough. */
+ const holdRef = useRef<Hold>();
/**
* The order while a segment is being carried, which the caller is told of once it lands.
*
@@ -113,6 +127,8 @@ export function SortableCard<T extends SortableItem>({
grow: (segment) => segment.dataset.key === addedKey
});
+ useEffect(() => () => window.clearTimeout(holdRef.current?.timer), []);
+
useLayoutEffect(() => {
const key = pendingFocus.current;
if (!key) {
@@ -180,22 +196,17 @@ export function SortableCard<T extends SortableItem>({
}
}, [ onChange, segmentOf ]);
- const beginDrag = useCallback((event: PointerEvent, key: string) => {
- const list = listRef.current;
+ /** Takes up the segment, which is where the carry begins and where it is drawn as held. */
+ const startDrag = useCallback((key: string, pointerId: number, clientY: number) => {
const segment = segmentOf(key);
- if (!list || !segment || (event.pointerType === "mouse" && event.button !== 0)) {
+ if (!segment) {
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);
-
dragRef.current = {
key,
- pointerId: event.pointerId,
- grabbedAt: event.clientY,
+ pointerId,
+ grabbedAt: clientY,
from: segment.offsetTop,
height: segment.offsetHeight,
order: shown,
@@ -207,6 +218,46 @@ export function SortableCard<T extends SortableItem>({
segment.focus();
}, [ segmentOf, select, shown ]);
+ const cancelHold = useCallback(() => {
+ window.clearTimeout(holdRef.current?.timer);
+ holdRef.current = undefined;
+ }, []);
+
+ const beginDrag = useCallback((event: PointerEvent, key: string) => {
+ const list = listRef.current;
+ if (!list || !segmentOf(key) || (event.pointerType === "mouse" && event.button !== 0)) {
+ 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);
+
+ // 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.
+ if (event.pointerType === "mouse") {
+ startDrag(key, event.pointerId, event.clientY);
+ return;
+ }
+
+ cancelHold();
+ holdRef.current = {
+ pointerId: event.pointerId,
+ from: { x: event.clientX, y: event.clientY },
+ // Where the finger is when the rest is over, so a hand that settles by a pixel or two
+ // does not carry the segment off by as much.
+ at: event.clientY,
+ timer: window.setTimeout(() => {
+ const held = holdRef.current;
+ holdRef.current = undefined;
+ if (held) {
+ startDrag(key, held.pointerId, held.at);
+ }
+ }, TOUCH_HOLD_MS)
+ };
+ }, [ cancelHold, segmentOf, startDrag ]);
+
const dragTo = useCallback((clientY: number) => {
const drag = dragRef.current;
const list = listRef.current;
@@ -314,13 +365,39 @@ export function SortableCard<T extends SortableItem>({
className="tn-sortable-list"
role="list"
onPointerMove={(event) => {
+ const held = holdRef.current;
+ if (held?.pointerId === event.pointerId) {
+ // Gone before it settled, which is a finger on its way somewhere else.
+ held.at = event.clientY;
+ if (Math.hypot(event.clientX - held.from.x, event.clientY - held.from.y)
+ > TOUCH_SLACK_PX) {
+ cancelHold();
+ }
+
+ return;
+ }
+
if (dragRef.current?.pointerId === event.pointerId) {
event.preventDefault();
dragTo(event.clientY);
}
}}
- onPointerUp={() => endDrag(true)}
- onPointerCancel={() => endDrag(false)}
+ // Refused only while a segment is being carried, which is what keeps the page
+ // still under it. Until then the gesture is the page's, so a finger that came to
+ // scroll scrolls, grip or no grip.
+ onTouchMove={(event) => {
+ if (dragRef.current) {
+ event.preventDefault();
+ }
+ }}
+ onPointerUp={() => {
+ cancelHold();
+ endDrag(true);
+ }}
+ onPointerCancel={() => {
+ cancelHold();
+ endDrag(false);
+ }}
>
{shown.map((item, index) => (
<section
@@ -390,6 +467,16 @@ export function SortableCard<T extends SortableItem>({
);
}
+/** A finger resting on a grip, until it has rested long enough to carry the segment. */
+interface Hold {
+ pointerId: number;
+ /** Where it went down, against which a stray is measured. */
+ from: { x: number, y: number };
+ /** Where it is now, which is where the carry begins from. */
+ at: number;
+ timer: number;
+}
+
/** A segment being carried: where it was taken from, and the order it has reached. */
interface Drag<T extends SortableItem> {
key: string;