Skip to content

Commit e505dcc

Browse files
feat(studio): dock dividers and tabs work from the keyboard and side columns fit narrow windows (#4186)
* feat(studio): dock sashes and tab strips work from the keyboard and side columns yield on narrow windows Every dockview sash is a focusable separator (role, aria-orientation, aria-label); arrow keys along the split axis resize the neighbouring groups by 16px (Shift 64), clamped by the panels' minimum sizes, by replaying a pointer drag so dockview's own clamping applies. Tab arrows now activate the tab they land on and wrap; Home/End jump to the first and last tab. Narrow-window rule: the preview keeps its 360x200 floor first; side groups' minimum width is clamp((viewport - 360) / 2, 120, 200) and they shrink to their defaults when it drops below 200. Below 600px the dock overflows and clips instead of squeezing the preview. Measured at 560px: before, the inspector column sat fully off-screen at x 560-760; after, columns are 120/360/120 and reachable. * refactor(studio): simplify side group zone lookup in dock layout * fix(studio): dock side-fit spares the preview group, follows panel moves and is covered through the shell A side panel tabbed into the preview group no longer lowers the preview minimum width, moving a panel re-applies the side minimums, a project switch disposes the previous observer, and the dock shell tests now cover the sash and resize wiring. * fix(studio): pin the preview floor on every group that holds the preview A group that held the preview kept a stale side-column minimum after a drag, so the preview could shrink below its floor. Every group with a preview tab is now constrained to the preview minimum explicitly. * fix(studio): every dock group gets a width minimum, shortcuts keep their meaning on tabs and sashes The side-fit now rewrites the minimum of every group each time, so a group that stopped holding the preview or gained the timeline never keeps a stale value. Alt, Ctrl and Meta arrows are left to the browser. The shell test now proves the resize observer watches the dock. * test(studio): the dock shell tests cover panel drags and sash labels * refactor(studio): share the dock test harness and drop an unused export The sash decorator is internal, the bare dockview setup and the persisted-group lookup used by several dock tests live in one place each. * refactor(studio): shorten the side-minimum comment to one line
1 parent c610d84 commit e505dcc

8 files changed

Lines changed: 570 additions & 24 deletions

File tree

‎packages/studio/src/components/dock/Dock.test.tsx‎

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
3+
import React, { act, type ComponentProps } from "react";
4+
import type { DockviewApi } from "dockview-react";
45
import { createRoot, type Root } from "react-dom/client";
56
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
7+
import * as dockLayout from "./dockLayout";
68
import { Dock } from "./Dock";
79
import { parseDockLayout } from "./dockLayoutSchema";
810
import { useDockLayoutStore } from "./dockLayoutStore";
@@ -11,13 +13,45 @@ import { readStudioUiPreferences } from "../../utils/studioUiPreferences";
1113

1214
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1315

16+
let dockApi: DockviewApi | null = null;
17+
vi.mock("dockview-react", async (importOriginal) => {
18+
const actual = await importOriginal<typeof import("dockview-react")>();
19+
return {
20+
...actual,
21+
DockviewReact: (props: ComponentProps<typeof actual.DockviewReact>) =>
22+
React.createElement(actual.DockviewReact, {
23+
...props,
24+
onReady: (event) => {
25+
dockApi = event.api;
26+
props.onReady(event);
27+
},
28+
}),
29+
};
30+
});
31+
32+
const liveObservers = new Set<{ callback: () => void; target?: Element }>();
1433
class ResizeObserverStub {
15-
observe() {}
34+
private readonly entry: { callback: () => void; target?: Element };
35+
constructor(callback: () => void) {
36+
this.entry = { callback };
37+
}
38+
observe(target: Element) {
39+
this.entry.target = target;
40+
liveObservers.add(this.entry);
41+
}
1642
unobserve() {}
17-
disconnect() {}
43+
disconnect() {
44+
liveObservers.delete(this.entry);
45+
}
1846
}
1947
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverStub;
2048

49+
vi.mock("./dockLayout", async (importOriginal) => {
50+
const actual = await importOriginal<typeof import("./dockLayout")>();
51+
return { ...actual, applySideMinimums: vi.fn(actual.applySideMinimums) };
52+
});
53+
const applySideMinimums = vi.mocked(dockLayout.applySideMinimums);
54+
2155
let root: Root | null = null;
2256

2357
function mount(projectId: string | null) {
@@ -38,6 +72,16 @@ function mount(projectId: string | null) {
3872
return host;
3973
}
4074

75+
/** The persisted views of the group holding `id`, after the debounced write lands. */
76+
function persistedGroupOf(id: string): string | undefined {
77+
act(() => {
78+
vi.advanceTimersByTime(1000);
79+
});
80+
const grid = JSON.stringify(readStudioUiPreferences(undefined, "p1").dockLayout?.grid);
81+
const groups = [...grid.matchAll(/"views":\[([^\]]*)\]/g)].map((m) => m[1]);
82+
return groups.find((views) => views.includes(`"${id}"`));
83+
}
84+
4185
beforeEach(() => {
4286
localStorage.clear();
4387
vi.useFakeTimers();
@@ -102,13 +146,7 @@ describe("Dock on React 19", () => {
102146
mount("p1");
103147
act(() => useDockLayoutStore.getState().closePanel("compositions"));
104148
act(() => useDockLayoutStore.getState().togglePanel("compositions"));
105-
act(() => {
106-
vi.advanceTimersByTime(1000);
107-
});
108-
const grid = JSON.stringify(readStudioUiPreferences(undefined, "p1").dockLayout?.grid);
109-
const groups = [...grid.matchAll(/"views":\[([^\]]*)\]/g)].map((m) => m[1]);
110-
const home = groups.find((views) => views.includes('"compositions"'));
111-
expect(home).toContain('"assets"');
149+
expect(persistedGroupOf("compositions")).toContain('"assets"');
112150
});
113151

114152
it("reopens a side panel next to the preview when its whole column was closed", () => {
@@ -125,12 +163,7 @@ describe("Dock on React 19", () => {
125163
mount("p1");
126164
act(() => useDockLayoutStore.getState().closePanel("timeline"));
127165
act(() => useDockLayoutStore.getState().togglePanel("timeline"));
128-
act(() => {
129-
vi.advanceTimersByTime(1000);
130-
});
131-
const grid = JSON.stringify(readStudioUiPreferences(undefined, "p1").dockLayout?.grid);
132-
const groups = [...grid.matchAll(/"views":\[([^\]]*)\]/g)].map((m) => m[1]);
133-
expect(groups.find((views) => views.includes('"timeline"'))).not.toContain('"preview"');
166+
expect(persistedGroupOf("timeline")).not.toContain('"preview"');
134167
});
135168

136169
it("restores the stored layout on the next mount instead of rebuilding the default", () => {
@@ -157,6 +190,41 @@ describe("Dock on React 19", () => {
157190
});
158191
});
159192

193+
describe("Dock wiring", () => {
194+
it("re-applies the side minimums when a panel is dragged to another group", () => {
195+
mount(null);
196+
applySideMinimums.mockClear();
197+
const preview = dockApi?.getPanel("preview");
198+
const design = dockApi?.getPanel("design");
199+
if (!preview || !design) throw new Error("default layout is missing panels");
200+
act(() => design.api.moveTo({ group: preview.group }));
201+
expect(applySideMinimums).toHaveBeenCalled();
202+
});
203+
204+
it("makes the sashes keyboard-focusable separators", () => {
205+
const host = mount(null);
206+
const sashes = host.querySelectorAll<HTMLElement>('.dv-sash[role="separator"]');
207+
expect(sashes.length).toBeGreaterThan(0);
208+
for (const sash of sashes) expect(sash.tabIndex).toBe(0);
209+
});
210+
211+
it("re-fits the side columns to the window width when it resizes", () => {
212+
mount(null);
213+
const innerWidth = window.innerWidth;
214+
Object.defineProperty(window, "innerWidth", { value: 560, configurable: true });
215+
try {
216+
applySideMinimums.mockClear();
217+
const dockObservers = [...liveObservers].filter(({ target }) =>
218+
target?.classList.contains("hf-dock"),
219+
);
220+
act(() => dockObservers.forEach(({ callback }) => callback()));
221+
expect(applySideMinimums).toHaveBeenCalledWith(expect.anything(), 560);
222+
} finally {
223+
Object.defineProperty(window, "innerWidth", { value: innerWidth, configurable: true });
224+
}
225+
});
226+
});
227+
160228
describe("parseDockLayout", () => {
161229
it("rejects shapes that are not a dock layout", () => {
162230
expect(parseDockLayout(null)).toBeNull();

‎packages/studio/src/components/dock/Dock.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
type IDockviewPanelProps,
1616
} from "dockview-react";
1717
import { readStudioUiPreferences, writeStudioUiPreferences } from "../../utils/studioUiPreferences";
18-
import { addRegisteredPanel, buildEditLayout } from "./dockLayout";
18+
import { installDockAccessibility } from "./dockAccessibility";
19+
import { addRegisteredPanel, applySideMinimums, buildEditLayout } from "./dockLayout";
1920
import { DOCK_PANEL_COMPONENT } from "./dockLayoutSchema";
2021
import { useDockLayoutStore, type DockController, type DockSnapshot } from "./dockLayoutStore";
2122
import {
@@ -135,7 +136,14 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
135136

136137
const onReady = useCallback(
137138
({ api }: DockviewReadyEvent) => {
139+
disposeRef.current();
138140
restoreOrBuild(api, projectId);
141+
applySideMinimums(api);
142+
const root = api.groups[0]?.element.closest<HTMLElement>(".hf-dock");
143+
const disposeAccessibility = root ? installDockAccessibility(api, root) : () => {};
144+
// The dock spans the window (buildEditLayout sizes against it too); its own box lags a resize.
145+
const resizeObserver = new ResizeObserver(() => applySideMinimums(api, window.innerWidth));
146+
if (root) resizeObserver.observe(root);
139147
const store = useDockLayoutStore.getState();
140148
store.attach(createController(api));
141149
store.sync(snapshot(api));
@@ -156,14 +164,23 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
156164
persist();
157165
};
158166
const subscriptions = [
159-
api.onDidAddPanel(onDockChange),
167+
api.onDidAddPanel(() => {
168+
applySideMinimums(api);
169+
onDockChange();
170+
}),
171+
api.onDidMovePanel(() => {
172+
applySideMinimums(api);
173+
onDockChange();
174+
}),
160175
api.onDidRemovePanel(onDockChange),
161176
api.onDidActivePanelChange(onDockChange),
162177
api.onDidLayoutChange(onDockChange),
163178
];
164179
disposeRef.current = () => {
165180
clearTimeout(timer);
166181
for (const subscription of subscriptions) subscription.dispose();
182+
disposeAccessibility();
183+
resizeObserver.disconnect();
167184
useDockLayoutStore.getState().detach();
168185
};
169186
},

‎packages/studio/src/components/dock/dock.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@
2121
--dv-tab-font-size: 11px;
2222
--dv-tabs-and-actions-container-font-size: 11px;
2323
}
24+
25+
.hf-dock .dv-sash:focus-visible {
26+
background-color: var(--dv-active-sash-color);
27+
outline: none;
28+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// @vitest-environment happy-dom
2+
3+
import type { DockviewApi } from "dockview-react";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import { mountBareDockview } from "./dockTestHarness";
6+
import { installDockAccessibility } from "./dockAccessibility";
7+
import { applySideMinimums, buildEditLayout } from "./dockLayout";
8+
9+
class ResizeObserverStub {
10+
observe() {}
11+
unobserve() {}
12+
disconnect() {}
13+
}
14+
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverStub;
15+
16+
let host: HTMLElement;
17+
let api: DockviewApi;
18+
let dispose: () => void;
19+
20+
beforeEach(() => {
21+
({ host, api } = mountBareDockview());
22+
api.layout(1200, 700);
23+
buildEditLayout(api, 1200);
24+
applySideMinimums(api, 1200);
25+
dispose = installDockAccessibility(api, host);
26+
});
27+
28+
afterEach(() => {
29+
dispose();
30+
api.dispose();
31+
document.body.innerHTML = "";
32+
});
33+
34+
const sashes = () => [...host.querySelectorAll<HTMLElement>(".dv-sash")];
35+
const columnSash = (index: number) =>
36+
sashes().filter((sash) => sash.getAttribute("aria-orientation") === "vertical")[
37+
index
38+
] as HTMLElement;
39+
const widthOf = (panel: string) => api.getPanel(panel)?.group.width;
40+
const press = (target: Element, key: string, init: KeyboardEventInit = {}) =>
41+
target.dispatchEvent(
42+
new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...init }),
43+
);
44+
45+
describe("dock sashes", () => {
46+
it("are focusable separators with an orientation and a label", () => {
47+
expect(sashes()).toHaveLength(3);
48+
for (const sash of sashes()) {
49+
expect(sash.getAttribute("role")).toBe("separator");
50+
expect(sash.tabIndex).toBe(0);
51+
const label = sash.getAttribute("aria-label");
52+
const orientation = sash.getAttribute("aria-orientation");
53+
expect(label).toBe(orientation === "vertical" ? "Resize columns" : "Resize rows");
54+
}
55+
expect(
56+
sashes()
57+
.map((sash) => sash.getAttribute("aria-orientation"))
58+
.sort(),
59+
).toEqual(["horizontal", "vertical", "vertical"]);
60+
});
61+
62+
it("are decorated again after a layout restore rebuilds them", () => {
63+
api.fromJSON(api.toJSON());
64+
expect(sashes()).toHaveLength(3);
65+
for (const sash of sashes()) expect(sash.getAttribute("role")).toBe("separator");
66+
});
67+
68+
it("resize the neighbouring groups by 16px per arrow press, 64px with Shift", () => {
69+
const left = widthOf("compositions") as number;
70+
const preview = widthOf("preview") as number;
71+
const sash = columnSash(0);
72+
press(sash, "ArrowRight");
73+
expect(widthOf("compositions")).toBe(left + 16);
74+
expect(widthOf("preview")).toBe(preview - 16);
75+
press(sash, "ArrowRight", { shiftKey: true });
76+
expect(widthOf("compositions")).toBe(left + 80);
77+
press(sash, "ArrowLeft");
78+
expect(widthOf("compositions")).toBe(left + 64);
79+
});
80+
81+
it("stop at the panels' minimum sizes", () => {
82+
const sash = columnSash(0);
83+
for (let i = 0; i < 20; i++) press(sash, "ArrowLeft", { shiftKey: true });
84+
expect(widthOf("compositions")).toBe(200);
85+
for (let i = 0; i < 40; i++) press(sash, "ArrowRight", { shiftKey: true });
86+
expect(widthOf("preview")).toBe(360);
87+
});
88+
89+
it("ignore the arrow keys of the other axis and keys pressed anywhere else", () => {
90+
const left = widthOf("compositions");
91+
press(columnSash(0), "ArrowDown");
92+
press(host, "ArrowRight");
93+
press(document.body, "ArrowRight");
94+
expect(widthOf("compositions")).toBe(left);
95+
});
96+
97+
it("resize rows with the vertical arrows", () => {
98+
const timeline = api.getPanel("timeline")?.group.height as number;
99+
const rows = sashes().find((sash) => sash.getAttribute("aria-orientation") === "horizontal");
100+
press(rows as HTMLElement, "ArrowUp");
101+
expect(api.getPanel("timeline")?.group.height).toBe(timeline + 16);
102+
});
103+
});
104+
105+
describe("dock tab strips", () => {
106+
const tabsOf = (panel: string) => [
107+
...(api.getPanel(panel)?.group.element.querySelectorAll<HTMLElement>('[role="tab"]') ?? []),
108+
];
109+
110+
it("keep dockview's tablist, tab and selected roles with one tab stop", () => {
111+
const tabs = tabsOf("compositions");
112+
expect(tabs).toHaveLength(4);
113+
expect(tabs[0]?.closest('[role="tablist"]')).not.toBeNull();
114+
expect(tabs.map((tab) => tab.getAttribute("aria-selected"))).toEqual([
115+
"true",
116+
"false",
117+
"false",
118+
"false",
119+
]);
120+
expect(tabs.map((tab) => tab.tabIndex)).toEqual([0, -1, -1, -1]);
121+
});
122+
123+
it("leave browser shortcuts alone when Alt, Ctrl or Meta is held", () => {
124+
const sash = columnSash(0);
125+
const left = widthOf("compositions");
126+
const tab = tabsOf("compositions")[0] as HTMLElement;
127+
const active = () => api.getPanel("compositions")?.group.activePanel?.id;
128+
for (const init of [{ altKey: true }, { ctrlKey: true }, { metaKey: true }]) {
129+
const onSash = new KeyboardEvent("keydown", {
130+
key: "ArrowRight",
131+
cancelable: true,
132+
bubbles: true,
133+
...init,
134+
});
135+
sash.dispatchEvent(onSash);
136+
press(tab, "ArrowRight", init);
137+
expect(onSash.defaultPrevented).toBe(false);
138+
expect(active()).toBe("compositions");
139+
}
140+
expect(widthOf("compositions")).toBe(left);
141+
});
142+
143+
it("move focus and activate the neighbouring tab, wrapping at both ends", () => {
144+
const tabs = tabsOf("compositions");
145+
const active = () => api.getPanel("compositions")?.group.activePanel?.id;
146+
(tabs[0] as HTMLElement).focus();
147+
press(tabs[0] as HTMLElement, "ArrowRight");
148+
expect(active()).toBe("assets");
149+
expect(document.activeElement).toBe(tabs[1]);
150+
press(tabs[1] as HTMLElement, "ArrowLeft");
151+
press(tabs[0] as HTMLElement, "ArrowLeft");
152+
expect(active()).toBe("catalog");
153+
expect(document.activeElement).toBe(tabs[3]);
154+
press(tabs[3] as HTMLElement, "ArrowRight");
155+
expect(active()).toBe("compositions");
156+
});
157+
158+
it("jump to the first and last tab with Home and End", () => {
159+
const tabs = tabsOf("design");
160+
press(tabs[0] as HTMLElement, "End");
161+
expect(api.getPanel("design")?.group.activePanel?.id).toBe("variables");
162+
expect(document.activeElement).toBe(tabs[3]);
163+
press(tabs[3] as HTMLElement, "Home");
164+
expect(api.getPanel("design")?.group.activePanel?.id).toBe("design");
165+
});
166+
});

0 commit comments

Comments
 (0)