|
2 | 2 |
|
3 | 3 | import { Dialog } from "@base-ui/react/dialog"; |
4 | 4 | import { act } from "react"; |
| 5 | +import { createPortal } from "react-dom"; |
5 | 6 | import { createRoot, type Root } from "react-dom/client"; |
6 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
7 | 8 |
|
@@ -48,6 +49,16 @@ async function renderPanel( |
48 | 49 | <PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}> |
49 | 50 | <input aria-label="Preview URL" /> |
50 | 51 | <button type="button">Reload preview</button> |
| 52 | + {/* Like the add-surface trigger, removed as soon as the panel closes. */} |
| 53 | + {open !== false ? ( |
| 54 | + <button type="button" aria-label="Add panel surface"> |
| 55 | + + |
| 56 | + </button> |
| 57 | + ) : null} |
| 58 | + {/* Like the add-surface menu, portaled outside the panel. */} |
| 59 | + {open !== false |
| 60 | + ? createPortal(<button type="button">Add terminal</button>, document.body) |
| 61 | + : null} |
51 | 62 | </PreviewPanelShell> |
52 | 63 | ) : null} |
53 | 64 | <button type="button" data-testid="outside"> |
@@ -144,6 +155,41 @@ describe("right panel keyboard access", () => { |
144 | 155 | ); |
145 | 156 | }); |
146 | 157 |
|
| 158 | + it.each([ |
| 159 | + [ |
| 160 | + "control", |
| 161 | + () => document.querySelector<HTMLButtonElement>('[aria-label="Add panel surface"]'), |
| 162 | + ], |
| 163 | + [ |
| 164 | + "portaled menu item", |
| 165 | + () => [...document.querySelectorAll("button")].find((b) => b.textContent === "Add terminal"), |
| 166 | + ], |
| 167 | + ])("returns focus when the close removes a focused %s before the animation", async (_, find) => { |
| 168 | + await renderPanel(true); |
| 169 | + await act(async () => find()!.focus()); |
| 170 | + expect(document.activeElement).toBe(find()); |
| 171 | + await renderPanel(false); |
| 172 | + expect(document.activeElement).toBe( |
| 173 | + document.querySelector('[aria-label="Toggle right panel"]'), |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + it.each([ |
| 178 | + ["after the closing animation", true], |
| 179 | + ["without an animation", false], |
| 180 | + ])("keeps focus on a toggle that closed the panel and relocates %s", async (_, animated) => { |
| 181 | + await renderPanel(true); |
| 182 | + const closingToggle = document.querySelector<HTMLButtonElement>( |
| 183 | + '[aria-label="Toggle right panel"]', |
| 184 | + )!; |
| 185 | + await act(async () => closingToggle.focus()); |
| 186 | + if (animated) await renderPanel(false); |
| 187 | + await renderPanel(false, "inline", false); |
| 188 | + const settledToggle = document.querySelector('[aria-label="Toggle right panel"]'); |
| 189 | + expect(settledToggle).not.toBe(closingToggle); |
| 190 | + expect(document.activeElement).toBe(settledToggle); |
| 191 | + }); |
| 192 | + |
147 | 193 | it("preserves focus moved outside while the closing animation settles", async () => { |
148 | 194 | await renderPanel(true); |
149 | 195 | await act(async () => { |
|
0 commit comments