merge: integrate reviewed AlertDialog Drawer with ListBox main
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, nextTick, ref } from "vue";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogBackdrop,
|
||||
AlertDialogBody,
|
||||
AlertDialogClose,
|
||||
AlertDialogCloseTrigger,
|
||||
AlertDialogContainer,
|
||||
AlertDialogDialog,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogHeading,
|
||||
AlertDialogIcon,
|
||||
AlertDialogTrigger,
|
||||
Button,
|
||||
useOverlayState,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(async () => {
|
||||
for (const w of wrappers.splice(0)) w.unmount();
|
||||
await flushPromises();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
const components = {
|
||||
AlertDialog,
|
||||
AlertDialogBackdrop,
|
||||
AlertDialogContainer,
|
||||
AlertDialogDialog,
|
||||
AlertDialogCloseTrigger,
|
||||
AlertDialogHeader,
|
||||
AlertDialogHeading,
|
||||
AlertDialogIcon,
|
||||
AlertDialogBody,
|
||||
AlertDialogFooter,
|
||||
AlertDialogClose,
|
||||
AlertDialogTrigger,
|
||||
Button,
|
||||
};
|
||||
|
||||
const panel =
|
||||
'<AlertDialogBackdrop><AlertDialogContainer><AlertDialogDialog><AlertDialogCloseTrigger/><AlertDialogHeader><AlertDialogIcon status="danger"/><AlertDialogHeading>要删除吗?</AlertDialogHeading></AlertDialogHeader><AlertDialogBody>内容</AlertDialogBody><AlertDialogFooter><AlertDialogClose as-child><Button>取消</Button></AlertDialogClose></AlertDialogFooter></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop>';
|
||||
|
||||
function view(template: string, setup = () => ({})) {
|
||||
const w = mount(defineComponent({ components, template, setup }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
wrappers.push(w);
|
||||
return w;
|
||||
}
|
||||
|
||||
it("opens with role=alertdialog and real accessible name", async () => {
|
||||
const w = view(`<AlertDialog><Button>打开</Button>${panel}</AlertDialog>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[role="alertdialog"]')!;
|
||||
expect(dialog).not.toBeNull();
|
||||
expect(dialog.getAttribute("data-slot")).toBe("alert-dialog-dialog");
|
||||
const labelled = dialog.getAttribute("aria-labelledby")!;
|
||||
expect(document.getElementById(labelled)?.textContent).toContain("要删除吗");
|
||||
});
|
||||
|
||||
it("defaults to non-dismissable outside click and disabled Escape", async () => {
|
||||
const w = view(`<AlertDialog><Button>打开</Button>${panel}</AlertDialog>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[role="alertdialog"]')!;
|
||||
expect(dialog).not.toBeNull();
|
||||
// Escape must not close when keyboard dismiss is disabled (default).
|
||||
window.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Escape",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
it("allows Escape and outside dismiss only when explicitly enabled", async () => {
|
||||
const w = view(
|
||||
`<AlertDialog><Button>打开</Button><AlertDialogBackdrop :is-dismissable="true" :is-keyboard-dismiss-disabled="false"><AlertDialogContainer><AlertDialogDialog><AlertDialogHeading>标题</AlertDialogHeading></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></AlertDialog>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).not.toBeNull();
|
||||
window.dispatchEvent(
|
||||
new KeyboardEvent("keydown", {
|
||||
key: "Escape",
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
}),
|
||||
);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("close button and AlertDialogClose close the dialog", async () => {
|
||||
const w = view(`<AlertDialog><Button>打开</Button>${panel}</AlertDialog>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).not.toBeNull();
|
||||
(
|
||||
document.querySelector(
|
||||
"[data-slot=alert-dialog-close-trigger]",
|
||||
) as HTMLElement
|
||||
).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
|
||||
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const cancel = [...document.querySelectorAll("button")].find(
|
||||
(b) => b.textContent?.trim() === "取消",
|
||||
)!;
|
||||
cancel.click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("controlled parent rejects open request and allows retry", async () => {
|
||||
const opens = vi.fn();
|
||||
const w = view(
|
||||
`<AlertDialog :is-open="false" @update:is-open="opens"><Button>打开</Button>${panel}</AlertDialog>`,
|
||||
() => ({ opens }),
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(opens).toHaveBeenCalled();
|
||||
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
|
||||
// Same request again still emits.
|
||||
const before = opens.mock.calls.length;
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(opens.mock.calls.length).toBeGreaterThan(before);
|
||||
});
|
||||
|
||||
it("supports useOverlayState controlled mode", async () => {
|
||||
const w = view(
|
||||
`<div><Button @click="state.open()">打开</Button><AlertDialogBackdrop :state="state"><AlertDialogContainer><AlertDialogDialog><AlertDialogHeading>状态</AlertDialogHeading></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></div>`,
|
||||
() => ({ state: useOverlayState() }),
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
it("icon status variants and custom icon slot", async () => {
|
||||
const w = view(
|
||||
`<AlertDialog><Button>打开</Button><AlertDialogBackdrop><AlertDialogContainer><AlertDialogDialog><AlertDialogHeader><AlertDialogIcon status="success" class="custom-icon"><span class="custom">X</span></AlertDialogIcon><AlertDialogHeading>成功</AlertDialogHeading></AlertDialogHeader></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></AlertDialog>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const icon = document.querySelector('[data-slot="alert-dialog-icon"]')!;
|
||||
expect(icon.className).toContain("hv-alert-dialog__icon--success");
|
||||
expect(icon.className).toContain("custom-icon");
|
||||
expect(icon.querySelector(".custom")?.textContent).toBe("X");
|
||||
});
|
||||
|
||||
it("placement and size land on container and dialog", async () => {
|
||||
const w = view(
|
||||
`<AlertDialog><Button>打开</Button><AlertDialogBackdrop><AlertDialogContainer placement="bottom" size="lg"><AlertDialogDialog><AlertDialogHeading>标题</AlertDialogHeading></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></AlertDialog>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const container = document.querySelector(
|
||||
'[data-slot="alert-dialog-container"]',
|
||||
)!;
|
||||
expect(container.getAttribute("data-placement")).toBe("bottom");
|
||||
expect(container.className).toContain("hv-alert-dialog__container");
|
||||
const dialog = document.querySelector('[role="alertdialog"]')!;
|
||||
expect(dialog.className).toContain("hv-alert-dialog__dialog--lg");
|
||||
expect(dialog.getAttribute("data-placement")).toBe("bottom");
|
||||
});
|
||||
|
||||
it("explicit aria-labelledby wins over computed titles", async () => {
|
||||
const w = view(
|
||||
`<AlertDialog><Button>打开</Button><AlertDialogBackdrop><AlertDialogContainer><AlertDialogDialog aria-labelledby="custom-title"><AlertDialogHeading>内部标题</AlertDialogHeading></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></AlertDialog>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[role="alertdialog"]')!;
|
||||
expect(dialog.getAttribute("aria-labelledby")).toBe("custom-title");
|
||||
});
|
||||
|
||||
it("all 14 demos mount without Vue warnings", async () => {
|
||||
const names = [
|
||||
"default",
|
||||
"statuses",
|
||||
"placements",
|
||||
"sizes",
|
||||
"controlled",
|
||||
"custom-icon",
|
||||
"custom-trigger",
|
||||
"backdrop-variants",
|
||||
"custom-backdrop",
|
||||
"dismiss-behavior",
|
||||
"close-methods",
|
||||
"custom-animations",
|
||||
"custom-portal",
|
||||
"custom-styles",
|
||||
];
|
||||
expect(names).toHaveLength(14);
|
||||
const warnings: string[] = [];
|
||||
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
|
||||
warnings.push(args.map(String).join(" "));
|
||||
});
|
||||
for (const name of names) {
|
||||
const mod = await import(`../apps/docs/demos/alert-dialog-${name}.vue`);
|
||||
const w = mount(mod.default, { attachTo: document.body });
|
||||
await nextTick();
|
||||
expect(w.html().length).toBeGreaterThan(0);
|
||||
w.unmount();
|
||||
}
|
||||
spy.mockRestore();
|
||||
expect(warnings).toEqual([]);
|
||||
});
|
||||
|
||||
it("outside pointer remains blocked by default and closes only when enabled", async () => {
|
||||
const dismissable = ref(false);
|
||||
const w = view(
|
||||
`<AlertDialog><Button>打开</Button><AlertDialogBackdrop :is-dismissable="dismissable"><AlertDialogContainer><AlertDialogDialog><AlertDialogHeading>保护操作</AlertDialogHeading><Button>取消</Button></AlertDialogDialog></AlertDialogContainer></AlertDialogBackdrop></AlertDialog>`,
|
||||
() => ({ dismissable }),
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const outside = () => {
|
||||
const backdrop = document.querySelector(
|
||||
'[data-slot="alert-dialog-backdrop"]',
|
||||
)!;
|
||||
backdrop.dispatchEvent(
|
||||
new MouseEvent("pointerdown", { bubbles: true, cancelable: true }),
|
||||
);
|
||||
backdrop.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
||||
};
|
||||
outside();
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).not.toBeNull();
|
||||
dismissable.value = true;
|
||||
await nextTick();
|
||||
outside();
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[role="alertdialog"]')).toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,351 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, nextTick, ref } from "vue";
|
||||
import { readFileSync } from "node:fs";
|
||||
import {
|
||||
Button,
|
||||
Drawer,
|
||||
DrawerBackdrop,
|
||||
DrawerBody,
|
||||
DrawerClose,
|
||||
DrawerCloseTrigger,
|
||||
DrawerContent,
|
||||
DrawerDialog,
|
||||
DrawerFooter,
|
||||
DrawerHandle,
|
||||
DrawerHeader,
|
||||
DrawerHeading,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(async () => {
|
||||
for (const w of wrappers.splice(0)) w.unmount();
|
||||
await flushPromises();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
const components = {
|
||||
Drawer,
|
||||
DrawerBackdrop,
|
||||
DrawerContent,
|
||||
DrawerDialog,
|
||||
DrawerCloseTrigger,
|
||||
DrawerHandle,
|
||||
DrawerHeader,
|
||||
DrawerHeading,
|
||||
DrawerBody,
|
||||
DrawerFooter,
|
||||
DrawerClose,
|
||||
Button,
|
||||
};
|
||||
|
||||
const panel =
|
||||
'<DrawerBackdrop><DrawerContent placement="bottom"><DrawerDialog><DrawerHandle/><DrawerCloseTrigger/><DrawerHeader><DrawerHeading>抽屉标题</DrawerHeading></DrawerHeader><DrawerBody>内容</DrawerBody><DrawerFooter><DrawerClose as-child><Button>关闭</Button></DrawerClose></DrawerFooter></DrawerDialog></DrawerContent></DrawerBackdrop>';
|
||||
|
||||
function view(template: string, setup = () => ({})) {
|
||||
const w = mount(defineComponent({ components, template, setup }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
wrappers.push(w);
|
||||
return w;
|
||||
}
|
||||
|
||||
it("opens drawer with dialog role and heading association", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[data-slot="drawer-dialog"]')!;
|
||||
expect(dialog).not.toBeNull();
|
||||
const labelled = dialog.getAttribute("aria-labelledby")!;
|
||||
expect(document.getElementById(labelled)?.textContent).toContain("抽屉标题");
|
||||
expect(
|
||||
document
|
||||
.querySelector('[data-slot="drawer-handle"]')
|
||||
?.getAttribute("aria-hidden"),
|
||||
).toBe("true");
|
||||
});
|
||||
|
||||
it("places panel by placement class", async () => {
|
||||
const w = view(
|
||||
`<Drawer><Button>打开</Button><DrawerBackdrop><DrawerContent placement="left"><DrawerDialog><DrawerHeading>左</DrawerHeading></DrawerDialog></DrawerContent></DrawerBackdrop></Drawer>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const content = document.querySelector('[data-slot="drawer-content"]')!;
|
||||
expect(content.className).toContain("hv-drawer__content--left");
|
||||
expect(content.getAttribute("data-placement")).toBe("left");
|
||||
const dialog = document.querySelector('[data-slot="drawer-dialog"]')!;
|
||||
expect(dialog.getAttribute("data-placement")).toBe("left");
|
||||
});
|
||||
|
||||
it("isDismissable=false blocks drag handlers on the dialog", async () => {
|
||||
const w = view(
|
||||
`<Drawer><Button>打开</Button><DrawerBackdrop :is-dismissable="false"><DrawerContent><DrawerDialog><DrawerHeading>不可拖</DrawerHeading></DrawerDialog></DrawerContent></DrawerBackdrop></Drawer>`,
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[data-slot="drawer-dialog"]')!;
|
||||
expect(dialog.getAttribute("style") ?? "").not.toContain("none");
|
||||
});
|
||||
|
||||
it("drag past 30% dimension closes the drawer", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector('[data-slot="drawer-dialog"]')!;
|
||||
// Give the dialog a measurable height (jsdom defaults to 0).
|
||||
Object.defineProperty(dialog, "offsetHeight", {
|
||||
value: 200,
|
||||
configurable: true,
|
||||
});
|
||||
function pointer(type: string, clientY: number) {
|
||||
const event = new MouseEvent(type, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
button: 0,
|
||||
clientY,
|
||||
});
|
||||
Object.defineProperty(event, "pointerId", { value: 1 });
|
||||
dialog.dispatchEvent(event);
|
||||
}
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 120);
|
||||
pointer("pointermove", 180);
|
||||
// 80px of 200 = 40% > 30% → dismiss
|
||||
pointer("pointerup", 180);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("small slow drag snaps back and does not dismiss", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector<HTMLElement>(
|
||||
'[data-slot="drawer-dialog"]',
|
||||
)!;
|
||||
Object.defineProperty(dialog, "offsetHeight", {
|
||||
value: 200,
|
||||
configurable: true,
|
||||
});
|
||||
// Controlled clock: 10px over 200ms → 0.05px/ms, well under the 0.5 threshold.
|
||||
let now = 1_000_000;
|
||||
const nowSpy = vi.spyOn(Date, "now").mockImplementation(() => now);
|
||||
function pointer(type: string, clientY: number) {
|
||||
now += 100;
|
||||
const event = new MouseEvent(type, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
button: 0,
|
||||
clientY,
|
||||
});
|
||||
Object.defineProperty(event, "pointerId", { value: 1 });
|
||||
dialog.dispatchEvent(event);
|
||||
}
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 105);
|
||||
pointer("pointermove", 110);
|
||||
pointer("pointerup", 110);
|
||||
nowSpy.mockRestore();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).not.toBeNull();
|
||||
});
|
||||
|
||||
it("fast flick closes even under the distance threshold", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const dialog = document.querySelector<HTMLElement>(
|
||||
'[data-slot="drawer-dialog"]',
|
||||
)!;
|
||||
Object.defineProperty(dialog, "offsetHeight", {
|
||||
value: 200,
|
||||
configurable: true,
|
||||
});
|
||||
// 15px in 10ms → 1.5px/ms > 0.5, dismiss on velocity alone.
|
||||
let now = 1_000_000;
|
||||
const nowSpy = vi.spyOn(Date, "now").mockImplementation(() => now);
|
||||
function pointer(type: string, clientY: number) {
|
||||
now += 5;
|
||||
const event = new MouseEvent(type, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
button: 0,
|
||||
clientY,
|
||||
});
|
||||
Object.defineProperty(event, "pointerId", { value: 1 });
|
||||
dialog.dispatchEvent(event);
|
||||
}
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 110);
|
||||
pointer("pointerup", 115);
|
||||
nowSpy.mockRestore();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("controlled parent rejects open and allows retry", async () => {
|
||||
const opens = vi.fn();
|
||||
const w = view(
|
||||
`<Drawer :is-open="false" @update:is-open="opens"><Button>打开</Button>${panel}</Drawer>`,
|
||||
() => ({ opens }),
|
||||
);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(opens).toHaveBeenCalled();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
const before = opens.mock.calls.length;
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(opens.mock.calls.length).toBeGreaterThan(before);
|
||||
});
|
||||
|
||||
it("closes via DrawerClose and close trigger", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
(
|
||||
document.querySelector("[data-slot=drawer-close-trigger]") as HTMLElement
|
||||
).click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
[...document.querySelectorAll("button")]
|
||||
.find((b) => b.textContent?.trim() === "关闭")!
|
||||
.click();
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
});
|
||||
|
||||
it("all 9 demos mount without Vue warnings", async () => {
|
||||
const names = [
|
||||
"basic",
|
||||
"placements",
|
||||
"non-dismissable",
|
||||
"scrollable-content",
|
||||
"controlled",
|
||||
"with-form",
|
||||
"navigation",
|
||||
"backdrop-variants",
|
||||
"custom-styles",
|
||||
];
|
||||
expect(names).toHaveLength(9);
|
||||
const warnings: string[] = [];
|
||||
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
|
||||
warnings.push(args.map(String).join(" "));
|
||||
});
|
||||
for (const name of names) {
|
||||
const mod = await import(`../apps/docs/demos/drawer-${name}.vue`);
|
||||
const w = mount(mod.default, { attachTo: document.body });
|
||||
await nextTick();
|
||||
expect(w.html().length).toBeGreaterThan(0);
|
||||
w.unmount();
|
||||
}
|
||||
spy.mockRestore();
|
||||
expect(warnings).toEqual([]);
|
||||
});
|
||||
|
||||
it("uses CSS animations so Presence can defer exit unmount", async () => {
|
||||
// Source contract: enter/exit must be CSS animations (not only transitions)
|
||||
// because Reka Presence keys off animationName. jsdom has no animation
|
||||
// engine so runtime deferral is verified in the browser instead.
|
||||
const css = readFileSync("packages/styles/components/drawer.css", "utf8");
|
||||
for (const name of [
|
||||
"hv-drawer-backdrop-in",
|
||||
"hv-drawer-backdrop-out",
|
||||
"hv-drawer-in-from-bottom",
|
||||
"hv-drawer-out-to-bottom",
|
||||
"hv-drawer-out-to-right",
|
||||
"hv-drawer-out-to-left",
|
||||
"hv-drawer-out-to-top",
|
||||
]) {
|
||||
expect(css).toContain(`@keyframes ${name}`);
|
||||
}
|
||||
expect(css).toMatch(/\.hv-drawer__dialog\[data-exiting="true"\]/);
|
||||
expect(css).not.toMatch(
|
||||
/\.hv-drawer__dialog\s*\{[^}]*transition:\s*translate/,
|
||||
);
|
||||
});
|
||||
|
||||
it("multiple drawers can coexist with unique heading ids", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components,
|
||||
template: `
|
||||
<div>
|
||||
<Drawer>
|
||||
<Button class="open-a">打开 A</Button>
|
||||
<DrawerBackdrop>
|
||||
<DrawerContent placement="bottom">
|
||||
<DrawerDialog>
|
||||
<DrawerHeading>抽屉 A</DrawerHeading>
|
||||
</DrawerDialog>
|
||||
</DrawerContent>
|
||||
</DrawerBackdrop>
|
||||
</Drawer>
|
||||
<Drawer>
|
||||
<Button class="open-b">打开 B</Button>
|
||||
<DrawerBackdrop>
|
||||
<DrawerContent placement="right">
|
||||
<DrawerDialog>
|
||||
<DrawerHeading>抽屉 B</DrawerHeading>
|
||||
</DrawerDialog>
|
||||
</DrawerContent>
|
||||
</DrawerBackdrop>
|
||||
</Drawer>
|
||||
</div>
|
||||
`,
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("button.open-a").trigger("click");
|
||||
await flushPromises();
|
||||
await w.get("button.open-b").trigger("click");
|
||||
await flushPromises();
|
||||
const dialogs = document.querySelectorAll('[data-slot="drawer-dialog"]');
|
||||
expect(dialogs.length).toBe(2);
|
||||
const ids = [...dialogs].map((d) => d.getAttribute("aria-labelledby"));
|
||||
expect(new Set(ids).size).toBe(2);
|
||||
for (const id of ids) {
|
||||
expect(id && document.getElementById(id)).toBeTruthy();
|
||||
}
|
||||
});
|
||||
|
||||
it("outside click closes when isDismissable and not when disabled", async () => {
|
||||
const w = view(`<Drawer><Button>打开</Button>${panel}</Drawer>`);
|
||||
await w.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).not.toBeNull();
|
||||
// Click the backdrop (outside the dialog panel).
|
||||
const backdrop = document.querySelector("[data-slot=drawer-backdrop]");
|
||||
backdrop!.dispatchEvent(
|
||||
new MouseEvent("pointerdown", { bubbles: true, cancelable: true }),
|
||||
);
|
||||
backdrop!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).toBeNull();
|
||||
|
||||
// Non-dismissable: outside click keeps the panel.
|
||||
const w2 = view(
|
||||
`<Drawer><Button>打开</Button><DrawerBackdrop :is-dismissable="false"><DrawerContent><DrawerDialog><DrawerHeading>不可关</DrawerHeading></DrawerDialog></DrawerContent></DrawerBackdrop></Drawer>`,
|
||||
);
|
||||
await w2.get("button").trigger("click");
|
||||
await flushPromises();
|
||||
const backdrop2 = document.querySelector("[data-slot=drawer-backdrop]")!;
|
||||
backdrop2.dispatchEvent(
|
||||
new MouseEvent("pointerdown", { bubbles: true, cancelable: true }),
|
||||
);
|
||||
backdrop2.dispatchEvent(new MouseEvent("click", { bubbles: true }));
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(document.querySelector('[data-slot="drawer-dialog"]')).not.toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h, nextTick } from "vue";
|
||||
|
||||
const demos = import.meta.glob(
|
||||
"../apps/docs/demos/{alert-dialog,drawer}-*.vue",
|
||||
);
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(async () => {
|
||||
wrappers.splice(0).forEach((w) => w.unmount());
|
||||
await flushPromises();
|
||||
vi.restoreAllMocks();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
it("all 23 overlay demos coexist in one Vue app without duplicate IDs or warnings", async () => {
|
||||
const entries = await Promise.all(
|
||||
Object.values(demos).map(
|
||||
async (load) =>
|
||||
((await load()) as { default: Parameters<typeof h>[0] }).default,
|
||||
),
|
||||
);
|
||||
expect(entries).toHaveLength(23);
|
||||
const warnings = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(
|
||||
"main",
|
||||
entries.map((demo, i) => h("section", { "data-demo": i }, [h(demo)])),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(wrapper);
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(wrapper.findAll("section[data-demo]")).toHaveLength(23);
|
||||
expect(wrapper.findAll("button").length).toBeGreaterThan(23);
|
||||
const ids = [...document.querySelectorAll("[id]")].map((el) => el.id);
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
expect(warnings).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -0,0 +1,114 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, h, nextTick, ref } from "vue";
|
||||
import { useDrawerDrag } from "../packages/vue/src/components/drawer/useDrawerDrag";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => wrappers.splice(0).forEach((w) => w.unmount()));
|
||||
async function gesture() {
|
||||
const open = ref(true);
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
const { dialogRef } = useDrawerDrag(
|
||||
ref("bottom"),
|
||||
ref(true),
|
||||
open,
|
||||
() => {
|
||||
open.value = false;
|
||||
},
|
||||
);
|
||||
// Keep the node mounted to model Presence retaining it through exit.
|
||||
return () => h("div", { ref: dialogRef, "data-slot": "drawer-dialog" });
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(wrapper);
|
||||
await nextTick();
|
||||
const el = wrapper.element as HTMLElement;
|
||||
Object.defineProperty(el, "offsetHeight", { value: 200 });
|
||||
function pointer(type: string, clientY: number) {
|
||||
const event = new MouseEvent(type, { bubbles: true, button: 0, clientY });
|
||||
Object.defineProperty(event, "pointerId", { value: 1 });
|
||||
el.dispatchEvent(event);
|
||||
}
|
||||
return { el, open, pointer };
|
||||
}
|
||||
it("normal lost capture after an accepted drag-close preserves exit offset", async () => {
|
||||
const { el, open, pointer } = await gesture();
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 180);
|
||||
pointer("pointerup", 180);
|
||||
await nextTick();
|
||||
expect(open.value).toBe(false);
|
||||
expect(el.style.transform).toBe("translateY(80px)");
|
||||
pointer("lostpointercapture", 180);
|
||||
expect(el.style.transform).toBe("translateY(80px)");
|
||||
});
|
||||
it("programmatic close prevents retained exit node from continuing to drag", async () => {
|
||||
const { el, open, pointer } = await gesture();
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 120);
|
||||
open.value = false;
|
||||
await nextTick();
|
||||
const closingTransform = el.style.transform;
|
||||
pointer("pointermove", 180);
|
||||
expect(el.style.transform).toBe(closingTransform);
|
||||
});
|
||||
|
||||
it("reopening a retained exit node clears the old drag offset", async () => {
|
||||
const { el, open, pointer } = await gesture();
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 180);
|
||||
pointer("pointerup", 180);
|
||||
await nextTick();
|
||||
expect(el.style.transform).toBe("translateY(80px)");
|
||||
open.value = true;
|
||||
await nextTick();
|
||||
expect(el.style.transform).toBe("");
|
||||
pointer("lostpointercapture", 180);
|
||||
expect(el.style.transform).toBe("");
|
||||
});
|
||||
it("replacing the dialog releases and clears the old node, leaving the new node intact", async () => {
|
||||
let replace!: (el: HTMLElement) => void;
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
setup() {
|
||||
const { dialogRef } = useDrawerDrag(
|
||||
ref("bottom"),
|
||||
ref(true),
|
||||
ref(true),
|
||||
() => {},
|
||||
);
|
||||
replace = (el) => {
|
||||
dialogRef.value = el;
|
||||
};
|
||||
return () => h("div", { ref: dialogRef, "data-slot": "drawer-dialog" });
|
||||
},
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(wrapper);
|
||||
await nextTick();
|
||||
const old = wrapper.element as HTMLElement;
|
||||
const release = vi.fn();
|
||||
Object.defineProperty(old, "setPointerCapture", { value: vi.fn() });
|
||||
Object.defineProperty(old, "releasePointerCapture", { value: release });
|
||||
for (const [type, clientY] of [
|
||||
["pointerdown", 100],
|
||||
["pointermove", 120],
|
||||
] as const) {
|
||||
const e = new MouseEvent(type, { bubbles: true, button: 0, clientY });
|
||||
Object.defineProperty(e, "pointerId", { value: 7 });
|
||||
old.dispatchEvent(e);
|
||||
}
|
||||
expect(old.style.transform).toBe("translateY(20px)");
|
||||
const next = document.createElement("div");
|
||||
next.style.transform = "scale(1.1)";
|
||||
replace(next);
|
||||
await nextTick();
|
||||
expect(release).toHaveBeenCalledWith(7);
|
||||
expect(old.style.transform).toBe("");
|
||||
expect(next.style.transform).toBe("scale(1.1)");
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, nextTick, ref } from "vue";
|
||||
import {
|
||||
Drawer,
|
||||
DrawerBackdrop,
|
||||
DrawerContent,
|
||||
DrawerDialog,
|
||||
DrawerHeading,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(async () => {
|
||||
wrappers.splice(0).forEach((w) => w.unmount());
|
||||
await flushPromises();
|
||||
vi.restoreAllMocks();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
async function panel(controlled = false, preventDrag = false) {
|
||||
const dismissable = ref(true);
|
||||
const placement = ref<"bottom" | "left">("bottom");
|
||||
const requests = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
Drawer,
|
||||
DrawerBackdrop,
|
||||
DrawerContent,
|
||||
DrawerDialog,
|
||||
DrawerHeading,
|
||||
},
|
||||
setup: () => ({
|
||||
dismissable,
|
||||
placement,
|
||||
requests,
|
||||
controlled,
|
||||
preventDrag,
|
||||
}),
|
||||
template: `<Drawer :open="controlled ? true : undefined" :default-open="true" @update:open="requests"><DrawerBackdrop :is-dismissable="dismissable"><DrawerContent :placement="placement"><DrawerDialog @pointerdown.capture="preventDrag && $event.preventDefault()"><DrawerHeading>Review drawer</DrawerHeading></DrawerDialog></DrawerContent></DrawerBackdrop></Drawer>`,
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
await flushPromises();
|
||||
const el = document.querySelector<HTMLElement>(
|
||||
'[data-slot="drawer-dialog"]',
|
||||
)!;
|
||||
Object.defineProperty(el, "offsetHeight", { configurable: true, value: 200 });
|
||||
Object.defineProperty(el, "offsetWidth", { configurable: true, value: 200 });
|
||||
let now = 1000;
|
||||
vi.spyOn(Date, "now").mockImplementation(() => now);
|
||||
function pointer(type: string, y: number, pointerId = 1) {
|
||||
now += 100;
|
||||
const e = new MouseEvent(type, {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
button: 0,
|
||||
clientY: y,
|
||||
});
|
||||
Object.defineProperty(e, "pointerId", { value: pointerId });
|
||||
el.dispatchEvent(e);
|
||||
}
|
||||
return { el, pointer, dismissable, placement, requests };
|
||||
}
|
||||
|
||||
it("controlled rejected drag close restores the still-open panel", async () => {
|
||||
const { el, pointer, requests } = await panel(true);
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 180);
|
||||
pointer("pointerup", 180);
|
||||
await flushPromises();
|
||||
expect(requests).toHaveBeenCalledWith(false);
|
||||
expect(el.isConnected).toBe(true);
|
||||
expect(el.style.transform).toBe("");
|
||||
expect(el.style.transition).not.toBe("none");
|
||||
});
|
||||
it("turning dismissable off during a drag cancels it without a close request", async () => {
|
||||
const { el, pointer, dismissable, requests } = await panel(true);
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 180);
|
||||
dismissable.value = false;
|
||||
await nextTick();
|
||||
pointer("pointerup", 180);
|
||||
await flushPromises();
|
||||
expect(requests).not.toHaveBeenCalled();
|
||||
expect(el.style.transform).toBe("");
|
||||
});
|
||||
it("placement change cancels the old-axis drag", async () => {
|
||||
const { el, pointer, placement } = await panel(true);
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 120);
|
||||
placement.value = "left";
|
||||
await nextTick();
|
||||
expect(el.style.transform).toBe("");
|
||||
expect(el.style.transition).not.toBe("none");
|
||||
});
|
||||
it("another pointer cannot move or finish the owning pointer gesture", async () => {
|
||||
const { el, pointer, requests } = await panel(true);
|
||||
pointer("pointerdown", 100, 1);
|
||||
pointer("pointermove", 180, 2);
|
||||
pointer("pointerup", 180, 2);
|
||||
await flushPromises();
|
||||
expect(requests).not.toHaveBeenCalled();
|
||||
expect(el.style.transform).toBe("");
|
||||
});
|
||||
|
||||
it("caller cancellation prevents starting the drag", async () => {
|
||||
const { el, pointer, requests } = await panel(true, true);
|
||||
pointer("pointerdown", 100);
|
||||
pointer("pointermove", 180);
|
||||
pointer("pointerup", 180);
|
||||
await flushPromises();
|
||||
expect(requests).not.toHaveBeenCalled();
|
||||
expect(el.style.transform).toBe("");
|
||||
});
|
||||
@@ -17,3 +17,5 @@ SLIDER-01 已在 `/private/tmp/heroui-slider-consumer` 完成上述检查,临
|
||||
M30–M32 增加 consume-m30-m32.mjs 和 m30-m32-contract.ts。将它们复制到同一个独立消费工程,执行运行脚本,并将契约文件加入上述两种 tsc 命令。合并后的产物还应同时通过原 NumberField/Slider 消费检查,避免只验证新加组件。
|
||||
|
||||
LISTBOX-01 增加 `consume-list-box.mjs` / `list-box-contract.ts`。同样复制到独立消费者,用两份新 tarball 安装后运行脚本,并在 NodeNext / bundler 两种模式执行契约文件。Root 的泛型 Props 曾在 Parameters 提取后退化,非法模型/模式反例与事件参数推断必须保留。
|
||||
|
||||
M33–M34 使用 consume-m33-m34.mjs / m33-m34-contract.ts;同样复制到独立 tarball 消费工程,分别运行 node 与两种解析模式的 tsc。包括所有公开部件、真正 node SSR、CSS 关键帧与策略/尺寸/方向的正反类型检查。集成验证目录为 /private/tmp/heroui-m33-consumer,不复用其他并行任务的临时产物。
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createSSRApp, h } from "vue";
|
||||
import { renderToString } from "@vue/server-renderer";
|
||||
import * as library from "@heroui-vue/components";
|
||||
for (const name of [
|
||||
"AlertDialog",
|
||||
"AlertDialogRoot",
|
||||
"AlertDialogTrigger",
|
||||
"AlertDialogBackdrop",
|
||||
"AlertDialogContainer",
|
||||
"AlertDialogDialog",
|
||||
"AlertDialogHeader",
|
||||
"AlertDialogHeading",
|
||||
"AlertDialogBody",
|
||||
"AlertDialogFooter",
|
||||
"AlertDialogIcon",
|
||||
"AlertDialogCloseTrigger",
|
||||
"AlertDialogClose",
|
||||
"Drawer",
|
||||
"DrawerRoot",
|
||||
"DrawerTrigger",
|
||||
"DrawerBackdrop",
|
||||
"DrawerContent",
|
||||
"DrawerDialog",
|
||||
"DrawerHeader",
|
||||
"DrawerHeading",
|
||||
"DrawerBody",
|
||||
"DrawerFooter",
|
||||
"DrawerHandle",
|
||||
"DrawerCloseTrigger",
|
||||
"DrawerClose",
|
||||
])
|
||||
assert(library[name], name);
|
||||
for (const family of ["AlertDialog", "Drawer"]) {
|
||||
const html = await renderToString(
|
||||
createSSRApp({
|
||||
render: () =>
|
||||
h(library[family], {}, () => [
|
||||
h(
|
||||
library[`${family}Trigger`],
|
||||
{ as: "button" },
|
||||
() => `Open ${family}`,
|
||||
),
|
||||
h(library[`${family}Backdrop`], {}, () =>
|
||||
family === "Drawer"
|
||||
? h(library.DrawerContent)
|
||||
: h(library.AlertDialogContainer),
|
||||
),
|
||||
]),
|
||||
}),
|
||||
);
|
||||
assert(html.includes(`Open ${family}`));
|
||||
assert(html.includes('aria-expanded="false"'));
|
||||
}
|
||||
const css = readFileSync(
|
||||
new URL(import.meta.resolve("@heroui-vue/styles/css")),
|
||||
"utf8",
|
||||
);
|
||||
assert(css.includes(".hv-alert-dialog__dialog"));
|
||||
assert(css.includes("hv-drawer-in-from-right"));
|
||||
assert(css.includes("hv-drawer-out-to-bottom"));
|
||||
console.log("M33-M34 tarball ESM exports, node SSR and CSS passed");
|
||||
@@ -0,0 +1,40 @@
|
||||
import {
|
||||
type AlertDialogContainerProps,
|
||||
type AlertDialogIconProps,
|
||||
type AlertDialogBackdropProps,
|
||||
type DrawerContentProps,
|
||||
type DrawerBackdropProps,
|
||||
DrawerHandle,
|
||||
AlertDialog,
|
||||
} from "@heroui-vue/components";
|
||||
const size: AlertDialogContainerProps = { size: "cover", placement: "bottom" };
|
||||
const status: AlertDialogIconProps = { status: "danger" };
|
||||
const backdrop: AlertDialogBackdropProps = {
|
||||
isDismissable: false,
|
||||
isKeyboardDismissDisabled: true,
|
||||
};
|
||||
const drawer: DrawerContentProps = { placement: "left" };
|
||||
const state: DrawerBackdropProps = {
|
||||
open: true,
|
||||
"onUpdate:open": (v: boolean) => {
|
||||
void v;
|
||||
},
|
||||
};
|
||||
// @ts-expect-error placement is constrained
|
||||
const invalidDirection: DrawerContentProps = { placement: "diagonal" };
|
||||
// @ts-expect-error size is constrained
|
||||
const invalidSize: AlertDialogContainerProps = { size: "giant" };
|
||||
// @ts-expect-error dismiss policy must remain Boolean
|
||||
const invalidPolicy: AlertDialogBackdropProps = { isDismissable: "false" };
|
||||
void [
|
||||
size,
|
||||
status,
|
||||
backdrop,
|
||||
drawer,
|
||||
state,
|
||||
invalidDirection,
|
||||
invalidSize,
|
||||
invalidPolicy,
|
||||
DrawerHandle,
|
||||
AlertDialog,
|
||||
];
|
||||
Reference in New Issue
Block a user