fix: enforce handbook scrollbar and reduced motion contracts

This commit is contained in:
maoyingjie
2026-10-27 21:10:51 +08:00
parent b1344d8a4c
commit 202e3a5b5d
23 changed files with 702 additions and 23 deletions
+397
View File
@@ -0,0 +1,397 @@
<script setup lang="ts">
import { nextTick, onBeforeUnmount, ref } from "vue";
import {
Button,
Card,
Input,
Spinner,
Skeleton,
Popover,
PopoverContent,
PopoverDialog,
PopoverHeading,
Table,
TableScrollContainer,
TableContent,
TableHeader,
TableColumn,
TableBody,
TableRow,
TableCell,
} from "@heroui-vue/components";
import { buttonVariants, tv } from "@heroui-vue/styles";
const root = ref<HTMLElement>();
const local = ref<HTMLElement>();
const open = ref(false);
const reduced = ref(false);
const mode = ref("light");
const scrollMode = ref("none");
const clicks = ref(0);
const records = ref<string[]>([]);
const running = ref(false);
const surfaces = [
"hv-popover",
"hv-tooltip",
"hv-dropdown__popover",
"hv-select__popover",
"hv-combo-box__popover",
"hv-autocomplete__popover",
"hv-date-picker__popover",
"hv-date-range-picker__popover",
"hv-drawer__dialog",
"hv-drawer__backdrop",
];
const custom = tv({
extend: buttonVariants,
variants: { audit: { true: "audit-extra" } },
});
const frame = () =>
new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
const wait = (ms: number) =>
new Promise<void>((resolve) => setTimeout(resolve, ms));
function record(name: string, pass: boolean, detail: unknown) {
records.value.push(
`${pass ? "PASS" : "FAIL"} ${name}: ${JSON.stringify(detail)}`,
);
}
function style(selector: string) {
return getComputedStyle(document.querySelector<HTMLElement>(selector)!);
}
// All changes are confined to this regression subtree; no saved user preferences.
async function inspect() {
if (running.value) return;
running.value = true;
records.value = [];
try {
mode.value = "light";
reduced.value = true;
await nextTick();
await frame();
const token = document.createElement("span");
token.style.color = "var(--accent)";
local.value!.append(token);
const accent = getComputedStyle(token).color;
const before = style("#audit-input").backgroundColor;
record(
"semantic button accent",
style("#audit-button").backgroundColor === accent,
{ accent, button: style("#audit-button").backgroundColor },
);
mode.value = "dark";
await nextTick();
await frame();
record(
"field follows dark theme",
before !== style("#audit-input").backgroundColor,
{ light: before, dark: style("#audit-input").backgroundColor },
);
record(
"reduced spinner",
style("#audit-spinner").animationName === "none",
style("#audit-spinner").animationName,
);
record(
"reduced button transition",
style("#audit-button").transitionProperty === "none",
style("#audit-button").transitionProperty,
);
record(
"reduced skeleton shimmer",
getComputedStyle(document.querySelector("#audit-skeleton")!, "::after")
.animationName === "none",
getComputedStyle(document.querySelector("#audit-skeleton")!, "::after")
.animationName,
);
for (const [id, pseudo] of [
["#audit-skeleton-pulse", null],
["#audit-skeleton-parent", "::before"],
] as const) {
const animation = getComputedStyle(
document.querySelector(id)!,
pseudo,
).animationName;
record(`reduced skeleton ${id}`, animation === "none", animation);
}
const motionRules = Array.from(
document.querySelectorAll<HTMLElement>("[data-motion-probe]"),
).map((el) => ({
surface: el.className,
phase: el.dataset.motionProbe,
animation: getComputedStyle(el).animationName,
}));
record(
"all 20 BEM entrance/exit rules reduce motion",
motionRules.length === 20 &&
motionRules.every((item) => item.animation === "none"),
{
checked: motionRules.length,
failures: motionRules.filter((item) => item.animation !== "none"),
},
);
const composed = document.querySelector<HTMLElement>("#audit-composed")!;
const prior = clicks.value;
composed.click();
await nextTick();
record(
"asChild merges root and handlers once",
composed.tagName === "BUTTON" &&
composed.classList.contains("hv-button") &&
clicks.value === prior + 2,
{
tag: composed.tagName,
clicks: clicks.value - prior,
class: composed.className,
},
);
record(
"inline style and extended variants",
style("#audit-composed").borderTopWidth === "3px" &&
composed.classList.contains("audit-extra"),
{ border: style("#audit-composed").borderTopWidth },
);
for (const value of ["none", "default", "thin"]) {
scrollMode.value = value;
await nextTick();
await frame();
const expected = value === "default" ? "auto" : value;
record(
`scrollbar subtree ${value}`,
style("#audit-scroll").scrollbarWidth === expected,
style("#audit-scroll").scrollbarWidth,
);
record(
`nested scrollbar overrides ${value}`,
style("#audit-inner-scroll").scrollbarWidth === "thin",
style("#audit-inner-scroll").scrollbarWidth,
);
}
record(
"nested scrollbar overrides parent",
style("#audit-inner-scroll").scrollbarWidth === "thin",
style("#audit-inner-scroll").scrollbarWidth,
);
reduced.value = false;
await nextTick();
await frame();
const normalRules = Array.from(
document.querySelectorAll<HTMLElement>("[data-motion-probe]"),
).map((el) => getComputedStyle(el).animationName);
record(
"normal BEM animations retained",
normalRules.length === 20 && normalRules.every((name) => name !== "none"),
{ checked: normalRules.length },
);
open.value = true;
await nextTick();
await frame();
await frame();
const panel = document.querySelector<HTMLElement>("#audit-popover")!;
record(
"local portal theme",
panel &&
getComputedStyle(panel).getPropertyValue("--accent").trim() ===
getComputedStyle(local.value!).getPropertyValue("--accent").trim(),
{
parent: panel?.parentElement?.tagName,
accent:
panel && getComputedStyle(panel).getPropertyValue("--accent").trim(),
},
);
record(
"popover visible and focused",
!!panel &&
panel.getBoundingClientRect().width > 0 &&
panel.contains(document.activeElement),
{
width: panel?.getBoundingClientRect().width,
focus: document.activeElement?.getAttribute("data-slot"),
},
);
reduced.value = true;
await nextTick();
await frame();
await frame();
record(
"dynamic reduced motion clears entering",
!panel.hasAttribute("data-entering") &&
getComputedStyle(panel).animationName === "none",
{
entering: panel.dataset.entering,
animation: getComputedStyle(panel).animationName,
},
);
open.value = false;
await nextTick();
await wait(70);
record(
"reduced exit unmounts",
!document.querySelector("#audit-popover"),
null,
);
reduced.value = false;
await nextTick();
open.value = true;
await nextTick();
await wait(230);
open.value = false;
await nextTick();
await frame();
const exiting = document.querySelector<HTMLElement>("#audit-popover");
record(
"animated exit retained",
!!exiting && exiting.dataset.exiting === "true",
{ exiting: exiting?.dataset.exiting },
);
await wait(250);
record(
"animated exit unmounts",
!document.querySelector("#audit-popover"),
null,
);
record(
"focus restores to trigger",
document.activeElement?.hasAttribute("data-audit-trigger") === true,
document.activeElement?.id,
);
token.remove();
} catch (error) {
record("scenario exception", false, String(error));
} finally {
running.value = false;
}
}
onBeforeUnmount(() => {
open.value = false;
});
</script>
<template>
<main
ref="root"
class="handbook-lab"
:data-reduce-motion="reduced ? 'true' : undefined"
>
<h1>Handbook 实现回归</h1>
<p>检查公开组件最终样式与行为;仅修改此测试子树,不保存主题偏好。</p>
<Button :is-disabled="running" @click="inspect">运行规范检查</Button>
<label>
<input v-model="reduced" type="checkbox" />
减少动效
</label>
<label>
局部主题
<select v-model="mode">
<option>light</option>
<option>dark</option>
</select>
</label>
<section ref="local" :class="mode" class="audit-theme">
<Card class="audit-card"><p>语义表面</p></Card>
<Button id="audit-button">语义强调色</Button>
<Input
id="audit-input"
aria-label="规范字段"
placeholder="键盘焦点检查"
/>
<Spinner id="audit-spinner" aria-label="规范加载" />
<Skeleton id="audit-skeleton" style="width: 100px; height: 20px" />
<Skeleton
id="audit-skeleton-pulse"
animation-type="pulse"
style="width: 100px; height: 20px"
/>
<Skeleton id="audit-skeleton-parent">
<Skeleton style="width: 100px; height: 20px" />
</Skeleton>
<Button
as-child
:class="custom({ audit: true })"
style="border: 3px solid currentColor"
@click="clicks++"
>
<button id="audit-composed" @click="clicks++">
组合按钮 {{ clicks }}
</button>
</Button>
<div :data-scrollbar="scrollMode">
<Table>
<TableScrollContainer id="audit-scroll" class="audit-scroll">
<TableContent aria-label="滚动条规范">
<TableHeader><TableColumn>内容</TableColumn></TableHeader>
<TableBody>
<TableRow v-for="n in 12" :key="n" :row-key="n">
<TableCell>滚动内容 {{ n }}</TableCell>
</TableRow>
</TableBody>
</TableContent>
</TableScrollContainer>
</Table>
<div data-scrollbar="thin">
<Table>
<TableScrollContainer id="audit-inner-scroll" class="audit-scroll">
<TableContent aria-label="嵌套滚动条">
<TableHeader><TableColumn>内容</TableColumn></TableHeader>
<TableBody>
<TableRow v-for="n in 12" :key="n" :row-key="n">
<TableCell>嵌套内容 {{ n }}</TableCell>
</TableRow>
</TableBody>
</TableContent>
</TableScrollContainer>
</Table>
</div>
</div>
<Popover v-model:open="open">
<Button data-audit-trigger>规范弹层</Button>
<PopoverContent id="audit-popover" :to="local">
<PopoverDialog>
<PopoverHeading>局部主题弹层</PopoverHeading>
<Button @click="open = false">关闭规范弹层</Button>
</PopoverDialog>
</PopoverContent>
</Popover>
</section>
<!-- Raw BEM use is a public Handbook contract. These probes verify CSS only;
Popover above separately verifies the actual component lifecycle. -->
<div hidden aria-hidden="true">
<template v-for="surface in surfaces" :key="surface">
<div
v-for="phase in ['entering', 'exiting']"
:key="phase"
:class="surface"
:data-motion-probe="phase"
:data-entering="phase === 'entering' ? 'true' : undefined"
:data-exiting="phase === 'exiting' ? 'true' : undefined"
data-placement="bottom"
/>
</template>
</div>
<pre aria-live="polite" id="audit-results">{{ records.join("\n") }}</pre>
</main>
</template>
<style scoped>
.handbook-lab {
padding: 24px;
display: grid;
gap: 16px;
}
.audit-theme {
--accent: oklch(0.62 0.2 300);
padding: 20px;
display: grid;
gap: 16px;
background: var(--background);
color: var(--foreground);
}
.audit-scroll {
height: 72px;
overflow: auto;
}
pre {
white-space: pre-wrap;
overflow-wrap: anywhere;
font-size: 13px;
}
</style>