diff --git a/packages/app/e2e/browser/icon-button-focus-ring.spec.ts b/packages/app/e2e/browser/icon-button-focus-ring.spec.ts new file mode 100644 index 000000000..56029381c --- /dev/null +++ b/packages/app/e2e/browser/icon-button-focus-ring.spec.ts @@ -0,0 +1,56 @@ +import { expect, type Locator, type Page } from "@playwright/test"; +import { test } from "../support/fixtures"; +import { gotoWorkspace } from "../support/helpers/launcher"; +import { seedWorkspace } from "../support/helpers/seed-client"; +import { waitForWorkspaceTabsVisible } from "../support/helpers/workspace-tabs"; + +const HEADER_ICON_BUTTONS = ["workspace-header-menu-trigger", "workspace-explorer-toggle"]; + +function visible(page: Page, testId: string): Locator { + return page.getByTestId(testId).filter({ visible: true }).first(); +} + +function matchesFocusVisible(target: Locator): Promise { + return target.evaluate((el) => el.matches(":focus-visible")); +} + +async function expectKeyboardFocusRing(page: Page, testId: string): Promise { + const button = visible(page, testId); + // Chromium matches :focus-visible on a programmatic focus that follows keyboard input. + await page.keyboard.press("Shift"); + await button.focus(); + await expect(button).toBeFocused(); + expect(await matchesFocusVisible(button)).toBe(true); + await expect(button).toHaveCSS("outline-style", "solid"); + await expect(button).toHaveCSS("outline-width", "2px"); + // Header rows clip overflow; an outset ring would lose its top and bottom edges. + await expect(button).toHaveCSS("outline-offset", "-2px"); +} + +async function expectNoPointerFocusRing(page: Page, testId: string): Promise { + const button = visible(page, testId); + await button.blur(); + // Toggle twice so the layout ends where it started. + await button.click(); + await button.click(); + await expect(button).toBeFocused(); + expect(await matchesFocusVisible(button)).toBe(false); + await expect(button).toHaveCSS("outline-style", "none"); +} + +test.describe("Icon button focus ring", () => { + test("header icon buttons show the focus ring for keyboard focus only", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 900 }); + const workspace = await seedWorkspace({ repoPrefix: "icon-button-focus-" }); + try { + await gotoWorkspace(page, workspace.workspaceId); + await waitForWorkspaceTabsVisible(page); + for (const testId of HEADER_ICON_BUTTONS) { + await expectKeyboardFocusRing(page, testId); + } + await expectNoPointerFocusRing(page, "workspace-explorer-toggle"); + } finally { + await workspace.cleanup(); + } + }); +}); diff --git a/packages/app/src/components/ui/icon-button-chrome.ts b/packages/app/src/components/ui/icon-button-chrome.ts index da738df59..8cf68e892 100644 --- a/packages/app/src/components/ui/icon-button-chrome.ts +++ b/packages/app/src/components/ui/icon-button-chrome.ts @@ -126,6 +126,10 @@ export function smallIconButtonChromeFrameSize(compact = false): number { return ICON_BUTTON_SIZES.small[controlSizingForLayout(compact)]; } +// Icon buttons sit in header and toolbar rows that clip overflow, so the keyboard focus ring +// from public/index.html is drawn inside the frame instead of around it. +const FOCUS_RING_INSET = -2; + const styles = StyleSheet.create((theme) => ({ large: { width: bySizing(ICON_BUTTON_SIZES.large), @@ -135,8 +139,7 @@ const styles = StyleSheet.create((theme) => ({ alignItems: "center", justifyContent: "center", flexShrink: 0, - outlineWidth: 0, - outlineColor: "transparent", + outlineOffset: FOCUS_RING_INSET, }, small: { width: ICON_BUTTON_SIZES.small.pointer, @@ -146,8 +149,7 @@ const styles = StyleSheet.create((theme) => ({ alignItems: "center", justifyContent: "center", flexShrink: 0, - outlineWidth: 0, - outlineColor: "transparent", + outlineOffset: FOCUS_RING_INSET, }, smallTouch: { width: ICON_BUTTON_SIZES.small.touch, @@ -157,8 +159,7 @@ const styles = StyleSheet.create((theme) => ({ alignItems: "center", justifyContent: "center", flexShrink: 0, - outlineWidth: 0, - outlineColor: "transparent", + outlineOffset: FOCUS_RING_INSET, }, round: { borderRadius: theme.borderRadius.full,