Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions apps/web/src/components/settings/EnvironmentsMap.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, it, vi } from "vite-plus/test";
import { page, userEvent } from "vite-plus/test/browser";

import "~/index.css";
import { render } from "../../../test/render";
import { AppRouteModal } from "../AppRouteModal";
import { EnvironmentsMap } from "./EnvironmentsMap";

vi.mock("../chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null }));

const renderSettingsMap = () =>
render(
<AppRouteModal label="Settings">
<EnvironmentsMap
onAddEnvironment={() => {}}
renderSavedEnvironment={() => null}
renderConnectMachine={() => null}
/>
</AppRouteModal>,
{ router: "/settings/connections" },
);

describe("EnvironmentsMap", () => {
it("clears the selection on Esc before Settings closes", async () => {
await using _screen = await renderSettingsMap();
const settings = page.getByRole("dialog", { name: "Settings" });
const browser = page.getByRole("button", { name: "This browser", exact: true });

await expect.element(settings).toHaveFocus();
await browser.click();
await expect.element(browser).toHaveAttribute("aria-pressed", "true");
await userEvent.keyboard("{Escape}");

await expect.element(browser).toHaveAttribute("aria-pressed", "false");
// The close would render in the same commit that cleared the selection.
expect(settings.element()).toHaveAttribute("data-open");

await userEvent.keyboard("{Escape}");
await expect.element(settings).not.toBeInTheDocument();
});

it("clears the selection on Esc when a click does not focus the machine", async () => {
await using _screen = await renderSettingsMap();
// Safari leaves focus where it was when a button is clicked.
const keepFocus = (event: MouseEvent) => event.preventDefault();
document.addEventListener("mousedown", keepFocus, true);
using _keepFocus = {
[Symbol.dispose]: () => document.removeEventListener("mousedown", keepFocus, true),
};
const settings = page.getByRole("dialog", { name: "Settings" });
const browser = page.getByRole("button", { name: "This browser", exact: true });

await expect.element(settings).toHaveFocus();
await browser.click();
await expect.element(browser).toHaveAttribute("aria-pressed", "true");
await userEvent.keyboard("{Escape}");

await expect.element(browser).toHaveAttribute("aria-pressed", "false");
expect(settings.element()).toHaveAttribute("data-open");
});

it("clears the selection on Esc from the details before Settings closes", async () => {
await using _screen = await renderSettingsMap();
const settings = page.getByRole("dialog", { name: "Settings" });
const browser = page.getByRole("button", { name: "This browser", exact: true });
const details = page.getByRole("group", { name: "Details" });

await expect.element(settings).toHaveFocus();
await browser.click();
await expect.element(browser).toHaveAttribute("aria-pressed", "true");
// Choosing a machine or link inside the details leaves focus on the panel.
(details.element() as HTMLElement).focus();
await userEvent.keyboard("{Escape}");

await expect.element(browser).toHaveAttribute("aria-pressed", "false");
expect(settings.element()).toHaveAttribute("data-open");

await userEvent.keyboard("{Escape}");
await expect.element(settings).not.toBeInTheDocument();
});
});
13 changes: 11 additions & 2 deletions apps/web/src/components/settings/EnvironmentsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,11 @@ function FleetCanvas({
if (event.key === "+" || event.key === "=") zoom(1.25);
else if (event.key === "-") zoom(0.8);
else if (event.key === "0") fit();
else if (event.key === "Escape") onSelect(null);
else return;
else if (event.key === "Escape" && selection !== null) {
// Base UI's dialog closes on a document Esc listener.
event.stopPropagation();
onSelect(null);
} else return;
event.preventDefault();
};

Expand Down Expand Up @@ -1020,6 +1023,12 @@ function FleetCanvas({
onPointerUp={(event) => endDrag(event, false)}
onPointerCancel={(event) => endDrag(event, true)}
onLostPointerCapture={(event) => endDrag(event, true)}
// Safari does not focus a clicked button, so its keys would skip the map.
onClick={(event) =>
(event.target as Element)
.closest<HTMLElement>("button[data-fleet-item]")
?.focus({ preventScroll: true })
}
onKeyDown={onKeyDown}
>
{view === null ? null : (
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/components/settings/EnvironmentsMapDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ export function FleetDetails(props: FleetDetailsProps) {
role="group"
aria-label={gt("Details")}
className="min-h-20 border-t border-border/60 text-xs outline-none"
onKeyDown={(event) => {
if (event.key !== "Escape" || selection === null || event.target !== panel.current) return;
// Base UI's dialog closes on a document Esc listener.
event.stopPropagation();
event.preventDefault();
onSelect(null);
}}
>
{connectProblem ? (
<div className="flex flex-wrap items-center gap-x-3 gap-y-1.5 border-b border-border/60 px-3 py-2 sm:px-4">
Expand Down
Loading