Open History to see the last commit
1import type { ReactNode } from "react";
2import { createPortal } from "react-dom";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5
6import stylesheet from "~/index.css?inline";
7import { act, render, type Screen, stubGlobal } from "../../../test/render";
8import { PreviewPanelShell } from "./PreviewPanelShell";
9
10function Workspace(props: { open: boolean; present?: boolean; children?: ReactNode }) {
11 return (
12 <div>
13 <div data-workspace-area="composer">
14 <input aria-label="Message New thread" />
15 </div>
16 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
17 {(props.present ?? true) ? (
18 <PreviewPanelShell mode="inline" open={props.open}>
19 {props.children ?? <div role="region" aria-label="Open a surface" tabIndex={-1} />}
20 </PreviewPanelShell>
21 ) : null}
22 </div>
23 );
24}
25
26// Mirrors rightPanel.toggle from the composer: the panel opens and the shortcut focuses its content.
27async function openPanelFromComposer(screen: Screen) {
28 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
29 await screen.rerender(<Workspace open />);
30 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
31}
32
33describe("PreviewPanelShell focus", () => {
34 it("returns focus to the composer when a panel opened from it collapses", async () => {
35 await using screen = await render(<Workspace open={false} />);
36 await openPanelFromComposer(screen);
37
38 await screen.rerender(<Workspace open={false} />);
39
40 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
41 });
42
43 it("returns focus to the composer when a click on the toggle collapses a panel opened from it", async () => {
44 await using screen = await render(<Workspace open={false} />);
45 await openPanelFromComposer(screen);
46 // Pressing the toggle moves focus onto it before its click closes the panel.
47 await userEvent.click(screen.getByRole("button", { name: "Toggle right panel" }));
48
49 await screen.rerender(<Workspace open={false} />);
50
51 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
52 });
53
54 it("returns focus to the composer when a click on the toggle unmounts a panel and moves the toggle", async () => {
55 // The app's toggle moves when the panel unmounts, which drops its focus to the body.
56 const toggle = (
57 <button key="toggle" type="button" aria-label="Toggle right panel" data-right-panel-toggle />
58 );
59 const layout = (present: boolean) => (
60 <div>
61 <div data-workspace-area="composer">
62 <input aria-label="Message New thread" />
63 </div>
64 <div>{present ? [toggle, <div key="spacer" />] : [<div key="spacer" />, toggle]}</div>
65 <div>
66 {present ? (
67 <PreviewPanelShell mode="inline" open>
68 <div role="region" aria-label="Open a surface" tabIndex={-1} />
69 </PreviewPanelShell>
70 ) : null}
71 </div>
72 </div>
73 );
74 await using screen = await render(layout(false));
75 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
76 await screen.rerender(layout(true));
77 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
78 await userEvent.click(screen.getByRole("button", { name: "Toggle right panel" }));
79
80 await screen.rerender(layout(false));
81
82 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
83 });
84
85 it("keeps focus on the toggle when the keyboard collapses a panel from it", async () => {
86 await using screen = await render(<Workspace open={false} />);
87 await openPanelFromComposer(screen);
88 await userEvent.keyboard("{Shift>}{Tab}{/Shift}");
89 const toggle = screen.getByRole("button", { name: "Toggle right panel" });
90 await expect.element(toggle).toHaveFocus();
91
92 await screen.rerender(<Workspace open={false} />);
93
94 await expect.element(toggle).toHaveFocus();
95 });
96
97 it("returns focus to the composer when a panel opened from it unmounts", async () => {
98 await using screen = await render(<Workspace open={false} />);
99 await openPanelFromComposer(screen);
100
101 await screen.rerender(<Workspace open={false} present={false} />);
102
103 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
104 });
105
106 it("returns focus to the composer when the panel's content takes focus as it mounts", async () => {
107 // A terminal surface focuses itself from its ref callback, before the shell's own ref attaches.
108 const focusOnMount = (element: HTMLElement | null) => element?.focus();
109 const panel = (present: boolean) => (
110 <Workspace open present={present}>
111 <div role="region" aria-label="Terminal" tabIndex={-1} ref={focusOnMount} />
112 </Workspace>
113 );
114 await using screen = await render(panel(false));
115 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
116 await screen.rerender(panel(true));
117 await expect.element(screen.getByRole("region", { name: "Terminal" })).toHaveFocus();
118
119 await screen.rerender(panel(false));
120
121 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
122 });
123
124 it("keeps the composer as the origin after focus visits a menu portalled from the panel", async () => {
125 const panel = (open: boolean) => (
126 <Workspace open={open}>
127 <div role="region" aria-label="Open a surface" tabIndex={-1} />
128 {createPortal(<button type="button">Add panel surface</button>, document.body)}
129 </Workspace>
130 );
131 await using screen = await render(panel(false));
132 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
133 await screen.rerender(panel(true));
134 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
135 await act(() => page.getByRole("button", { name: "Add panel surface" }).element().focus());
136 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
137
138 await screen.rerender(panel(false));
139
140 await expect.element(screen.getByRole("textbox", { name: "Message New thread" })).toHaveFocus();
141 });
142
143 it("falls back to the toggle without scrolling when the origin has scrolled out of view", async () => {
144 const layout = (open: boolean) => (
145 <div>
146 <div
147 data-testid="timeline"
148 data-workspace-area="conversation"
149 style={{ height: 100, overflowY: "auto" }}
150 >
151 <button type="button">Copy message</button>
152 <div style={{ height: 2000 }} />
153 </div>
154 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
155 <PreviewPanelShell mode="inline" open={open}>
156 <div role="region" aria-label="Open a surface" tabIndex={-1} />
157 </PreviewPanelShell>
158 </div>
159 );
160 await using screen = await render(layout(false));
161 await userEvent.click(screen.getByRole("button", { name: "Copy message" }));
162 await screen.rerender(layout(true));
163 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
164 const timeline = screen.getByTestId("timeline").element();
165 timeline.scrollTop = 1500;
166
167 await screen.rerender(layout(false));
168
169 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
170 expect(timeline.scrollTop).toBe(1500);
171 });
172
173 it("falls back to the toggle when the origin's drawer is closing", async () => {
174 // A closing drawer stays mounted for its exit animation but is no longer a workspace area.
175 const layout = (open: boolean, drawerOpen: boolean) => (
176 <div>
177 <div data-workspace-area={drawerOpen ? "terminal" : undefined}>
178 <input aria-label="Terminal input" />
179 </div>
180 <button type="button" aria-label="Toggle right panel" data-right-panel-toggle />
181 <PreviewPanelShell mode="inline" open={open}>
182 <div role="region" aria-label="Open a surface" tabIndex={-1} />
183 </PreviewPanelShell>
184 </div>
185 );
186 await using screen = await render(layout(false, true));
187 await userEvent.click(screen.getByRole("textbox", { name: "Terminal input" }));
188 await screen.rerender(layout(true, true));
189 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
190 await screen.rerender(layout(true, false));
191
192 await screen.rerender(layout(false, false));
193
194 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
195 });
196
197 it("does not return focus to the origin of an earlier open", async () => {
198 await using screen = await render(<Workspace open={false} />);
199 await openPanelFromComposer(screen);
200 await screen.rerender(<Workspace open={false} />);
201 const composer = screen.getByRole("textbox", { name: "Message New thread" });
202 await expect.element(composer).toHaveFocus();
203 await act(() => composer.element().blur());
204 await screen.rerender(<Workspace open />);
205 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
206
207 await screen.rerender(<Workspace open={false} />);
208
209 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
210 });
211
212 it("returns focus to the toggle when a panel opened from it collapses", async () => {
213 await using screen = await render(<Workspace open={false} />);
214 await userEvent.click(screen.getByRole("textbox", { name: "Message New thread" }));
215 await userEvent.click(screen.getByRole("button", { name: "Toggle right panel" }));
216 await screen.rerender(<Workspace open />);
217 await act(() => screen.getByRole("region", { name: "Open a surface" }).element().focus());
218
219 await screen.rerender(<Workspace open={false} />);
220
221 await expect.element(screen.getByRole("button", { name: "Toggle right panel" })).toHaveFocus();
222 });
223});
224
225describe("PreviewPanelShell resize", () => {
226 it("resizes the clipped content without changing the width property on its descendants", async () => {
227 const storageKey = "test:preview-panel-live-width";
228 const styles = document.createElement("style");
229 styles.textContent = stylesheet;
230 document.head.append(styles);
231 const panel = (open: boolean) => (
232 <div style={{ width: 1000 }}>
233 <PreviewPanelShell
234 mode="inline"
235 defaultWidth={400}
236 widthStorageKey={storageKey}
237 open={open}
238 >
239 <div role="region" aria-label="Panel content" />
240 </PreviewPanelShell>
241 </div>
242 );
243 try {
244 using _width = stubGlobal("innerWidth", 1000);
245 await using screen = await render(panel(true));
246 const handle = screen.getByRole("separator").element();
247 const host = handle.parentElement!;
248 const content = screen.getByRole("region", { name: "Panel content" }).element();
249 const inheritedWidth = () => getComputedStyle(content).getPropertyValue("--resizable-width");
250 const before = inheritedWidth();
251 const pointer = { bubbles: true, pointerId: 1, button: 0 };
252 // Synthetic events have no active browser pointer to capture.
253 using _capture = vi.spyOn(handle, "setPointerCapture").mockImplementation(() => {});
254 await act(() => {
255 handle.dispatchEvent(new PointerEvent("pointerdown", { ...pointer, clientX: 500 }));
256 handle.dispatchEvent(new PointerEvent("pointermove", { ...pointer, clientX: 460 }));
257 });
258
259 await vi.waitFor(() => {
260 expect(host.getBoundingClientRect().width).toBe(440);
261 expect(content.getBoundingClientRect().width).toBe(439);
262 });
263 expect(inheritedWidth()).toBe(before);
264
265 await act(() => {
266 handle.dispatchEvent(new PointerEvent("pointerup", { ...pointer, clientX: 460 }));
267 });
268 await screen.rerender(panel(false));
269 expect(content.getBoundingClientRect().width).toBe(439);
270 } finally {
271 styles.remove();
272 localStorage.removeItem(storageKey);
273 }
274 });
275});