Skip to content

Commit 76c7f7a

Browse files
cliffhallclaude
andcommitted
fix(web): preserve empty-section intent + kill accordion collapse jump (#1464 review)
Address review feedback on PR #1464: - Intent loss (review #1): the accordion's `value` is the filtered `visibleOpenSections`, so Mantine's toggle dropped open-but-empty sections from the persisted intent. Merge them back in the onChange handler so an open empty section stays open and reappears with items. Adds a unit test. - Collapse animation jump (review #3): Mantine's Collapse briefly set the panel to its full natural height (fighting the flex layout) on open/close, flashing siblings. Set `transitionDuration={0}` so flex controls the height; restore a smooth chevron rotation via CSS. Verified no jump in a real browser (panel 501→0, no spike). Since closed sections now render no content, drop the redundant (and incorrect) "Templates" toggle clicks from 4 ResourcesScreen tests (the section is open by default) and add a section-toggle test to keep the openSections handler covered. - Nits: scope the stories' chevron query to the accordion root; tighten the Collapsed assertion to the identity transform. Native-vs-styled scrollbar (review #4) split out as #1466. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 9f888bc commit 76c7f7a

5 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎clients/web/src/App.css‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@
191191
* `[data-rotate]` is set by Mantine on the chevron slot when the item is open;
192192
* the class+attribute selector outranks Mantine's `:where()`-wrapped default. */
193193

194+
/* Restore a smooth rotation: the accordion sets `transitionDuration={0}` to
195+
* disable its panel height animation (which fights the flex layout), and that
196+
* zeroes the shared `--accordion-transition-duration` the chevron would use. */
197+
.disclosure-chevron {
198+
transition: transform 200ms ease;
199+
}
200+
194201
.disclosure-chevron[data-rotate] {
195202
transform: rotate(90deg);
196203
}

‎clients/web/src/components/groups/ResourceControls/ResourceControls.stories.tsx‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import { ResourceControls } from "./ResourceControls";
1010
// rotate(90deg) — Mantine sets `data-rotate` on the chevron slot of an open
1111
// section; App.css rotates the right-pointing arrow 90° so it points down.
1212
const ROTATED_DOWN = "matrix(0, 1, -1, 0, 0, 0)";
13+
// Identity — a closed section's right-pointing chevron at its natural 0°.
14+
const NOT_ROTATED = "matrix(1, 0, 0, 1, 0, 0)";
1315

1416
function chevronTransforms(canvasElement: HTMLElement): string[] {
15-
return [...canvasElement.querySelectorAll(".disclosure-chevron")].map(
16-
(c) => getComputedStyle(c).transform,
17-
);
17+
// Scoped to the accordion root so it still works if a story ever renders more
18+
// than one disclosure accordion on the canvas.
19+
const root = canvasElement.querySelector(".disclosure-sections");
20+
return [
21+
...(root ?? canvasElement).querySelectorAll(".disclosure-chevron"),
22+
].map((c) => getComputedStyle(c).transform);
1823
}
1924

2025
const meta: Meta<typeof ResourceControls> = {
@@ -116,8 +121,8 @@ export const Collapsed: Story = {
116121
play: async ({ canvasElement }) => {
117122
const transforms = chevronTransforms(canvasElement);
118123
expect(transforms).toHaveLength(3);
119-
// `none` (or an identity matrix) = the right-pointing arrow's natural 0°.
120-
for (const t of transforms) expect(t).not.toBe(ROTATED_DOWN);
124+
// Identity matrix = the right-pointing arrow's natural 0° (not rotated).
125+
for (const t of transforms) expect(t).toBe(NOT_ROTATED);
121126
},
122127
};
123128

‎clients/web/src/components/groups/ResourceControls/ResourceControls.test.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ describe("ResourceControls", () => {
245245
).toHaveAttribute("aria-expanded", "false");
246246
});
247247

248+
it("preserves an open-but-empty section's intent when toggling another section", async () => {
249+
// Subscriptions is open-in-intent but empty (excluded from the accordion's
250+
// value). Collapsing a populated section must not drop subscriptions from
251+
// the persisted intent, so it reopens once it has items again (#1462).
252+
const user = userEvent.setup();
253+
const onOpenSectionsChange = vi.fn();
254+
renderWithMantine(
255+
<ResourceControls
256+
{...baseProps}
257+
subscriptions={[]}
258+
openSections={["resources", "templates", "subscriptions"]}
259+
onOpenSectionsChange={onOpenSectionsChange}
260+
/>,
261+
);
262+
await user.click(screen.getByRole("button", { name: /Templates \(1\)/ }));
263+
// Mantine emits ["resources"]; "subscriptions" is merged back in.
264+
expect(onOpenSectionsChange).toHaveBeenCalledWith(
265+
expect.arrayContaining(["resources", "subscriptions"]),
266+
);
267+
expect(onOpenSectionsChange.mock.calls[0][0]).not.toContain("templates");
268+
});
269+
248270
it("filters by resource title when title is set", async () => {
249271
const user = userEvent.setup();
250272
const resourcesWithTitle: Resource[] = [

‎clients/web/src/components/groups/ResourceControls/ResourceControls.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ export function ResourceControls({
120120
const visibleOpenSections = openSections.filter(
121121
(section) => (sectionItemCounts[section] ?? 0) > 0,
122122
);
123+
// Open-in-intent but currently empty (so excluded from the accordion's
124+
// `value`). Mantine derives the next open-array by toggling the clicked
125+
// section against the `value` we hand it, which omits these — so without
126+
// merging them back, toggling any populated section would silently drop an
127+
// empty section's intent and it wouldn't reappear once it has items again.
128+
const intendedButEmptySections = openSections.filter(
129+
(section) => !visibleOpenSections.includes(section),
130+
);
131+
function handleOpenSectionsChange(next: string[]) {
132+
onOpenSectionsChange([...next, ...intendedButEmptySections]);
133+
}
123134

124135
function handleToggleList() {
125136
// Compute the next compact value from what the click will produce so a
@@ -163,9 +174,15 @@ export function ResourceControls({
163174
variant="disclosure"
164175
chevron={<RiArrowRightSLine />}
165176
value={visibleOpenSections}
166-
onChange={onOpenSectionsChange}
177+
onChange={handleOpenSectionsChange}
167178
flex={1}
168179
mih={0}
180+
// Disable Mantine's panel height animation: its Collapse drives the
181+
// open/close via an inline `height` that briefly jumps the panel to its
182+
// full natural height, fighting the flex sizing (the panels are
183+
// flex-distributed and scroll). With it off, flex controls the height
184+
// cleanly. The chevron still rotates smoothly (CSS, in App.css). #1462
185+
transitionDuration={0}
169186
>
170187
<Accordion.Item
171188
value="resources"

‎clients/web/src/components/screens/ResourcesScreen/ResourcesScreen.test.tsx‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,21 @@ describe("ResourcesScreen", () => {
138138
).toBeInTheDocument();
139139
});
140140

141+
it("toggles a section's open state through the lifted openSections handler", async () => {
142+
const user = userEvent.setup();
143+
renderWithMantine(<ControlledResourcesScreen />);
144+
const urisHeader = screen.getByRole("button", { name: /URIs \(2\)/ });
145+
// Open by default (compact=false); clicking routes through ResourcesScreen's
146+
// onOpenSectionsChange → onUiChange and collapses it.
147+
expect(urisHeader).toHaveAttribute("aria-expanded", "true");
148+
await user.click(urisHeader);
149+
expect(urisHeader).toHaveAttribute("aria-expanded", "false");
150+
});
151+
141152
it("renders the template panel when a template is selected", async () => {
142153
const user = userEvent.setup();
143154
renderWithMantine(<ControlledResourcesScreen />);
144-
await user.click(screen.getByText("Templates (1)"));
155+
// Templates section is open by default; select the template to open its form.
145156
await user.click(screen.getByText("files"));
146157
expect(
147158
screen.getByRole("button", { name: "Read Resource" }),
@@ -154,7 +165,6 @@ describe("ResourcesScreen", () => {
154165
renderWithMantine(
155166
<ControlledResourcesScreen onReadResource={onReadResource} />,
156167
);
157-
await user.click(screen.getByText("Templates (1)"));
158168
await user.click(screen.getByText("files"));
159169
await user.type(screen.getByLabelText("path"), "alpha");
160170
await user.click(screen.getByRole("button", { name: "Read Resource" }));
@@ -210,8 +220,7 @@ describe("ResourcesScreen", () => {
210220
onReadResource={onReadResource}
211221
/>,
212222
);
213-
// Open the template form.
214-
await user.click(screen.getByText("Templates (1)"));
223+
// Templates is open by default; select the template to open its form.
215224
await user.click(screen.getByText("files"));
216225
// Submit it — the screen calls onReadResource and remembers the
217226
// template URI for the close handler.
@@ -249,7 +258,6 @@ describe("ResourcesScreen", () => {
249258
const { rerender } = renderWithMantine(
250259
<ControlledResourcesScreen templates={templates} />,
251260
);
252-
await user.click(screen.getByText("Templates (1)"));
253261
await user.click(screen.getByText("Dynamic"));
254262
await user.type(screen.getByLabelText("id"), "asdf");
255263
await user.click(screen.getByRole("button", { name: "Read Resource" }));

0 commit comments

Comments
 (0)