Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions packages/scout-for-lol/packages/design-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ restoration, and native form validation semantics. They target specific
stories by id, so renaming those stories or their exports breaks the specs —
`Runtime/ThemeMenu` → `Default`, `Components/Overlays/Dialog` → `CreateReport`,
and `Components/Forms/Field` → `SemanticFormStates` are load-bearing names.
`e2e/rich-tooltip.spec.ts` drives `Domain/EntityTooltip` → `Interactive` and
`ThreeLayers` the same way.

The parts both Scout catalogs share — the story-index loader, the page-level
axe exclusions, the mount assertions, and the CI reporter shape — live in
Expand All @@ -119,6 +121,28 @@ preview with the installed Node runtime.
Cross-browser visual coverage lives in `@scout-for-lol/design-audit`, which
exercises the shipped Scout surfaces nightly. This package runs chromium only.

## Rich tooltips

`RichTooltip` (`components/overlays/rich-tooltip`) is a layered shell: hover
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
rules live in `hover-layer-controller.ts` and are tested with fake timers. The
shell never fetches.

`domain/tooltips/entity-tooltip` supplies the bodies for champions, abilities, items,
runes, summoner spells and keywords from the `GameFact` shape, and
`domain/tooltips/player-tooltip` the player body. Each takes a `scout` node and an
`href`; the page owns Scout's numbers. `GameFactTooltip` wires a trigger to a
body through `useGameFact`, which loads through the `GameFactProvider`'s
`loader` (the app passes `loadGameFact` from
`@scout-for-lol/data/game-facts/client.ts`), caches in memory and aborts the
request when the layer closes. The facts' types come from
`@scout-for-lol/data/game-facts/schema.ts`, and the stories load the generated
fact JSON itself (`entity-tooltip.facts.ts`), never retyped values.

## Tokens

`tokens/themes.json` is the source of truth. `bun run generate` writes
Expand Down
202 changes: 202 additions & 0 deletions packages/scout-for-lol/packages/design-system/e2e/rich-tooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { expect, test, type Page } from "@playwright/test";

const interactive =
"/iframe.html?viewMode=story&id=domain-entitytooltip--interactive";
const threeLayers =
"/iframe.html?viewMode=story&id=domain-entitytooltip--three-layers";

const OPEN_DELAY = 350;
const CLOSE_DELAY = 150;

const layer = (page: Page, name: string) =>
page.getByRole("dialog", { name, exact: true });

test.describe("pointer", () => {
test("hover opens a layer after the open delay, not before", async ({
page,
}) => {
await page.clock.install();
await page.goto(interactive);
await page.getByRole("button", { name: "Ahri", exact: true }).hover();

await page.clock.runFor(OPEN_DELAY - 50);
await expect(layer(page, "Ahri")).toHaveCount(0);

await page.clock.runFor(100);
await expect(layer(page, "Ahri")).toBeVisible();
// The fact is loaded for the layer; its header names the champion.
await expect(
layer(page, "Ahri").getByText("the Nine-Tailed Fox"),
).toBeVisible();
});

test("leaving before the delay never opens it", async ({ page }) => {
await page.clock.install();
await page.goto(interactive);
await page.getByRole("button", { name: "Ahri", exact: true }).hover();
await page.clock.runFor(OPEN_DELAY - 100);
await page.mouse.move(700, 600);
await page.clock.runFor(1000);
await expect(layer(page, "Ahri")).toHaveCount(0);
});

test("moving into a child keeps the parent open, and leaving closes both after the grace period", async ({
page,
}) => {
await page.clock.install();
await page.goto(interactive);
await page.getByRole("button", { name: "Ahri", exact: true }).hover();
await page.clock.runFor(OPEN_DELAY + 50);
await expect(layer(page, "Ahri")).toBeVisible();

// Hover the Charm ability inside the champion layer.
await layer(page, "Ahri").getByRole("button", { name: "Charm" }).hover();
await page.clock.runFor(OPEN_DELAY + 50);
await expect(layer(page, "Charm")).toBeVisible();

// Into the child, then well past the grace period: both stay.
await layer(page, "Charm").getByText("Cooldown").hover();
await page.clock.runFor(CLOSE_DELAY * 4);
await expect(layer(page, "Charm")).toBeVisible();
await expect(layer(page, "Ahri")).toBeVisible();

// The child sits beside its parent, not over its middle.
const parentBox = await layer(page, "Ahri").boundingBox();
const childBox = await layer(page, "Charm").boundingBox();
expect(parentBox).not.toBeNull();
expect(childBox).not.toBeNull();
if (parentBox === null || childBox === null) return;
expect(childBox.x).toBeGreaterThan(parentBox.x + parentBox.width / 2);

// Out of everything: nothing closes inside the grace period, all does after.
await page.mouse.move(5, 700);
await page.clock.runFor(CLOSE_DELAY - 50);
await expect(layer(page, "Charm")).toBeVisible();
await page.clock.runFor(200);
await expect(layer(page, "Charm")).toHaveCount(0);
await expect(layer(page, "Ahri")).toHaveCount(0);
});
});

test.describe("keyboard", () => {
test("Escape closes only the top layer", async ({ page }) => {
await page.goto(threeLayers);
await expect(layer(page, "Ahri")).toBeVisible();
await expect(layer(page, "Charm")).toHaveCount(2);

// The keyword layer is the top one.
await page.keyboard.press("Escape");
await expect(layer(page, "Charm")).toHaveCount(1);
await expect(layer(page, "Ahri")).toBeVisible();

await page.keyboard.press("Escape");
await expect(layer(page, "Charm")).toHaveCount(0);
await expect(layer(page, "Ahri")).toBeVisible();

await page.keyboard.press("Escape");
await expect(layer(page, "Ahri")).toHaveCount(0);
});

test("a part is reachable by keyboard and opens its own layer with focus in 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();
// Focus opens immediately and leaves focus on the trigger.
await expect(layer(page, "Ahri")).toBeVisible();
await expect(trigger).toBeFocused();

// The right arrow moves focus into the layer, onto its first part. Wait
// for the fact to load first: a layer still showing its skeleton has no
// part to focus.
await expect(
layer(page, "Ahri").getByRole("button", { name: "Essence Theft" }),
).toBeVisible();
await page.keyboard.press("ArrowRight");
const ahri = layer(page, "Ahri");
await expect(
ahri.getByRole("button", { name: "Essence Theft" }),
).toBeFocused();

// Tab to the Charm ability: its layer opens on focus.
for (let press = 0; press < 3; press += 1) await page.keyboard.press("Tab");
const charmPart = ahri.getByRole("button", { name: "Charm" });
await expect(charmPart).toBeFocused();
const charm = layer(page, "Charm").filter({ hasText: "Ahri's E" });
const keyword = layer(page, "Charm").filter({ hasText: "Crowd control" });
await expect(charm).toBeVisible();

// Enter moves focus into that layer, onto the keyword it contains. The
// keyword's own layer opens beside it as it takes focus.
const charms = charm.getByRole("button", { name: "Charms" });
await expect(charms).toBeVisible();
await page.keyboard.press("Enter");
await expect(charms).toBeFocused();
await expect(keyword).toBeVisible();

// Escape closes only the top layer: the keyword's, then the ability's,
// which puts focus back on its part.
await page.keyboard.press("Escape");
await expect(keyword).toHaveCount(0);
await expect(charm).toBeVisible();
await page.keyboard.press("Escape");
await expect(charm).toHaveCount(0);
await expect(ahri).toBeVisible();
await expect(charmPart).toBeFocused();

// Focus returning to the part does not reopen its layer.
await page.waitForTimeout(OPEN_DELAY + 100);
await expect(charm).toHaveCount(0);

// A last Escape closes the champion layer and returns to the trigger.
await page.keyboard.press("Escape");
await expect(ahri).toHaveCount(0);
await expect(trigger).toBeFocused();
});
});

test.describe("phone", () => {
test.use({ viewport: { width: 390, height: 844 }, hasTouch: true });

test("a tap opens a bottom sheet, a tapped part replaces its content, back returns", async ({
page,
}) => {
await page.goto(interactive);
await page.getByRole("button", { name: "Ahri", exact: true }).tap();

const sheet = page.getByRole("dialog", { name: "Ahri", exact: true });
await expect(sheet).toBeVisible();
await expect(sheet.getByText("the Nine-Tailed Fox")).toBeVisible();
// The sheet is a bottom sheet: full width, flush with the bottom edge.
const box = await sheet.boundingBox();
expect(box?.width).toBe(390);
expect((box?.y ?? 0) + (box?.height ?? 0)).toBeCloseTo(844, 0);

// A part replaces the content with a back control.
await sheet.getByRole("button", { name: "Charm" }).tap();
const charm = page.getByRole("dialog", { name: "Charm", exact: true });
await expect(charm.getByText("Ahri's E")).toBeVisible();
const back = page.getByRole("button", { name: "Back to Ahri" });
await expect(back).toBeVisible();

// And again, down to the keyword, then back out one step at a time.
await charm.getByRole("button", { name: "Charms" }).tap();
await expect(
page.getByRole("button", { name: "Back to Charm" }),
).toBeVisible();
await page.getByRole("button", { name: "Back to Charm" }).tap();
await expect(page.getByText("Ahri's E")).toBeVisible();
await page.getByRole("button", { name: "Back to Ahri" }).tap();
await expect(page.getByText("the Nine-Tailed Fox")).toBeVisible();

// Closing returns focus to the trigger.
await page.getByRole("button", { name: "Close" }).tap();
await expect(sheet).toHaveCount(0);
await expect(
page.getByRole("button", { name: "Ahri", exact: true }),
).toBeFocused();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
RICH_TOOLTIP_CLOSE_DELAY,
RICH_TOOLTIP_OPEN_DELAY,
createLayerController,
type LayerController,
} from "./hover-layer-controller.ts";

type Harness = {
controller: LayerController;
changes: boolean[];
engagement: boolean[];
};

function harness(initialOpen = false): Harness {
const changes: boolean[] = [];
const engagement: boolean[] = [];
const controller = createLayerController<ReturnType<typeof setTimeout>>({
initialOpen,
onOpenChange: (open) => changes.push(open),
onEngagedChange: (engaged) => engagement.push(engaged),
timers: {
setTimeout: (callback, milliseconds) =>
globalThis.setTimeout(callback, milliseconds),
clearTimeout: (handle) => {
globalThis.clearTimeout(handle);
},
},
});
return { controller, changes, engagement };
}

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

describe("layer controller", () => {
test("hover opens only after the open delay", () => {
const { controller, changes } = harness();
controller.pointerTrigger(true);
vi.advanceTimersByTime(RICH_TOOLTIP_OPEN_DELAY - 1);
expect(changes).toEqual([]);
vi.advanceTimersByTime(1);
expect(changes).toEqual([true]);
expect(controller.isOpen()).toBe(true);
});

test("leaving before the delay cancels the open", () => {
const { controller, changes } = harness();
controller.pointerTrigger(true);
vi.advanceTimersByTime(RICH_TOOLTIP_OPEN_DELAY - 50);
controller.pointerTrigger(false);
vi.advanceTimersByTime(1000);
expect(changes).toEqual([]);
});

test("focus opens immediately", () => {
const { controller, changes } = harness();
controller.focusTrigger(true);
expect(changes).toEqual([true]);
});

test("closes after the grace period, not before", () => {
const { controller, changes } = harness();
controller.focusTrigger(true);
controller.focusTrigger(false);
vi.advanceTimersByTime(RICH_TOOLTIP_CLOSE_DELAY - 1);
expect(controller.isOpen()).toBe(true);
vi.advanceTimersByTime(1);
expect(changes).toEqual([true, false]);
});

test("moving from the trigger into the content keeps it open", () => {
const { controller, changes } = harness();
controller.pointerTrigger(true);
vi.advanceTimersByTime(RICH_TOOLTIP_OPEN_DELAY);
controller.pointerTrigger(false);
vi.advanceTimersByTime(RICH_TOOLTIP_CLOSE_DELAY - 50);
controller.pointerContent(true);
vi.advanceTimersByTime(5000);
expect(changes).toEqual([true]);
});

test("stays open while a descendant layer is engaged", () => {
const { controller, changes } = harness();
controller.focusTrigger(true);
controller.pointerContent(true);
controller.childEngaged("child", true);
controller.pointerContent(false);
vi.advanceTimersByTime(5000);
expect(changes).toEqual([true]);
controller.childEngaged("child", false);
vi.advanceTimersByTime(RICH_TOOLTIP_CLOSE_DELAY);
// Focus on the trigger still holds it.
expect(controller.isOpen()).toBe(true);
controller.focusTrigger(false);
vi.advanceTimersByTime(RICH_TOOLTIP_CLOSE_DELAY);
expect(changes).toEqual([true, false]);
});

test("close is immediate and forgets the content", () => {
const { controller, changes } = harness();
controller.pointerTrigger(true);
vi.advanceTimersByTime(RICH_TOOLTIP_OPEN_DELAY);
controller.pointerContent(true);
controller.close();
expect(changes).toEqual([true, false]);
controller.pointerTrigger(false);
expect(controller.isEngaged()).toBe(false);
});

test("reports engagement changes once each", () => {
const { controller, engagement } = harness();
controller.pointerTrigger(true);
controller.pointerContent(true);
controller.pointerTrigger(false);
controller.pointerContent(false);
expect(engagement).toEqual([true, false]);
});

test("dispose drops pending timers and reports disengagement", () => {
const { controller, changes, engagement } = harness();
controller.pointerTrigger(true);
controller.dispose();
vi.advanceTimersByTime(1000);
expect(changes).toEqual([]);
expect(engagement).toEqual([true, false]);
});

test("a layer that starts open does not close until it is engaged and left", () => {
const { controller, changes } = harness(true);
vi.advanceTimersByTime(5000);
expect(changes).toEqual([]);
controller.pointerContent(true);
controller.pointerContent(false);
vi.advanceTimersByTime(RICH_TOOLTIP_CLOSE_DELAY);
expect(changes).toEqual([false]);
});
});
Loading