Skip to content
Merged
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
13 changes: 13 additions & 0 deletions app/src/components/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useRender } from "@base-ui/react/use-render";
import { IconLayoutSidebar } from "@tabler/icons-react";
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
Expand Down Expand Up @@ -356,6 +357,7 @@ function SidebarRail({ className, ...props }: React.ComponentProps<"button">) {
const { toggleSidebar, state, isMobile } = useSidebar();
const resize = React.useContext(SidebarResizeContext);
const drag = React.useRef<{
panel: HTMLElement;
startX: number;
startWidth: number;
min: number;
Expand Down Expand Up @@ -383,6 +385,7 @@ function SidebarRail({ className, ...props }: React.ComponentProps<"button">) {
const startWidth = panel.getBoundingClientRect().width;
const min = narrowest(panel);
drag.current = {
panel,
startX: event.clientX,
startWidth,
min,
Expand Down Expand Up @@ -411,6 +414,16 @@ function SidebarRail({ className, ...props }: React.ComponentProps<"button">) {
drag.current = null;
if (!current?.moved || !resize) return;
dragged.current = true;
/*
* Drawn at the released width before the sidebar eases again. The last move's width may not
* be drawn yet (a busy window, or a browser handing over a held-back move just ahead of the
* release), and drawn together with the easing coming back, the sidebar eased the last few
* pixels after the pointer instead of being where it was let go. Reading the panel's box makes
* the browser lay it out at that width now, while it does not ease, so turning the easing back
* on leaves nothing to ease.
*/
ReactDOM.flushSync(() => resize.setWidth(current.width));
current.panel.getBoundingClientRect();
resize.setResizing(false);
resize.commitWidth(current.width);
}}
Expand Down
62 changes: 62 additions & 0 deletions app/tests/sidebar-zoom-drag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,43 @@ async function drag(page: Page, by: number) {
await page.mouse.up();
}

/**
* A drag whose last move and release reach the page together, before it has drawn that move: what
* a busy window does, and what a browser does when it holds a move back for the next frame and then
* hands it over just ahead of the release. Pressed and moved by hand most of the way; the last move
* and the release dispatched in one go; then the hand let go.
*/
async function dragReleasedWithLastMove(page: Page, by: number) {
const rail = page.locator('[data-slot="sidebar-rail"]');
const box = await rail.boundingBox();
if (!box) throw new Error("the sidebar's edge has no box");
const x = box.x + box.width / 4;
const y = box.y + box.height / 2;
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + by / 2, y, { steps: 6 });
await rail.evaluate(
(element, at) => {
for (const type of ["pointermove", "pointerup"])
element.dispatchEvent(
new PointerEvent(type, {
bubbles: true,
cancelable: true,
composed: true,
pointerId: 1,
pointerType: "mouse",
isPrimary: true,
button: type === "pointerup" ? 0 : -1,
clientX: at.x,
clientY: at.y,
}),
);
},
{ x: x + by, y },
);
await page.mouse.up();
}

async function shot(page: Page, name: string) {
if (!shots) return;
mkdirSync(shots, { recursive: true });
Expand Down Expand Up @@ -431,6 +468,31 @@ describe.skipIf(!asked)("the sidebar's edge at any zoom", () => {
}
}, 120_000);

/*
* Let go, the sidebar is at the width it was let go at, there and then: it follows the pointer
* to the end rather than easing the last of the way after it. It used to ease when the release
* came before the last move was drawn, so now and then it was still a few pixels short.
*/
test(`${shape.name}: let go just after the last move, the sidebar is drawn at the released width at once`, async () => {
for (const [engine, browser] of engines) {
const page = await open(browser, shape, ZOOMED);
try {
for (const by of [-40, 30]) {
const before = (await measured(page)).width;
await dragReleasedWithLastMove(page, by);
expect({ engine, ...(await measured(page)) }).toEqual({
engine,
width: before + by,
window: ZOOMED.width,
stored: String(before + by),
});
}
} finally {
await page.close();
}
}
}, 120_000);

/*
* "It stops way too wide": at 100% on a large screen, a drag goes past the old floor down to the
* new one, and the next page drawn starts at that width.
Expand Down
Loading