Skip to content

Build your personal knowledge base with Trilium Notes

client/ui: fix some issues

Adorian Doraned1a3dd090da
diff --git a/apps/client/src/widgets/react/drag_pan.spec.tsx b/apps/client/src/widgets/react/drag_pan.spec.tsx
index 476ad7797f..bbe7689434 100644
--- a/apps/client/src/widgets/react/drag_pan.spec.tsx
+++ b/apps/client/src/widgets/react/drag_pan.spec.tsx
@@ -5,7 +5,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DragPanOptions, useDragPan } from "./drag_pan";
-vi.mock("../../services/utils", () => ({ isMobile: () => false }));
+// Spread rather than replaced: the hook reaches the shared hooks module, which reads far more of
+// this than the one export under test.
+vi.mock("../../services/utils", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../../services/utils")>()),
+ isMobile: () => false
+}));
describe("useDragPan", () => {
let container: HTMLElement | undefined;
@@ -165,6 +170,21 @@ describe("useDragPan", () => {
expect(scroller.scrollLeft).toBe(released);
});
+ /** The pointer was taken away rather than let go, so the movement was never finished. */
+ it("does not glide when the gesture is cancelled", () => {
+ const scroller = setup();
+
+ press(scroller, 400);
+ move(scroller, 300, 16);
+ act(() => { scroller.dispatchEvent(pointer("pointercancel", 300, 0, {})); });
+
+ const cancelled = scroller.scrollLeft;
+ act(() => { vi.advanceTimersByTime(500); });
+
+ expect(scroller.scrollLeft).toBe(cancelled);
+ expect(scroller.className).not.toContain("panning");
+ });
+
it("stops a glide when the next pan starts", () => {
const scroller = setup();
diff --git a/apps/client/src/widgets/react/drag_pan.ts b/apps/client/src/widgets/react/drag_pan.ts
index d1f428305a..148f216ded 100644
--- a/apps/client/src/widgets/react/drag_pan.ts
+++ b/apps/client/src/widgets/react/drag_pan.ts
@@ -1,7 +1,8 @@
import { RefObject } from "preact";
-import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { isMobile } from "../../services/utils";
+import { useTrackedElement } from "./hooks";
/** How much of the speed survives each frame once the pointer is up. */
const FRICTION = 0.94;
@@ -47,15 +48,7 @@ export function useDragPan(ref: RefObject<HTMLElement>, options: DragPanOptions
// A ref holds no render of its own, so an effect keyed on one never hears the element arrive.
// Containers drawn only once their content has loaded are the ordinary case, so the element is
// tracked in state instead. Set only when it actually changes, so this settles in one pass.
- // A ref holds no render of its own, so an effect keyed on one never hears the element arrive.
- // Containers drawn only once their content has loaded are the ordinary case, so the element is
- // tracked in state instead. Set only when it actually changes, so this settles in one pass.
- const [ element, setElement ] = useState<HTMLElement | null>(null);
- useLayoutEffect(() => {
- if (ref.current !== element) {
- setElement(ref.current);
- }
- });
+ const element = useTrackedElement(ref);
const stopGlide = useCallback(() => {
if (glideRef.current !== undefined) {
@@ -144,7 +137,10 @@ export function useDragPan(ref: RefObject<HTMLElement>, options: DragPanOptions
setPanning(false);
container.releasePointerCapture?.(event.pointerId);
- if (Math.abs(velocity) > MIN_VELOCITY) {
+ // A cancelled gesture is not a release: the pointer was taken away rather than let go,
+ // so the board stops where it stands instead of carrying the movement on.
+ const cancelled = event.type === "pointercancel";
+ if (!cancelled && Math.abs(velocity) > MIN_VELOCITY) {
glide();
}
};
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 76c3739bbc..6c56113e4c 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1367,6 +1367,26 @@ export function useContextualShortcutHints(hints: ShortcutHintDefinition | (() =
useDebugValue("contextual-shortcut-hints");
}
+/**
+ * The element a ref points at, as state, so an effect keyed on it runs once the element is there.
+ *
+ * Filling a ref triggers no render, so an effect that reads `ref.current` in its dependencies never
+ * hears the element arrive. Containers drawn only once their content has loaded are the ordinary
+ * case for that.
+ */
+export function useTrackedElement<T extends HTMLElement>(ref: RefObject<T>): T | null {
+ const [ element, setElement ] = useState<T | null>(null);
+
+ // Every render, and set only where it changed, so this settles in one further pass.
+ useLayoutEffect(() => {
+ if (ref.current !== element) {
+ setElement(ref.current);
+ }
+ });
+
+ return element;
+}
+
export function useSyncedRef<T>(externalRef?: Ref<T>, initialValue: T | null = null): RefObject<T> {
const ref = useRef<T>(initialValue);
diff --git a/apps/client/src/widgets/react/scroll_fade.ts b/apps/client/src/widgets/react/scroll_fade.ts
index e677583584..d2ab502a35 100644
--- a/apps/client/src/widgets/react/scroll_fade.ts
+++ b/apps/client/src/widgets/react/scroll_fade.ts
@@ -3,7 +3,9 @@ import "./scroll_fade.css";
import clsx from "clsx";
import { RefObject } from "preact";
import { CSSProperties } from "preact/compat";
-import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+
+import { useTrackedElement } from "./hooks";
/** Fractional scroll offsets are reported at sub-pixel precision, so an edge is never tested exactly. */
const EDGE_EPSILON = 1;
@@ -41,13 +43,7 @@ export function useScrollFade(ref: RefObject<HTMLElement>, options: ScrollFadeOp
const [ overflow, setOverflow ] = useState(0);
const frameRef = useRef<number>();
- // A ref holds no render of its own, so an effect keyed on one never hears the element arrive.
- const [ element, setElement ] = useState<HTMLElement | null>(null);
- useLayoutEffect(() => {
- if (ref.current !== element) {
- setElement(ref.current);
- }
- });
+ const element = useTrackedElement(ref);
const measure = useCallback(() => {
if (!element) return;