feat: migrate Toolbar with teleport-safe focus ownership
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, nextTick, ref } from "vue";
|
||||
import {
|
||||
Button,
|
||||
Toolbar,
|
||||
ToggleButton,
|
||||
ToggleButtonGroup,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => {
|
||||
wrappers.splice(0).forEach((wrapper) => wrapper.unmount());
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
function fixture(template: string, setup = () => ({})) {
|
||||
const wrapper = mount(
|
||||
defineComponent({
|
||||
components: { Button, Toolbar, ToggleButton, ToggleButtonGroup },
|
||||
template,
|
||||
setup,
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(wrapper);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
it("standalone Button preserves the caller tabindex after Toolbar integration", async () => {
|
||||
const wrapper = fixture('<Button :tabindex="-1">Excluded</Button>');
|
||||
await nextTick();
|
||||
expect(wrapper.get("button").attributes("tabindex")).toBe("-1");
|
||||
});
|
||||
|
||||
it("standalone asChild buttons preserve tabindex supplied by their native child", async () => {
|
||||
const wrapper = fixture(
|
||||
'<div><Button as-child><button tabindex="-1">Button child</button></Button><ToggleButton as-child><button tabindex="-1">Toggle child</button></ToggleButton></div>',
|
||||
);
|
||||
await nextTick();
|
||||
expect(
|
||||
wrapper.findAll("button").map((button) => button.attributes("tabindex")),
|
||||
).toEqual(["-1", "-1"]);
|
||||
});
|
||||
|
||||
it("moving an existing Button through Teleport restores its own tabindex", async () => {
|
||||
const inline = ref(true);
|
||||
const wrapper = fixture(
|
||||
'<Toolbar><Button>A</Button><Teleport to="body" :disabled="inline"><Button data-outside tabindex="3">Moving</Button></Teleport></Toolbar>',
|
||||
() => ({ inline }),
|
||||
);
|
||||
await nextTick();
|
||||
const moving = document.querySelector("[data-outside]") as HTMLButtonElement;
|
||||
expect(moving.getAttribute("tabindex")).toBe("-1");
|
||||
inline.value = false;
|
||||
await nextTick();
|
||||
expect(wrapper.element.contains(moving)).toBe(false);
|
||||
expect(moving.getAttribute("tabindex")).toBe("3");
|
||||
});
|
||||
|
||||
it("moving an existing group outside Toolbar restores its arrow navigation", async () => {
|
||||
const inline = ref(true);
|
||||
fixture(
|
||||
'<Toolbar><Button>A</Button><Teleport to="body" :disabled="inline"><ToggleButtonGroup data-moving><ToggleButton value="x">X</ToggleButton><ToggleButton value="y">Y</ToggleButton></ToggleButtonGroup></Teleport></Toolbar>',
|
||||
() => ({ inline }),
|
||||
);
|
||||
await nextTick();
|
||||
inline.value = false;
|
||||
await nextTick();
|
||||
const buttons = document.querySelectorAll<HTMLButtonElement>(
|
||||
"[data-moving] button",
|
||||
);
|
||||
buttons[0].focus();
|
||||
buttons[0].dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
expect(document.activeElement).toBe(buttons[1]);
|
||||
});
|
||||
|
||||
it("Toolbar root keydown handler is preserved and can cancel navigation", async () => {
|
||||
const handler = vi.fn((event: KeyboardEvent) => event.preventDefault());
|
||||
const wrapper = fixture(
|
||||
'<Toolbar @keydown="handler"><Button>A</Button><Button>B</Button></Toolbar>',
|
||||
() => ({ handler }),
|
||||
);
|
||||
await nextTick();
|
||||
const buttons = wrapper.findAll("button");
|
||||
(buttons[0].element as HTMLElement).focus();
|
||||
await buttons[0].trigger("keydown", { key: "ArrowRight" });
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
expect(document.activeElement).toBe(buttons[0].element);
|
||||
});
|
||||
|
||||
it("Toolbar navigation excludes controls teleported outside its DOM", async () => {
|
||||
const wrapper = fixture(
|
||||
'<Toolbar><Button>A</Button><Button>B</Button><Teleport to="body"><Button>Outside</Button></Teleport></Toolbar>',
|
||||
);
|
||||
await nextTick();
|
||||
const buttons = wrapper.findAll("button");
|
||||
expect(buttons).toHaveLength(2);
|
||||
(buttons[1].element as HTMLElement).focus();
|
||||
await buttons[1].trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[0].element);
|
||||
const outside = [...document.querySelectorAll("button")].find(
|
||||
(button) => button.textContent === "Outside",
|
||||
);
|
||||
expect(outside?.hasAttribute("tabindex")).toBe(false);
|
||||
});
|
||||
@@ -530,3 +530,40 @@ it("keeps standalone ToggleButton DOM ids stable on the server", async () => {
|
||||
);
|
||||
expect(html).toContain('id="like-toggle"');
|
||||
});
|
||||
|
||||
it("renders Toolbar with nested groups without browser globals", async () => {
|
||||
const {
|
||||
Toolbar,
|
||||
Button,
|
||||
ButtonGroup,
|
||||
Separator,
|
||||
ToggleButton,
|
||||
ToggleButtonGroup,
|
||||
} = await import("../packages/vue/src");
|
||||
const html = await renderToString(
|
||||
createSSRApp({
|
||||
render: () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{
|
||||
"aria-label": "文本格式",
|
||||
orientation: "vertical",
|
||||
isAttached: true,
|
||||
},
|
||||
() => [
|
||||
h(ToggleButtonGroup, { selectionMode: "multiple" }, () => [
|
||||
h(ToggleButton, { id: "bold" }, () => "Bold"),
|
||||
]),
|
||||
h(Separator),
|
||||
h(ButtonGroup, () => h(Button, () => "Copy")),
|
||||
],
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(html).toContain('role="toolbar"');
|
||||
expect(html).toContain('aria-orientation="vertical"');
|
||||
expect(html).toContain("hv-toolbar--attached");
|
||||
expect(html).toContain('data-slot="toggle-button-group"');
|
||||
expect(html).toContain("hv-separator--horizontal");
|
||||
expect(html).toContain('data-slot="button-group"');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,715 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { Teleport, defineComponent, h, nextTick, ref } from "vue";
|
||||
import { readFileSync } from "node:fs";
|
||||
import {
|
||||
Button,
|
||||
ButtonGroup,
|
||||
ButtonGroupSeparator,
|
||||
Separator,
|
||||
ToggleButton,
|
||||
ToggleButtonGroup,
|
||||
ToggleButtonGroupSeparator,
|
||||
Toolbar,
|
||||
} from "../packages/vue/src";
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
function mountTo<T>(
|
||||
comp: T,
|
||||
props: Record<string, unknown> = {},
|
||||
setup?: () => Record<string, unknown>,
|
||||
) {
|
||||
return mount(
|
||||
defineComponent({
|
||||
components: {
|
||||
Button,
|
||||
ButtonGroup,
|
||||
ButtonGroupSeparator,
|
||||
Separator,
|
||||
ToggleButton,
|
||||
ToggleButtonGroup,
|
||||
ToggleButtonGroupSeparator,
|
||||
Toolbar,
|
||||
},
|
||||
setup: setup ?? (() => ({})),
|
||||
...comp,
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
}
|
||||
|
||||
describe("Toolbar basics", () => {
|
||||
it("renders role, data-slot, aria-orientation and classes", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "文本格式" }, () => [
|
||||
h(Button, () => "One"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
const root = w.get('[role="toolbar"]');
|
||||
expect(root.attributes("data-slot")).toBe("toolbar");
|
||||
expect(root.attributes("aria-label")).toBe("文本格式");
|
||||
expect(root.attributes("aria-orientation")).toBe("horizontal");
|
||||
expect(root.classes().join(" ")).toContain("hv-toolbar");
|
||||
expect(root.classes().join(" ")).toContain("hv-toolbar--horizontal");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("exposes vertical and attached modifiers", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{ orientation: "vertical", isAttached: true, "aria-label": "工具" },
|
||||
() => [h(Button, () => "One")],
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
const classes = w.get('[role="toolbar"]').classes().join(" ");
|
||||
expect(classes).toContain("hv-toolbar--vertical");
|
||||
expect(classes).toContain("hv-toolbar--attached");
|
||||
expect(w.get('[role="toolbar"]').attributes("aria-orientation")).toBe(
|
||||
"vertical",
|
||||
);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("passes orientation to the scoped slot and merges attrs/class", async () => {
|
||||
const seen: string[] = [];
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{ orientation: "vertical", class: "extra", "data-testid": "bar" },
|
||||
{
|
||||
default: (scope: { orientation: string }) => {
|
||||
seen.push(scope.orientation);
|
||||
return [h(Button, () => "One")];
|
||||
},
|
||||
},
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
expect(seen).toEqual(["vertical"]);
|
||||
const root = w.get('[data-testid="bar"]');
|
||||
expect(root.classes().join(" ")).toContain("extra");
|
||||
expect(root.classes().join(" ")).toContain("hv-toolbar--vertical");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("updates class and aria when orientation and isAttached change", async () => {
|
||||
const vertical = ref(false);
|
||||
const attached = ref(false);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{
|
||||
orientation: vertical.value ? "vertical" : "horizontal",
|
||||
isAttached: attached.value,
|
||||
"aria-label": "bar",
|
||||
},
|
||||
() => [h(Button, () => "One")],
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
vertical.value = true;
|
||||
attached.value = true;
|
||||
await nextTick();
|
||||
const root = w.get('[role="toolbar"]');
|
||||
expect(root.attributes("aria-orientation")).toBe("vertical");
|
||||
expect(root.classes().join(" ")).toContain("hv-toolbar--vertical");
|
||||
expect(root.classes().join(" ")).toContain("hv-toolbar--attached");
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Toolbar keyboard ownership", () => {
|
||||
const withGroups = () =>
|
||||
h(Toolbar, { "aria-label": "编辑器" }, () => [
|
||||
h(ButtonGroup, { variant: "tertiary" }, () => [
|
||||
h(Button, () => "Undo"),
|
||||
h(Button, () => [h(ButtonGroupSeparator), "Redo"]),
|
||||
]),
|
||||
h(Separator),
|
||||
h(
|
||||
ToggleButtonGroup,
|
||||
{ selectionMode: "multiple", "aria-label": "styles" },
|
||||
() => [
|
||||
h(ToggleButton, { id: "bold" }, () => "Bold"),
|
||||
h(ToggleButton, { id: "italic" }, () => [
|
||||
h(ToggleButtonGroupSeparator),
|
||||
"Italic",
|
||||
]),
|
||||
],
|
||||
),
|
||||
]);
|
||||
|
||||
it("moves once per arrow key across ButtonGroup and ToggleButtonGroup", async () => {
|
||||
const w = mount(defineComponent({ render: withGroups }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
expect(buttons).toHaveLength(4);
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[2]!.element);
|
||||
await buttons[2]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[3]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("keeps a single Tab entry across nested groups", async () => {
|
||||
const w = mount(defineComponent({ render: withGroups }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const stops = w
|
||||
.findAll("button")
|
||||
.filter((b) => b.attributes("tabindex") === "0");
|
||||
expect(stops).toHaveLength(1);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("real focus moves the toolbar Tab entry", async () => {
|
||||
const w = mount(defineComponent({ render: withGroups }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[3]!.element as HTMLElement).focus();
|
||||
await nextTick();
|
||||
expect(buttons[3]!.attributes("tabindex")).toBe("0");
|
||||
expect(
|
||||
buttons.filter((b) => b.attributes("tabindex") === "0"),
|
||||
).toHaveLength(1);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("skips disabled members and promotes the next entry", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(Button, { isDisabled: true }, () => "No"),
|
||||
h(Button, () => "Yes"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
expect(buttons[1]!.attributes("tabindex")).toBe("0");
|
||||
(buttons[1]!.element as HTMLElement).focus();
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("reverses horizontal arrows in RTL across three members", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar", dir: "rtl" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Button, () => "B"),
|
||||
h(Button, () => "C"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowLeft" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowLeft" });
|
||||
expect(document.activeElement).toBe(buttons[2]!.element);
|
||||
await buttons[2]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("vertical toolbar uses up/down and ignores horizontal arrows", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar", orientation: "vertical" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Button, () => "B"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[0]!.element);
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowDown" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("respects preventDefault on arrow keys", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(
|
||||
Button,
|
||||
{ onKeydown: (e: KeyboardEvent) => e.preventDefault() },
|
||||
() => "A",
|
||||
),
|
||||
h(Button, () => "B"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[0]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("does not toggle selection when navigating with arrows", async () => {
|
||||
const change = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(
|
||||
ToggleButtonGroup,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
onSelectionChange: change,
|
||||
},
|
||||
() => [
|
||||
h(ToggleButton, { id: "a" }, () => "A"),
|
||||
h(ToggleButton, { id: "b" }, () => "B"),
|
||||
],
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
expect(change).not.toHaveBeenCalled();
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("Home and End move to toolbar edges", async () => {
|
||||
const w = mount(defineComponent({ render: withGroups }), {
|
||||
attachTo: document.body,
|
||||
});
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[1]!.element as HTMLElement).focus();
|
||||
await buttons[1]!.trigger("keydown", { key: "End" });
|
||||
expect(document.activeElement).toBe(buttons[3]!.element);
|
||||
await buttons[3]!.trigger("keydown", { key: "Home" });
|
||||
expect(document.activeElement).toBe(buttons[0]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("nested toolbars isolate arrow navigation", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "outer" }, () => [
|
||||
h(Button, () => "Outer"),
|
||||
h(Toolbar, { "aria-label": "inner" }, () => [
|
||||
h(Button, () => "Inner1"),
|
||||
h(Button, () => "Inner2"),
|
||||
]),
|
||||
h(Button, () => "Outer2"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
// DOM order: Outer, Inner1, Inner2, Outer2
|
||||
(buttons[1]!.element as HTMLElement).focus();
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[2]!.element);
|
||||
await buttons[2]!.trigger("keydown", { key: "ArrowRight" });
|
||||
// stays within inner when looping inner list? Inner has 2 items; next wraps to Inner1
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("keyed reorder changes the next arrow target", async () => {
|
||||
const keys = ref(["a", "b", "c"]);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () =>
|
||||
keys.value.map((key) => h(Button, { key }, () => key)),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
keys.value = ["c", "b", "a"];
|
||||
await nextTick();
|
||||
const buttons = w.findAll("button");
|
||||
expect(buttons.map((b) => b.text())).toEqual(["c", "b", "a"]);
|
||||
(buttons[1]!.element as HTMLElement).focus();
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(buttons[2]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("Enter activates a toggle once and does not double-fire", async () => {
|
||||
const change = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(
|
||||
ToggleButtonGroup,
|
||||
{ selectionMode: "multiple", onSelectionChange: change },
|
||||
() => [h(ToggleButton, { id: "a" }, () => "A")],
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const button = w.get("button");
|
||||
(button.element as HTMLElement).focus();
|
||||
await button.trigger("keydown", { key: "Enter" });
|
||||
await button.trigger("click");
|
||||
expect(change).toHaveBeenCalledTimes(1);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Toolbar orientation inheritance", () => {
|
||||
it("ToggleButtonGroup inherits toolbar orientation and restores after override", async () => {
|
||||
const override = ref<undefined | "horizontal">(undefined);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Toolbar, { orientation: "vertical", "aria-label": "bar" }, () => [
|
||||
h(
|
||||
ToggleButtonGroup,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
orientation: override.value,
|
||||
},
|
||||
() => [h(ToggleButton, { id: "a" }, () => "A")],
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
let group = w.get('[data-slot="toggle-button-group"]');
|
||||
expect(group.attributes("aria-orientation")).toBe("vertical");
|
||||
override.value = "horizontal";
|
||||
await nextTick();
|
||||
group = w.get('[data-slot="toggle-button-group"]');
|
||||
expect(group.attributes("aria-orientation")).toBe("horizontal");
|
||||
override.value = undefined;
|
||||
await nextTick();
|
||||
group = w.get('[data-slot="toggle-button-group"]');
|
||||
expect(group.attributes("aria-orientation")).toBe("vertical");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("Separator defaults to the perpendicular toolbar direction", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { orientation: "horizontal", "aria-label": "bar" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Separator),
|
||||
h(Button, () => "B"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
const sep = w.get('[data-slot="separator"]');
|
||||
expect(sep.classes().join(" ")).toContain("hv-separator--vertical");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("explicit Separator orientation overrides toolbar default", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { orientation: "horizontal", "aria-label": "bar" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Separator, { orientation: "horizontal" }),
|
||||
h(Button, () => "B"),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
const sep = w.get('[data-slot="separator"]');
|
||||
expect(sep.classes().join(" ")).toContain("hv-separator--horizontal");
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Toolbar demos", () => {
|
||||
it("mounts all 5 baseline demos without Vue warnings", async () => {
|
||||
const warnings: string[] = [];
|
||||
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
|
||||
warnings.push(args.map(String).join(" "));
|
||||
});
|
||||
const names = [
|
||||
"basic",
|
||||
"vertical",
|
||||
"attached",
|
||||
"with-button-group",
|
||||
"custom-styles",
|
||||
];
|
||||
for (const name of names) {
|
||||
const mod = await import(`../apps/docs/demos/toolbar-${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("includes every preview in the pinned upstream Toolbar document", () => {
|
||||
const document = readFileSync("apps/docs/components/toolbar.md", "utf8");
|
||||
const names = [
|
||||
...document.matchAll(/<ComponentPreview name="toolbar-([^"]+)"/g),
|
||||
].map((m) => m[1]);
|
||||
expect(names).toEqual([
|
||||
"basic",
|
||||
"vertical",
|
||||
"attached",
|
||||
"with-button-group",
|
||||
"custom-styles",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("M29 review boundaries", () => {
|
||||
it("keeps caller tabindex through add/remove/re-add on standalone Button", async () => {
|
||||
const tab = ref<string | number | undefined>(-1);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () => h(Button, { tabindex: tab.value }, () => "Solo"),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
expect(w.get("button").attributes("tabindex")).toBe("-1");
|
||||
tab.value = undefined;
|
||||
await nextTick();
|
||||
expect(w.get("button").attributes("tabindex")).toBeUndefined();
|
||||
tab.value = 0;
|
||||
await nextTick();
|
||||
expect(w.get("button").attributes("tabindex")).toBe("0");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("runs root keydown handler arrays in order and honors stopImmediatePropagation", async () => {
|
||||
const calls: string[] = [];
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{
|
||||
"aria-label": "bar",
|
||||
onKeydown: [
|
||||
(e: KeyboardEvent) => {
|
||||
calls.push("first");
|
||||
e.stopImmediatePropagation();
|
||||
},
|
||||
() => {
|
||||
calls.push("second");
|
||||
},
|
||||
],
|
||||
},
|
||||
() => [h(Button, () => "A"), h(Button, () => "B")],
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(calls).toEqual(["first"]);
|
||||
expect(document.activeElement).toBe(buttons[0]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("supports removing and re-adding the root keydown handler", async () => {
|
||||
const enable = ref(true);
|
||||
const handler = vi.fn((event: KeyboardEvent) => event.preventDefault());
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
Toolbar,
|
||||
{
|
||||
"aria-label": "bar",
|
||||
onKeydown: enable.value ? handler : undefined,
|
||||
},
|
||||
() => [h(Button, () => "A"), h(Button, () => "B")],
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const buttons = w.findAll("button");
|
||||
(buttons[0]!.element as HTMLElement).focus();
|
||||
enable.value = false;
|
||||
await nextTick();
|
||||
await buttons[0]!.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
enable.value = true;
|
||||
await nextTick();
|
||||
(buttons[1]!.element as HTMLElement).focus();
|
||||
await buttons[1]!.trigger("keydown", { key: "ArrowLeft" });
|
||||
expect(handler).toHaveBeenCalled();
|
||||
expect(document.activeElement).toBe(buttons[1]!.element);
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("restores ToggleButtonGroup navigation after teleporting outside the toolbar", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(ToggleButtonGroup, { selectionMode: "multiple" }, () => [
|
||||
h(ToggleButton, { value: "a" }, () => "A"),
|
||||
h(ToggleButton, { value: "b" }, () => "B"),
|
||||
]),
|
||||
h(Teleport, { to: "body" }, () => [
|
||||
h(
|
||||
ToggleButtonGroup,
|
||||
{ selectionMode: "multiple", "data-testid": "outside" },
|
||||
() => [
|
||||
h(ToggleButton, { value: "x" }, () => "X"),
|
||||
h(ToggleButton, { value: "y" }, () => "Y"),
|
||||
],
|
||||
),
|
||||
]),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
await flushPromises();
|
||||
const outside = document.querySelector('[data-testid="outside"]');
|
||||
expect(outside).toBeTruthy();
|
||||
const outButtons = [
|
||||
...(outside!.querySelectorAll("button") as NodeListOf<HTMLButtonElement>),
|
||||
];
|
||||
expect(outButtons).toHaveLength(2);
|
||||
expect(outButtons[0]!.getAttribute("tabindex")).toBe("0");
|
||||
expect(outButtons[1]!.getAttribute("tabindex")).toBe("-1");
|
||||
outButtons[0]!.focus();
|
||||
await flushPromises();
|
||||
outButtons[0]!.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
expect(document.activeElement).toBe(outButtons[1]);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe("M29 dynamic Teleport ownership", () => {
|
||||
it("restores toolbar ownership when the same node moves back inside", async () => {
|
||||
const inline = ref(false);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Teleport, { to: "body", disabled: inline.value }, () => [
|
||||
h(Button, { tabindex: "3", "data-moving": "" }, () => "Moving"),
|
||||
]),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
const moving = document.querySelector("[data-moving]") as HTMLButtonElement;
|
||||
expect(moving.getAttribute("tabindex")).toBe("3");
|
||||
inline.value = true;
|
||||
await nextTick();
|
||||
expect(moving.getAttribute("tabindex")).toBe("-1");
|
||||
inline.value = false;
|
||||
await nextTick();
|
||||
expect(moving.getAttribute("tabindex")).toBe("3");
|
||||
w.unmount();
|
||||
});
|
||||
|
||||
it("keeps single Tab entry after a toolbar-owned node leaves", async () => {
|
||||
const inline = ref(true);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Toolbar, { "aria-label": "bar" }, () => [
|
||||
h(Button, () => "A"),
|
||||
h(Teleport, { to: "body", disabled: inline.value }, () => [
|
||||
h(Button, () => "Moving"),
|
||||
]),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
await nextTick();
|
||||
inline.value = false;
|
||||
await nextTick();
|
||||
const root = w.get('[role="toolbar"]');
|
||||
const stops = root
|
||||
.findAll("button")
|
||||
.filter((b) => b.attributes("tabindex") === "0");
|
||||
expect(stops).toHaveLength(1);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
@@ -71,3 +71,18 @@ function buttonToggleContract() {
|
||||
};
|
||||
}
|
||||
void buttonToggleContract;
|
||||
|
||||
import type { ToolbarProps } from "../../packages/vue/src";
|
||||
import { Toolbar } from "../../packages/vue/src";
|
||||
|
||||
function toolbarContract() {
|
||||
const ok: ToolbarProps = {
|
||||
isAttached: true,
|
||||
orientation: "vertical",
|
||||
dir: "rtl",
|
||||
};
|
||||
// @ts-expect-error unsupported orientation
|
||||
const bad: ToolbarProps = { orientation: "diagonal" };
|
||||
return { bad, ok, Toolbar };
|
||||
}
|
||||
void toolbarContract;
|
||||
|
||||
Reference in New Issue
Block a user