Skip to content

Commit a92cb65

Browse files
author
cid
committed
fix(shell): animate the sidebar track from its real width
The collapsible navigation animated max-width from a container-relative ceiling far wider than the sidebar, so hiding held still for the first half of the transition and showing finished early. The sidebar also narrowed to its minimum while moving, re-measuring every row each frame. Animate a single grid track between 1fr and 0fr instead, which runs from the sidebar's actual extent to zero, and hold the sidebar at its own width until it is fully expanded. Tighten the geometry test to require early, midpoint and monotonic motion with a constant sidebar width, and make the AppShell test isolate selection changes. Signed-off-by: cid <d9f92a72922bf45c17379a47d64dae84b6020397c2d5a52b5317d512068cd9d3@buzz.block.builderlab.xyz>
1 parent 4f97e14 commit a92cb65

4 files changed

Lines changed: 103 additions & 54 deletions

File tree

‎src/app/shell/AppShell.test.tsx‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @vitest-environment jsdom
22
import "@testing-library/jest-dom/vitest";
3-
import { cleanup, render, screen } from "@testing-library/react";
3+
import { cleanup, render, screen, within } from "@testing-library/react";
44
import userEvent from "@testing-library/user-event";
55
import type { ReactNode } from "react";
66
import { afterEach, beforeEach, expect, it, vi } from "vitest";
7+
import type { RegisteredPage } from "../../features/pages/service";
78
import { createServices, type AppServices } from "../services";
89
import { ToastProvider } from "../../shared/design-system/ui/Toast";
910
import { AppShell } from "./AppShell";
@@ -40,13 +41,26 @@ it("hides and shows the channel sidebar without re-rendering its content", async
4041
);
4142
}
4243
const sidebar = (pages: ReactNode) => <Sidebar>{pages}</Sidebar>;
44+
// Stable inputs, so only `selected` changes between the renders below.
45+
const pages: RegisteredPage[] = [
46+
{
47+
id: "channels",
48+
key: "buzz.channels/channels",
49+
pluginId: "buzz.channels",
50+
revision: "1",
51+
title: "Channels",
52+
component: () => null,
53+
primary: true,
54+
},
55+
];
56+
const onSelect = () => {};
4357
const shell = (selected: string) => (
4458
<ToastProvider>
4559
<AppShell
46-
pages={[]}
60+
pages={pages}
4761
selected={selected}
4862
navigationAttempt=""
49-
onSelect={() => {}}
63+
onSelect={onSelect}
5064
tone="default"
5165
sidebar={sidebar}
5266
communities={current.communities}
@@ -60,8 +74,10 @@ it("hides and shows the channel sidebar without re-rendering its content", async
6074
const content = screen.getByRole("complementary", {
6175
name: "Channel sidebar",
6276
});
63-
const navigation = content.parentElement;
77+
const navigation = document.getElementById("shell-navigation");
6478
const rendered = renders.mock.calls.length;
79+
const row = () => within(content).getByRole("button", { name: "Messages" });
80+
expect(row()).toHaveAttribute("aria-current", "page");
6581

6682
for (const [label, hidden] of [
6783
["Hide Channel sidebar", "true"],
@@ -75,7 +91,8 @@ it("hides and shows the channel sidebar without re-rendering its content", async
7591
expect(content).toBeInTheDocument();
7692
expect(renders).toHaveBeenCalledTimes(rendered);
7793

78-
// Shell inputs that the sidebar receives still reach it.
94+
// A selection change still reaches the page rows the sidebar renders.
7995
rerender(shell("buzz.agents/agents"));
8096
expect(renders.mock.calls.length).toBeGreaterThan(rendered);
97+
expect(row()).not.toHaveAttribute("aria-current");
8198
});

‎src/app/shell/AppShell.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export function AppShell({
232232
}
233233
}}
234234
>
235-
{navigation}
235+
<div className="shell-navigation-content">{navigation}</div>
236236
</div>
237237
<main
238238
id="main-content"

‎src/shared/styles/globals.css‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,16 @@
219219
flex: 0 0 18px;
220220
place-items: center;
221221
}
222-
.shell-navigation {
222+
.shell-navigation,
223+
.shell-navigation-content {
223224
display: contents;
224225
}
226+
/* A single fr track animates from the sidebar's real extent (not a larger
227+
ceiling) to zero, so movement starts on the first frame. */
225228
.shell-navigation[data-sidebar-collapsible] {
226-
display: flex;
229+
display: grid;
230+
grid-template-columns: 1fr;
231+
grid-template-rows: minmax(0, 1fr);
227232
min-width: 0;
228233
flex: none;
229234
width: max-content;
@@ -232,18 +237,17 @@
232237
clip-path: none;
233238
/* Release resize help only after expansion; contain content while moving. */
234239
transition:
235-
max-width var(--duration-settle) ease-in-out,
240+
grid-template-columns var(--duration-settle) ease-in-out,
236241
clip-path var(--duration-settle) step-end allow-discrete;
237242
}
238243
.shell-navigation[data-sidebar-collapsible][aria-hidden="true"] {
239-
max-width: 0;
244+
grid-template-columns: 0fr;
240245
clip-path: inset(0);
241246
transition-timing-function: ease-in-out, step-start;
242247
}
243-
@media (prefers-reduced-motion: reduce) {
244-
.shell-navigation[data-sidebar-collapsible] {
245-
transition: none;
246-
}
248+
.shell-navigation[data-sidebar-collapsible] > .shell-navigation-content {
249+
display: flex;
250+
min-width: 0;
247251
}
248252
.shell-body {
249253
@apply relative flex min-h-0 min-w-0 flex-1 pl-0 pr-2 pb-2 sm:pl-0 sm:pr-4 sm:pb-4;
@@ -263,10 +267,28 @@
263267
.shell-sidebar-default > * {
264268
@apply min-w-0 flex-1;
265269
}
266-
.shell-navigation[data-sidebar-collapsible] > .shell-sidebar,
267-
.shell-navigation[data-sidebar-collapsible] > .shell-sidebar-default {
270+
.shell-navigation[data-sidebar-collapsible]
271+
> .shell-navigation-content
272+
> :is(.shell-sidebar, .shell-sidebar-default) {
268273
flex: none;
269274
max-width: 100%;
275+
/* Hold the sidebar at its own width while the track moves; reflowing it
276+
every frame re-measures every row. The clamp returns once expanded. */
277+
transition: max-width var(--duration-settle) step-end allow-discrete;
278+
}
279+
.shell-navigation[data-sidebar-collapsible][aria-hidden="true"]
280+
> .shell-navigation-content
281+
> :is(.shell-sidebar, .shell-sidebar-default) {
282+
max-width: none;
283+
transition-timing-function: step-start;
284+
}
285+
@media (prefers-reduced-motion: reduce) {
286+
.shell-navigation[data-sidebar-collapsible],
287+
.shell-navigation[data-sidebar-collapsible]
288+
> .shell-navigation-content
289+
> :is(.shell-sidebar, .shell-sidebar-default) {
290+
transition: none;
291+
}
270292
}
271293
.shell-actions {
272294
@apply flex items-center justify-end;
@@ -324,7 +346,7 @@
324346
max-width: 220px;
325347
margin-right: 0;
326348
}
327-
.shell-navigation > hr {
349+
.shell-navigation-content > hr {
328350
display: none;
329351
}
330352
}

‎tests/browser/sidenav-polish.spec.mjs‎

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ test("shell toggle restores the shared sidebar for Channels and Agents", async (
395395
};
396396
});
397397
expect(track.width).toBe(expandedWidth + gutterWidth);
398-
expect(track.property).toContain("width");
398+
expect(track.property).toContain("grid-template-columns");
399399
expect(parseFloat(track.duration)).toBeGreaterThan(0);
400400
const openGap = await page.evaluate(() => {
401401
const sidebar = document.querySelector(".shell-sidebar");
@@ -407,19 +407,24 @@ test("shell toggle restores the shared sidebar for Channels and Agents", async (
407407
);
408408
});
409409
expect(openGap).toBe(gutterWidth);
410-
// Seek a paused real transition so runner speed cannot hide an immediate jump.
411-
// Pause it when the toggle commits, before any frame: transitionrun arrives in
412-
// a later frame, and a stalled WebKit frame can finish the 220ms transition
413-
// first. Descendant transitions (the resize help) also bubble transitionrun.
410+
// Seek paused real transitions so runner speed cannot hide an immediate jump.
411+
// Pause them when the toggle commits, before any frame: transitionrun arrives
412+
// in a later frame, and a stalled WebKit frame can finish the 220ms transition
413+
// first. Only the track and the sidebar it holds are seeked; the resize help's
414+
// own descendant transitions are unrelated.
414415
for (const label of ["Hide Channel sidebar", "Show Channel sidebar"]) {
415416
const samples = await page.evaluate(async (label) => {
416417
const nav = document.querySelector("#shell-navigation");
418+
const sidebar = nav.querySelector(".shell-sidebar");
417419
const button = document.querySelector(`button[aria-label="${label}"]`);
418420
const committed = new Promise((resolve) => {
419421
const observer = new MutationObserver(() => {
420422
observer.disconnect();
421423
// getAnimations() flushes style, so the toggle's transitions exist.
422-
const transitions = nav.getAnimations();
424+
const transitions = [
425+
...nav.getAnimations(),
426+
...sidebar.getAnimations(),
427+
];
423428
for (const transition of transitions) transition.pause();
424429
resolve(transitions);
425430
});
@@ -430,45 +435,50 @@ test("shell toggle restores the shared sidebar for Channels and Agents", async (
430435
});
431436
button.click();
432437
const transitions = await committed;
433-
const animation = transitions.find(
434-
(animation) => animation.transitionProperty === "max-width",
435-
);
436-
if (!animation) throw new Error("Missing sidebar transition");
437438
try {
438-
const duration = animation.effect.getTiming().duration;
439-
const clipping = transitions.find(
440-
(animation) => animation.transitionProperty === "clip-path",
439+
const properties = transitions.map(
440+
(transition) => transition.transitionProperty,
441441
);
442-
if (!clipping) throw new Error("Missing containment transition");
443-
try {
444-
return [0, 0.25, 0.5, 0.75, 1].map((progress) => {
445-
animation.currentTime = duration * progress;
446-
clipping.currentTime = duration * progress;
447-
return {
448-
width: nav.getBoundingClientRect().width,
449-
clip: getComputedStyle(nav).clipPath,
450-
};
451-
});
452-
} finally {
453-
clipping.finish();
454-
await clipping.finished;
455-
}
442+
for (const property of [
443+
"grid-template-columns",
444+
"clip-path",
445+
"max-width",
446+
])
447+
if (!properties.includes(property))
448+
throw new Error(`Missing ${property} transition`);
449+
return [0, 0.1, 0.25, 0.5, 0.75, 1].map((progress) => {
450+
for (const transition of transitions)
451+
transition.currentTime =
452+
transition.effect.getTiming().duration * progress;
453+
return {
454+
width: nav.getBoundingClientRect().width,
455+
sidebar: sidebar.getBoundingClientRect().width,
456+
clip: getComputedStyle(nav).clipPath,
457+
};
458+
});
456459
} finally {
457-
animation.finish();
458-
await animation.finished;
460+
for (const transition of transitions) transition.finish();
461+
await Promise.all(transitions.map((transition) => transition.finished));
459462
}
460463
}, label);
461464
const width = expandedWidth + gutterWidth;
462-
expect(
463-
samples.some((sample) => sample.width > 0 && sample.width < width),
464-
).toBe(true);
465-
expect(samples[0].width).toBe(label.startsWith("Hide") ? width : 0);
466-
expect(samples.at(-1).width).toBe(label.startsWith("Hide") ? 0 : width);
465+
const hiding = label.startsWith("Hide");
466+
// The track leaves its real extent on the first sampled step and crosses
467+
// the midpoint halfway through, instead of stalling behind a larger
468+
// ceiling; the sidebar itself keeps its width and is only clipped.
469+
expect(samples[0].width).toBe(hiding ? width : 0);
470+
expect(samples[1].width).toBeGreaterThan(0);
471+
expect(samples[1].width).toBeLessThan(width);
472+
expect(Math.abs(samples[3].width - width / 2)).toBeLessThan(1);
473+
for (let index = 1; index < samples.length; index++) {
474+
const step = samples[index].width - samples[index - 1].width;
475+
expect(hiding ? -step : step).toBeGreaterThan(0);
476+
}
477+
expect(samples.at(-1).width).toBe(hiding ? 0 : width);
478+
for (const sample of samples) expect(sample.sidebar).toBe(expandedWidth);
467479
for (const sample of samples.slice(1, -1))
468480
expect(sample.clip).toBe("inset(0px)");
469-
expect(samples.at(-1).clip).toBe(
470-
label.startsWith("Hide") ? "inset(0px)" : "none",
471-
);
481+
expect(samples.at(-1).clip).toBe(hiding ? "inset(0px)" : "none");
472482
}
473483

474484
await page.emulateMedia({ reducedMotion: "reduce" });

0 commit comments

Comments
 (0)