fix: handle ColorSwatchPicker activation lifecycle and dynamic focus

This commit is contained in:
maoyingjie
2026-10-27 15:23:24 +08:00
parent 980930ed76
commit adca8d0047
3 changed files with 137 additions and 15 deletions
+68
View File
@@ -786,6 +786,12 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => {
items(w)[1]!.element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 2 }));
await w.vm.$nextTick();
expect(w.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
// Middle-button counter-example: never a selection gesture either.
items(w)[1]!.element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 1 }));
await w.vm.$nextTick();
expect(w.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
expect(items(w)[1]!.attributes("aria-selected")).toBe("false");
expect(items(w)[1]!.attributes("data-pressed")).toBeUndefined();
});
it("clears pressed state after the primary mouse release", async () => {
const w = picker();
@@ -793,6 +799,25 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => {
items(w)[0]!.element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, cancelable: true, button: 0 }));
await w.vm.$nextTick();
expect(items(w)[0]!.attributes("data-pressed")).toBeUndefined();
// A release elsewhere inside the listbox clears the state as well.
press(items(w)[1]!);
w.get("[data-slot=color-swatch-picker]").element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, cancelable: true, button: 0 }));
await w.vm.$nextTick();
expect(items(w)[1]!.attributes("data-pressed")).toBeUndefined();
// Dragging out cancels the press before any release.
press(items(w)[2]!);
items(w)[2]!.trigger("mouseleave");
await w.vm.$nextTick();
expect(items(w)[2]!.attributes("data-pressed")).toBeUndefined();
// A press that steals focus from another item keeps its own press state.
items(w)[1]!.element.focus();
await w.vm.$nextTick();
press(items(w)[0]!);
await w.vm.$nextTick();
expect(items(w)[0]!.attributes("data-pressed")).toBe("true");
items(w)[0]!.element.dispatchEvent(new MouseEvent("mouseup", { bubbles: true, cancelable: true, button: 0 }));
await w.vm.$nextTick();
expect(items(w)[0]!.attributes("data-pressed")).toBeUndefined();
});
it("keeps an enabled tab entry after dynamically disabling the focused item", async () => {
const off = shallowRef(false);
@@ -809,6 +834,31 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => {
const root = w.get("[data-slot=color-swatch-picker]");
const enabledTabEntries = [root, ...items(w)].filter(node => node.attributes("tabindex") === "0" && node.attributes("aria-disabled") !== "true");
expect(enabledTabEntries).toHaveLength(1);
// Focus is not lost to the document while the entry is rebuilt.
expect(document.activeElement).not.toBe(document.body);
// Re-enabling the item keeps a single legal entry again.
off.value = false;
await w.vm.$nextTick();
await w.vm.$nextTick();
expect([root, ...items(w)].filter(node => node.attributes("tabindex") === "0" && node.attributes("aria-disabled") !== "true")).toHaveLength(1);
});
it("keeps legal tab entries for all-disabled items and a disabled root", async () => {
const off = shallowRef(false);
const w = render(defineComponent({
components: { ColorSwatchPicker, ColorSwatchPickerItem },
setup: () => ({ off }),
template: `<ColorSwatchPicker :is-disabled="off"><ColorSwatchPickerItem color="#F43F5E" is-disabled/><ColorSwatchPickerItem color="#D946EF" is-disabled/></ColorSwatchPicker>`,
}));
const root = w.get("[data-slot=color-swatch-picker]");
// Every option disabled: the listbox itself stays the single entry.
expect([root, ...items(w)].filter(node => node.attributes("tabindex") === "0" && node.attributes("aria-disabled") !== "true")).toHaveLength(1);
// A disabled root leaves the tab order entirely, then restores one entry.
off.value = true;
await w.vm.$nextTick();
expect([root, ...items(w)].filter(node => node.attributes("tabindex") === "0")).toHaveLength(0);
off.value = false;
await w.vm.$nextTick();
expect([root, ...items(w)].filter(node => node.attributes("tabindex") === "0" && node.attributes("aria-disabled") !== "true")).toHaveLength(1);
});
it("supports click-only activation used by assistive technology", async () => {
const w = picker();
@@ -816,4 +866,22 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => {
await w.vm.$nextTick();
expect(items(w)[1]!.attributes("aria-selected")).toBe("true");
});
it("keeps press ownership so normal clicks never re-propose", async () => {
const w = picker();
const root = w.getComponent(ColorSwatchPicker);
press(items(w)[1]!);
// The compatibility click of the press does not emit a second proposal.
items(w)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 1 }));
await w.vm.$nextTick();
expect(root.emitted("update:modelValue")).toHaveLength(1);
// Click-only activation stays cancelable and skips disabled items.
const cancelled = picker({}, COLORS, { onClick: (event: MouseEvent) => event.preventDefault() });
items(cancelled)[0]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
await cancelled.vm.$nextTick();
expect(cancelled.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
const disabled = picker({}, COLORS, (_c: string, index: number) => ({ isDisabled: index === 1 }));
items(disabled)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
await disabled.vm.$nextTick();
expect(disabled.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
});
});