fix(sidebar): add draggable horizontal scrollbar
This commit is contained in:
5 files changed
+351
-32
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sidebarScrollbarGeometry } from "@/lib/sidebar/sidebarScrollbar";
|
||||
|
||||
describe("sidebarScrollbarGeometry", () => {
|
||||
it("returns no thumb when content fits the viewport", () => {
|
||||
expect(sidebarScrollbarGeometry({ scrollOffset: 0, viewportSize: 300, contentSize: 300, trackSize: 300 })).toEqual({
|
||||
thumbOffset: 0,
|
||||
thumbSize: 0,
|
||||
maxThumbOffset: 0,
|
||||
maxScrollOffset: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("maps horizontal content scrolling onto the draggable track", () => {
|
||||
expect(sidebarScrollbarGeometry({ scrollOffset: 150, viewportSize: 300, contentSize: 600, trackSize: 300 })).toEqual({
|
||||
thumbOffset: 75,
|
||||
thumbSize: 150,
|
||||
maxThumbOffset: 150,
|
||||
maxScrollOffset: 300,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the thumb usable for very wide content and clamps its position", () => {
|
||||
expect(sidebarScrollbarGeometry({ scrollOffset: 10_000, viewportSize: 200, contentSize: 10_000, trackSize: 200 })).toEqual({
|
||||
thumbOffset: 176,
|
||||
thumbSize: 24,
|
||||
maxThumbOffset: 176,
|
||||
maxScrollOffset: 9_800,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { alignedSidebarCommentLabelWidths, trailingCommentAvailableWidth, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout";
|
||||
import { alignedSidebarCommentLabelWidths, sidebarTreeNaturalContentWidth, trailingCommentAvailableWidth, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout";
|
||||
|
||||
describe("sidebar tree item layout", () => {
|
||||
it("keeps a table row constrained when it displays a comment", () => {
|
||||
@@ -31,4 +31,21 @@ describe("sidebar tree item layout", () => {
|
||||
expect(trailingCommentAvailableWidth(100, 100)).toBe(0);
|
||||
expect(trailingCommentAvailableWidth(99, 100)).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps the natural width of the widest node in the complete virtual tree", () => {
|
||||
const width = sidebarTreeNaturalContentWidth(
|
||||
[
|
||||
{ depth: 1, label: "visible", usesNaturalWidth: true },
|
||||
{ depth: 4, label: "widest-node-outside-the-mounted-window", usesNaturalWidth: true, trailingWidth: 20 },
|
||||
{ depth: 8, label: "constrained metadata row", usesNaturalWidth: false },
|
||||
],
|
||||
(text) => text.length * 7,
|
||||
);
|
||||
|
||||
expect(width).toBe(4 * 16 + 8 + 54 + "widest-node-outside-the-mounted-window".length * 7 + 20);
|
||||
});
|
||||
|
||||
it("returns zero when no tree row uses natural width", () => {
|
||||
expect(sidebarTreeNaturalContentWidth([{ depth: 2, label: "commented", usesNaturalWidth: false }], (text) => text.length * 7)).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
export interface SidebarScrollbarGeometryOptions {
|
||||
scrollOffset: number;
|
||||
viewportSize: number;
|
||||
contentSize: number;
|
||||
trackSize: number;
|
||||
minThumbSize?: number;
|
||||
}
|
||||
|
||||
export interface SidebarScrollbarGeometry {
|
||||
thumbOffset: number;
|
||||
thumbSize: number;
|
||||
maxThumbOffset: number;
|
||||
maxScrollOffset: number;
|
||||
}
|
||||
|
||||
export function sidebarScrollbarGeometry({ scrollOffset, viewportSize, contentSize, trackSize, minThumbSize = 24 }: SidebarScrollbarGeometryOptions): SidebarScrollbarGeometry {
|
||||
if (trackSize <= 0 || contentSize <= viewportSize) {
|
||||
return { thumbOffset: 0, thumbSize: 0, maxThumbOffset: 0, maxScrollOffset: 0 };
|
||||
}
|
||||
|
||||
const thumbSize = Math.max(minThumbSize, Math.min(trackSize, (viewportSize / contentSize) * trackSize));
|
||||
const maxThumbOffset = Math.max(0, trackSize - thumbSize);
|
||||
const maxScrollOffset = Math.max(1, contentSize - viewportSize);
|
||||
const thumbOffset = Math.min(maxThumbOffset, Math.max(0, (scrollOffset / maxScrollOffset) * maxThumbOffset));
|
||||
return { thumbOffset, thumbSize, maxThumbOffset, maxScrollOffset };
|
||||
}
|
||||
@@ -71,6 +71,26 @@ export interface SidebarCommentAlignmentItem {
|
||||
labelWidth: number;
|
||||
}
|
||||
|
||||
export interface SidebarTreeNaturalWidthItem {
|
||||
depth: number;
|
||||
label: string;
|
||||
usesNaturalWidth: boolean;
|
||||
trailingWidth?: number;
|
||||
}
|
||||
|
||||
// Right padding, expander/icon widths and the two flex gaps before the label.
|
||||
const sidebarTreeRowChromeWidth = 54;
|
||||
|
||||
export function sidebarTreeNaturalContentWidth(items: readonly SidebarTreeNaturalWidthItem[], measureText: (text: string) => number): number {
|
||||
let width = 0;
|
||||
for (const item of items) {
|
||||
if (!item.usesNaturalWidth) continue;
|
||||
const paddingLeft = item.depth * 16 + 8;
|
||||
width = Math.max(width, Math.ceil(paddingLeft + sidebarTreeRowChromeWidth + measureText(item.label) + (item.trailingWidth ?? 0)));
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
export function alignedSidebarCommentLabelWidths(items: readonly SidebarCommentAlignmentItem[]): Map<string, number> {
|
||||
const ancestorIds: string[] = [];
|
||||
const parentIdByCommentId = new Map<string, string>();
|
||||
|
||||
Reference in new issue
Block a user