Skip to content
Open
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
51 changes: 32 additions & 19 deletions apps/web/src/components/Sidebar.motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ function progress(animation: Animation) {
: (animation.effect?.getComputedTiming().progress ?? 0);
}

/** Position the list so all direct children's offsetTop share an origin. */
export function forEachSidebarListRow(
list: HTMLElement,
visit: (row: HTMLElement, folder?: HTMLElement) => void,
) {
for (const child of list.children) {
if (!(child instanceof HTMLElement)) continue;
if (child.hasAttribute("data-sidebar-folder-group")) {
for (const row of child.firstElementChild?.children ?? []) {
if (row instanceof HTMLElement) visit(row, child);
}
} else {
visit(child);
}
}
}

/** Position the list and each folder group so a row's offsets plus its folder's share an origin. */
export function createSidebarListMotion(parent: HTMLUListElement) {
let positions: Map<HTMLElement, RowPosition> | null = null;
let disposed = false;
Expand Down Expand Up @@ -126,19 +142,16 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
suspend();
return;
}
const next = new Map(
Array.from(parent.children)
.filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
.map((node) => [
node,
{
top: node.offsetTop,
left: node.offsetLeft,
width: node.offsetWidth,
height: node.offsetHeight,
},
]),
);
const next = new Map<HTMLElement, RowPosition>();
forEachSidebarListRow(parent, (node, folder) => {
if (exiting.has(node)) return;
next.set(node, {
top: node.offsetTop + (folder?.offsetTop ?? 0),
left: node.offsetLeft + (folder?.offsetLeft ?? 0),
width: node.offsetWidth,
height: node.offsetHeight,
});
});
let fadeCount = 0;
if (positions !== null) {
for (const [node, position] of positions) {
Expand Down Expand Up @@ -237,11 +250,11 @@ export function createSidebarListMotion(parent: HTMLUListElement) {
release() {
suspend();
const origin = parent.getBoundingClientRect().top;
released = new Map(
Array.from(parent.children)
.filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
.map((node) => [node, node.getBoundingClientRect().top - origin]),
);
const tops = new Map<HTMLElement, number>();
forEachSidebarListRow(parent, (node) => {
if (!exiting.has(node)) tops.set(node, node.getBoundingClientRect().top - origin);
});
released = tops;
},
suspend,
dispose() {
Expand Down
41 changes: 26 additions & 15 deletions apps/web/src/components/Sidebar.shiftGuard.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import { userEvent } from "vite-plus/test/browser";
import { fakeTimers, render } from "../../test/render";
import { createSidebarShiftGuard } from "./Sidebar.shiftGuard";

function mountList(container: HTMLElement, names: readonly string[]) {
function mountList(container: HTMLElement, names: readonly string[], insideFolder = false) {
const list = document.createElement("ul");
list.style.cssText = "position: relative; margin: 0; padding: 0; width: 200px; list-style: none";
const rowList = insideFolder ? document.createElement("ul") : list;
if (insideFolder) {
const folder = document.createElement("li");
folder.dataset.sidebarFolderGroup = "";
folder.style.position = "relative";
folder.append(rowList);
list.append(folder);
}
const pressed: string[] = [];
const rows = new Map<string, HTMLLIElement>();
const add = (name: string) => {
Expand All @@ -15,7 +23,7 @@ function mountList(container: HTMLElement, names: readonly string[]) {
button.textContent = name;
button.addEventListener("click", () => pressed.push(name));
row.append(button);
list.append(row);
rowList.append(row);
rows.set(name, row);
};
for (const name of names) add(name);
Expand All @@ -41,20 +49,23 @@ function duringPress(act: () => void) {
}

describe("sidebar shift guard", () => {
it("drops a press on a row that slid up on its own, then accepts it after the delay", async () => {
await using screen = await render(null);
using list = mountList(screen.container, ["A", "B", "C"]);
using _clock = fakeTimers();

list.remove("A");
list.guard.update();
await userEvent.click(list.button("B"));
expect(list.pressed).toEqual([]);
it.each([false, true])(
"drops a press on a row that slid up on its own (inside folder: %s)",
async (insideFolder) => {
await using screen = await render(null);
using list = mountList(screen.container, ["A", "B", "C"], insideFolder);
using _clock = fakeTimers();

vi.advanceTimersByTime(500);
await userEvent.click(list.button("B"));
expect(list.pressed).toEqual(["B"]);
});
list.remove("A");
list.guard.update();
await userEvent.click(list.button("B"));
expect(list.pressed).toEqual([]);

vi.advanceTimersByTime(500);
await userEvent.click(list.button("B"));
expect(list.pressed).toEqual(["B"]);
},
);

it("keeps rows that did not move, or barely moved, clickable", async () => {
await using screen = await render(null);
Expand Down
17 changes: 11 additions & 6 deletions apps/web/src/components/Sidebar.shiftGuard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { forEachSidebarListRow } from "./Sidebar.motion";

// How long a row the list just moved ignores presses. A click that was already
// on its way when the row slid under the pointer lands within this window.
const MOVED_ROW_PRESS_DELAY_MS = 500;
Expand All @@ -22,17 +24,17 @@ function editsText(event: KeyboardEvent) {

function readTops(list: HTMLElement) {
const tops = new Map<HTMLElement, number>();
for (const child of list.children) {
if (child instanceof HTMLElement) tops.set(child, child.offsetTop);
}
forEachSidebarListRow(list, (row, folder) => {
tops.set(row, row.offsetTop + (folder?.offsetTop ?? 0));
});
return tops;
}

/**
* Drops presses on rows that something other than the user just moved, so a
* thread settling, waking or arriving above the pointer cannot hand its click
* to the neighbouring row. Call `update` after each commit that can move rows.
* The list must be positioned so every direct child's offsetTop shares its origin.
* The list and project folders must be positioned to provide row offsets.
*/
export function createSidebarShiftGuard(list: HTMLElement) {
const view = list.ownerDocument.defaultView;
Expand All @@ -47,8 +49,11 @@ export function createSidebarShiftGuard(list: HTMLElement) {

const rowOf = (target: EventTarget | null) => {
let node = target instanceof Node ? target : null;
while (node !== null && node.parentNode !== list) node = node.parentNode;
return node instanceof Element ? node : null;
while (node !== null && node !== list) {
if (node instanceof HTMLElement && tops.has(node)) return node;
node = node.parentNode;
}
return null;
};
const swallow = (event: Event) => {
event.preventDefault();
Expand Down
Loading
Loading