diff --git a/packages/scout-for-lol/packages/design-system/README.md b/packages/scout-for-lol/packages/design-system/README.md index 0ba1e477cd..b0fa6d3ea2 100644 --- a/packages/scout-for-lol/packages/design-system/README.md +++ b/packages/scout-for-lol/packages/design-system/README.md @@ -128,7 +128,12 @@ opens a layer after 350 ms, focus opens it at once, a layer stays open while the pointer or focus is in it or in a layer it opened (150 ms grace), and a `RichTooltip` inside another's `content` opens beside its parent. Escape closes the top layer only. Under 640 px a tap opens a bottom sheet instead, and a -tapped part replaces the sheet's content behind a back control. The timing +tapped part replaces the sheet's content behind a back control. The sheet is +also keyboard-operable: Enter or Space on a trigger opens it (a page-rendered +element with a `tabindex` gets no click from those keys, so the sheet handles +them), focus moves into it, and Escape closes it and returns focus to the +trigger. A page-rendered link stays a popover on a phone, so focus shows its +layer and Enter follows the link. The timing rules live in `hover-layer-controller.ts` and are tested with fake timers. The shell never fetches. diff --git a/packages/scout-for-lol/packages/design-system/e2e/rich-tooltip.spec.ts b/packages/scout-for-lol/packages/design-system/e2e/rich-tooltip.spec.ts index 6a2bcc60fc..24553f1cda 100644 --- a/packages/scout-for-lol/packages/design-system/e2e/rich-tooltip.spec.ts +++ b/packages/scout-for-lol/packages/design-system/e2e/rich-tooltip.spec.ts @@ -255,6 +255,91 @@ test.describe("phone", () => { expect(page.url()).not.toContain("#ahri"); }); + test("a keyboard opens the sheet from a page-rendered trigger, focus moves in and Escape returns it", async ({ + page, + }) => { + await page.goto(delegated); + await page.waitForFunction( + () => document.querySelector("astro-island[ssr]") === null, + ); + const item = page.locator('[data-game-fact="item/3118"]'); + const sheet = page.getByRole("dialog", { name: "Malignance", exact: true }); + // The island makes marked elements focusable once it has hydrated. + await expect(item).toHaveAttribute("tabindex", "0"); + + await page.keyboard.press("Tab"); + await expect(item).toBeFocused(); + // The tooltip listens once it has mounted on the focused element. + await expect(item).toHaveAttribute("aria-haspopup", "dialog"); + // Focus alone shows nothing on a phone. + await expect(sheet).toHaveCount(0); + + for (const key of ["Enter", "Space"]) { + await page.keyboard.press(key); + await expect(sheet).toBeVisible(); + await expect(item).toHaveAttribute("aria-expanded", "true"); + await expect(sheet.locator(":focus")).toHaveCount(1); + + await page.keyboard.press("Escape"); + await expect(sheet).toHaveCount(0); + await expect(item).toBeFocused(); + } + }); + + test("a link takes the layer on focus and the second activation follows it", async ({ + page, + }) => { + await page.goto(delegated); + await page.waitForFunction( + () => document.querySelector("astro-island[ssr]") === null, + ); + const link = page.locator('[data-game-fact="champion/Ahri"]'); + await expect(page.locator('[data-game-fact="item/3118"]')).toHaveAttribute( + "tabindex", + "0", + ); + await page.keyboard.press("Tab"); + await page.keyboard.press("Tab"); + await expect(link).toBeFocused(); + const popover = layer(page, "Ahri"); + await expect(popover).toBeVisible(); + // Focus shows the layer; the arrow moves into it and Escape comes back. + await page.keyboard.press("ArrowRight"); + await expect(page.locator('[role="dialog"]:focus-within')).toHaveCount(1); + await page.keyboard.press("Escape"); + await expect(popover).toHaveCount(0); + await expect(link).toBeFocused(); + expect(page.url()).not.toContain("#ahri"); + // Enter on the link itself follows it. + await page.keyboard.press("Enter"); + await expect(page).toHaveURL(/#ahri$/); + }); + + test("a keyboard opens the sheet from a rendered trigger and Escape returns focus to it", async ({ + page, + }) => { + await page.goto(interactive); + const trigger = page.getByRole("button", { name: "Ahri", exact: true }); + await expect(trigger).toBeVisible(); + await page.keyboard.press("Tab"); + await expect(trigger).toBeFocused(); + const sheet = page.getByRole("dialog", { name: "Ahri", exact: true }); + await expect(sheet).toHaveCount(0); + + await page.keyboard.press("Enter"); + await expect(sheet).toBeVisible(); + await expect(sheet.locator(":focus")).toHaveCount(1); + await page.keyboard.press("Escape"); + await expect(sheet).toHaveCount(0); + await expect(trigger).toBeFocused(); + + await page.keyboard.press("Space"); + await expect(sheet).toBeVisible(); + await expect(sheet.locator(":focus")).toHaveCount(1); + await page.keyboard.press("Escape"); + await expect(trigger).toBeFocused(); + }); + test("a tap opens a bottom sheet, a tapped part replaces its content, back returns", async ({ page, }) => { diff --git a/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.test.ts b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.test.ts new file mode 100644 index 0000000000..6f9ef7d3b8 --- /dev/null +++ b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, test, vi } from "vitest"; +import { opensSheet, type KeyLike } from "./rich-tooltip-external.ts"; + +function press(key: string, modifiers: Partial = {}): KeyLike { + return { + key, + altKey: false, + ctrlKey: false, + metaKey: false, + preventDefault: vi.fn(), + ...modifiers, + }; +} + +describe("opensSheet", () => { + test("Enter and Space open the sheet from a trigger with no click of its own", () => { + expect(opensSheet(press("Enter"), false)).toBe(true); + expect(opensSheet(press(" "), false)).toBe(true); + }); + + test("other keys do not", () => { + for (const key of ["ArrowRight", "ArrowDown", "Tab", "Escape", "a"]) { + expect(opensSheet(press(key), false)).toBe(false); + } + }); + + test("a link or button is left to its click, so the sheet opens once", () => { + expect(opensSheet(press("Enter"), true)).toBe(false); + expect(opensSheet(press(" "), true)).toBe(false); + }); + + test("a shortcut chord is not an activation", () => { + expect(opensSheet(press("Enter", { metaKey: true }), false)).toBe(false); + expect(opensSheet(press("Enter", { ctrlKey: true }), false)).toBe(false); + expect(opensSheet(press(" ", { altKey: true }), false)).toBe(false); + }); +}); diff --git a/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.ts b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.ts index 1b6f66c6f0..100b611e94 100644 --- a/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.ts +++ b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-external.ts @@ -21,6 +21,31 @@ export type TriggerRules = { keyDown: (event: KeyLike, element: Element) => void; }; +/** What a keyboard activates as a click itself, with no help from the tooltip. */ +const NATIVELY_ACTIVATED = "a[href], button, summary, input, select, textarea"; + +/** + * Whether Enter or Space on this trigger already produces a click, so a rule + * for the click covers the keyboard and a key rule would open the layer twice. + */ +export function isNativelyActivated(element: Element): boolean { + return element.matches(NATIVELY_ACTIVATED); +} + +/** + * Whether a key press on a trigger should open its bottom sheet. A page- + * rendered span or list item with a tabindex gets no click from Enter or + * Space, so without this a keyboard cannot open the sheet a tap opens. + */ +export function opensSheet( + event: KeyLike, + nativelyActivated: boolean, +): boolean { + if (event.altKey || event.ctrlKey || event.metaKey) return false; + if (nativelyActivated) return false; + return event.key === "Enter" || event.key === " "; +} + /** * Wires a page-rendered element as the trigger: the same rules a rendered * trigger gets through its props, attached as DOM listeners and removed again diff --git a/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-sheet.tsx b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-sheet.tsx index d0b75c990b..646c04d9ae 100644 --- a/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-sheet.tsx +++ b/packages/scout-for-lol/packages/design-system/src/components/overlays/rich-tooltip-sheet.tsx @@ -6,6 +6,8 @@ import { BottomSheetContent, } from "#src/components/overlays/bottom-sheet.tsx"; import { + isNativelyActivated, + opensSheet, useTriggerAria, useTriggerListeners, type TriggerRules, @@ -52,14 +54,21 @@ export function RichTooltipSheet(props: RichTooltipProps) { const [pushed, setPushed] = useState([]); const triggerRef = useRef(null); - // A page-rendered trigger only needs to open the sheet; a tap is all a phone - // has, so the hover and focus rules do nothing here. + // A trigger only needs to open the sheet, so the hover and focus rules do + // nothing here. A tap clicks; Enter or Space clicks a link or button, and + // anything else page-rendered with a tabindex needs the key rule. Opening + // the sheet moves focus into it, and Escape returns it to the trigger. const rules = useMemo>( () => ({ click: (event) => { event.preventDefault(); setOpen(true); }, + keyDown: (event, element) => { + if (!opensSheet(event, isNativelyActivated(element))) return; + event.preventDefault(); + setOpen(true); + }, }), [], ); @@ -89,8 +98,10 @@ export function RichTooltipSheet(props: RichTooltipProps) { aria-expanded={open} data-open={open ? "" : undefined} onClick={(event: React.MouseEvent) => { - event.preventDefault(); - setOpen(true); + rules.click?.(event); + }} + onKeyDown={(event: React.KeyboardEvent) => { + rules.keyDown?.(event, event.currentTarget); }} > {children}