Skip to content
Open
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
7 changes: 6 additions & 1 deletion packages/scout-for-lol/packages/design-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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> = {}): 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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
BottomSheetContent,
} from "#src/components/overlays/bottom-sheet.tsx";
import {
isNativelyActivated,
opensSheet,
useTriggerAria,
useTriggerListeners,
type TriggerRules,
Expand Down Expand Up @@ -52,14 +54,21 @@ export function RichTooltipSheet(props: RichTooltipProps) {
const [pushed, setPushed] = useState<SheetEntry[]>([]);
const triggerRef = useRef<HTMLElement | null>(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<Partial<TriggerRules>>(
() => ({
click: (event) => {
event.preventDefault();
setOpen(true);
},
keyDown: (event, element) => {
if (!opensSheet(event, isNativelyActivated(element))) return;
event.preventDefault();
setOpen(true);
},
}),
[],
);
Expand Down Expand Up @@ -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<HTMLElement>) => {
rules.keyDown?.(event, event.currentTarget);
}}
>
{children}
Expand Down