Skip to content

Commit 1a692d5

Browse files
kalvinnchaucidam
authored
fix(shell): remove sidebar toggle render and animation delays (#475)
Signed-off-by: cid <d9f92a72922bf45c17379a47d64dae84b6020397c2d5a52b5317d512068cd9d3@buzz.block.builderlab.xyz> Signed-off-by: am <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz> Co-authored-by: cid <d9f92a72922bf45c17379a47d64dae84b6020397c2d5a52b5317d512068cd9d3@buzz.block.builderlab.xyz> Co-authored-by: am <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
1 parent bc6afab commit 1a692d5

4 files changed

Lines changed: 295 additions & 136 deletions

File tree

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// @vitest-environment jsdom
2+
import "@testing-library/jest-dom/vitest";
3+
import { cleanup, render, screen, within } from "@testing-library/react";
4+
import userEvent from "@testing-library/user-event";
5+
import type { ReactNode } from "react";
6+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
7+
import type { RegisteredPage } from "../../features/pages/service";
8+
import { createServices, type AppServices } from "../services";
9+
import { ToastProvider } from "../../shared/design-system/ui/Toast";
10+
import { AppShell } from "./AppShell";
11+
12+
vi.mock("../../bundled", () => ({ bundledPlugins: [] }));
13+
14+
// jsdom has no media queries or layout observers; responsive geometry is
15+
// covered in browser tests.
16+
beforeEach(() => {
17+
vi.stubGlobal(
18+
"ResizeObserver",
19+
class {
20+
observe() {}
21+
disconnect() {}
22+
},
23+
);
24+
vi.stubGlobal("matchMedia", (media: string) => ({
25+
media,
26+
matches: false,
27+
addEventListener() {},
28+
removeEventListener() {},
29+
}));
30+
});
31+
let services: AppServices | undefined;
32+
afterEach(async () => {
33+
cleanup();
34+
await services?.dispose();
35+
services = undefined;
36+
vi.unstubAllGlobals();
37+
});
38+
39+
it("hides and shows the channel sidebar without re-rendering its content", async () => {
40+
const current = createServices();
41+
services = current;
42+
const renders = vi.fn();
43+
function Sidebar({ children }: { children: ReactNode }) {
44+
renders();
45+
return (
46+
<aside aria-label="Channel sidebar" className="shell-sidebar">
47+
{children}
48+
</aside>
49+
);
50+
}
51+
const sidebar = (pages: ReactNode) => <Sidebar>{pages}</Sidebar>;
52+
// Stable inputs, so only `selected` changes between the renders below.
53+
const pages: RegisteredPage[] = [
54+
{
55+
id: "channels",
56+
key: "buzz.channels/channels",
57+
pluginId: "buzz.channels",
58+
revision: "1",
59+
title: "Channels",
60+
component: () => null,
61+
primary: true,
62+
},
63+
];
64+
const onSelect = () => {};
65+
const shell = (selected: string) => (
66+
<ToastProvider>
67+
<AppShell
68+
pages={pages}
69+
selected={selected}
70+
navigationAttempt=""
71+
onSelect={onSelect}
72+
tone="default"
73+
sidebar={sidebar}
74+
communities={current.communities}
75+
accountActions={current.accountActions}
76+
>
77+
content
78+
</AppShell>
79+
</ToastProvider>
80+
);
81+
const { rerender } = render(shell("buzz.channels/channels"));
82+
const content = screen.getByRole("complementary", {
83+
name: "Channel sidebar",
84+
});
85+
const navigation = document.getElementById("shell-navigation");
86+
const rendered = renders.mock.calls.length;
87+
const row = () => within(content).getByRole("button", { name: "Messages" });
88+
expect(row()).toHaveAttribute("aria-current", "page");
89+
90+
for (const [label, hidden] of [
91+
["Hide Channel sidebar", "true"],
92+
["Show Channel sidebar", "false"],
93+
["Hide Channel sidebar", "true"],
94+
["Show Channel sidebar", "false"],
95+
] as const) {
96+
await userEvent.click(screen.getByRole("button", { name: label }));
97+
expect(navigation).toHaveAttribute("aria-hidden", hidden);
98+
}
99+
expect(content).toBeInTheDocument();
100+
expect(renders).toHaveBeenCalledTimes(rendered);
101+
102+
// A selection change still reaches the page rows the sidebar renders.
103+
rerender(shell("buzz.agents/agents"));
104+
expect(renders.mock.calls.length).toBeGreaterThan(rendered);
105+
expect(row()).not.toHaveAttribute("aria-current");
106+
});

‎src/app/shell/AppShell.tsx‎

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NavigationItem } from "../../shared/design-system/ui/NavigationItem";
2-
import { useEffect, useRef, useState, type ReactNode } from "react";
2+
import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
33
import { IconButton } from "../../shared/design-system/ui/IconButton";
44
import { SidebarIcon } from "../../shared/design-system/icons";
55
import { Panel } from "../../shared/design-system/ui/Panel";
@@ -88,36 +88,50 @@ export function AppShell({
8888
}
8989
setNavigationOpen(false);
9090
}, [navigationAttempt]);
91-
// Only primary pages get a row; search below still lists every active page.
92-
// Every primary page comes from an optional plugin, so an empty list is
93-
// reachable; skip the landmark rather than announce an empty region.
94-
const primaryPages = orderPages(pages.filter((page) => page.primary));
95-
const pageNavigation = primaryPages.length ? (
96-
<nav aria-label="Pages" className="shell-pages">
97-
{primaryPages.map((page) => {
98-
const { label, icon: Icon } = pagePresentation(page);
99-
return (
100-
<NavigationItem
101-
type="button"
102-
key={page.key}
103-
onClick={() => {
104-
onSelect(page.key);
105-
document
106-
.getElementById("main-content")
107-
?.focus({ preventScroll: true });
108-
}}
109-
selected={selected === page.key}
110-
label={label}
111-
icon={
112-
<span className="shell-page-icon">
113-
<Icon aria-hidden="true" weight="bold" size={15} />
114-
</span>
115-
}
116-
/>
117-
);
118-
})}
119-
</nav>
120-
) : null;
91+
// Toggling the sidebar changes only this shell's own state. Keep the
92+
// navigation element stable across those renders so React skips the whole
93+
// sidebar subtree instead of re-rendering every row on each click.
94+
const navigation = useMemo(() => {
95+
// Only primary pages get a row; search below still lists every active page.
96+
// Every primary page comes from an optional plugin, so an empty list is
97+
// reachable; skip the landmark rather than announce an empty region.
98+
const primaryPages = orderPages(pages.filter((page) => page.primary));
99+
const pageNavigation = primaryPages.length ? (
100+
<nav aria-label="Pages" className="shell-pages">
101+
{primaryPages.map((page) => {
102+
const { label, icon: Icon } = pagePresentation(page);
103+
return (
104+
<NavigationItem
105+
type="button"
106+
key={page.key}
107+
onClick={() => {
108+
onSelect(page.key);
109+
document
110+
.getElementById("main-content")
111+
?.focus({ preventScroll: true });
112+
}}
113+
selected={selected === page.key}
114+
label={label}
115+
icon={
116+
<span className="shell-page-icon">
117+
<Icon aria-hidden="true" weight="bold" size={15} />
118+
</span>
119+
}
120+
/>
121+
);
122+
})}
123+
</nav>
124+
) : null;
125+
return sidebar ? (
126+
sidebar(pageNavigation)
127+
) : (
128+
<div className="shell-sidebar-default">
129+
<Panel as="aside" aria-label="Page sidebar">
130+
<div className="p-2">{pageNavigation}</div>
131+
</Panel>
132+
</div>
133+
);
134+
}, [pages, selected, onSelect, sidebar]);
121135
return (
122136
<div
123137
data-shell-tone={tone}
@@ -218,15 +232,7 @@ export function AppShell({
218232
}
219233
}}
220234
>
221-
{sidebar ? (
222-
sidebar(pageNavigation)
223-
) : (
224-
<div className="shell-sidebar-default">
225-
<Panel as="aside" aria-label="Page sidebar">
226-
<div className="p-2">{pageNavigation}</div>
227-
</Panel>
228-
</div>
229-
)}
235+
<div className="shell-navigation-content">{navigation}</div>
230236
</div>
231237
<main
232238
id="main-content"

‎src/shared/styles/globals.css‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,37 +219,41 @@
219219
flex: 0 0 1.125rem;
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;
230-
max-width: max(220px, calc(100% - 336px));
231235
overflow: visible;
232236
clip-path: none;
233237
/* Release resize help only after expansion; contain content while moving. */
234238
transition:
235-
max-width var(--duration-settle) ease-in-out,
239+
grid-template-columns var(--duration-settle) ease-in-out,
236240
clip-path var(--duration-settle) step-end allow-discrete;
237241
}
238242
.shell-navigation[data-sidebar-collapsible][aria-hidden="true"] {
239-
max-width: 0;
243+
grid-template-columns: 0fr;
240244
clip-path: inset(0);
241245
transition-timing-function: ease-in-out, step-start;
242246
}
243-
@media (prefers-reduced-motion: reduce) {
244-
.shell-navigation[data-sidebar-collapsible] {
245-
transition: none;
246-
}
247+
.shell-navigation[data-sidebar-collapsible] > .shell-navigation-content {
248+
display: flex;
249+
min-width: 0;
247250
}
248251
.shell-body {
249252
@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;
250253
}
251254
.shell-content {
252255
@apply relative flex h-full min-h-0 min-w-0;
256+
container-type: inline-size;
253257
}
254258
.shell-sidebar,
255259
.shell-sidebar-default {
@@ -264,10 +268,17 @@
264268
.shell-sidebar-default > * {
265269
@apply min-w-0 flex-1;
266270
}
267-
.shell-navigation[data-sidebar-collapsible] > .shell-sidebar,
268-
.shell-navigation[data-sidebar-collapsible] > .shell-sidebar-default {
271+
.shell-navigation[data-sidebar-collapsible]
272+
> .shell-navigation-content
273+
> :is(.shell-sidebar, .shell-sidebar-default) {
269274
flex: none;
270-
max-width: 100%;
275+
/* Clamp against the stable shell, not the animated navigation track. */
276+
max-width: max(220px, calc(100cqi - 336px));
277+
}
278+
@media (prefers-reduced-motion: reduce) {
279+
.shell-navigation[data-sidebar-collapsible] {
280+
transition: none;
281+
}
271282
}
272283
.shell-actions {
273284
@apply flex items-center justify-end;
@@ -327,7 +338,7 @@
327338
max-width: 220px;
328339
margin-right: 0;
329340
}
330-
.shell-navigation > hr {
341+
.shell-navigation-content > hr {
331342
display: none;
332343
}
333344
}

0 commit comments

Comments
 (0)