fix: handle ColorSwatchPicker activation lifecycle and dynamic focus
This commit is contained in:
@@ -92,7 +92,7 @@ const registry = useCollectionRegistry<ColorSwatchPickerMember>();
|
||||
let sequence = 0;
|
||||
const nextKey = () => ++sequence;
|
||||
const ordered = () => registry.ordered();
|
||||
const enabled = () => registry.available();
|
||||
const enabled = () => (state.disabled.value ? [] : registry.available());
|
||||
|
||||
const rootElement = shallowRef<HTMLElement>();
|
||||
const focused = shallowRef<ColorSwatchPickerKey>();
|
||||
@@ -270,17 +270,28 @@ function onRootKeydown(event: KeyboardEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
// A removed or disabled item cannot stay focused; focus falls back to the
|
||||
// listbox entry so it is not lost to the document.
|
||||
// A removed or disabled item cannot stay focused; the listbox keeps one legal
|
||||
// tab entry (the root or a usable item) and never drops focus to the document.
|
||||
watch(
|
||||
() => registry.ordered(),
|
||||
() =>
|
||||
[
|
||||
state.disabled.value,
|
||||
registry
|
||||
.ordered()
|
||||
.map((member) => `${String(member.key())}:${member.disabled()}`)
|
||||
.join(","),
|
||||
].join("|"),
|
||||
() => {
|
||||
if (focused.value === undefined) return;
|
||||
const member = enabled().find((item) => item.key() === focused.value);
|
||||
if (member) return;
|
||||
const usable = (key: ColorSwatchPickerKey | undefined) =>
|
||||
key !== undefined &&
|
||||
enabled().some((item) => item.key() === key && !itemDisabled(key));
|
||||
if (pressed.value !== undefined && !usable(pressed.value))
|
||||
pressed.value = undefined;
|
||||
if (focused.value === undefined || usable(focused.value)) return;
|
||||
focused.value = undefined;
|
||||
if (rootElement.value?.contains(document.activeElement))
|
||||
rootElement.value.focus();
|
||||
focusVisible.value = false;
|
||||
const root = rootElement.value;
|
||||
if (root?.contains(document.activeElement)) root.focus();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -328,6 +339,9 @@ function rootAttrs() {
|
||||
onMousedown: () => {
|
||||
focusVisible.value = false;
|
||||
},
|
||||
onMouseup: (event: MouseEvent) => {
|
||||
if (event.button === 0) pressed.value = undefined;
|
||||
},
|
||||
onKeyupCapture: () => {
|
||||
pressed.value = undefined;
|
||||
},
|
||||
@@ -358,9 +372,10 @@ provide(colorSwatchPickerKey, {
|
||||
focused.value = key;
|
||||
},
|
||||
noteBlur: (key) => {
|
||||
// Press release is owned by the item's mouse/leave handlers: a blur caused
|
||||
// by a new press must not clear the new press state.
|
||||
if (focused.value !== key) return;
|
||||
focusVisible.value = false;
|
||||
pressed.value = undefined;
|
||||
},
|
||||
focusItem,
|
||||
activate,
|
||||
|
||||
@@ -91,16 +91,45 @@ const state = computed<ColorSwatchPickerItemRenderProps>(() => {
|
||||
|
||||
provide(colorSwatchPickerItemKey, { key, state });
|
||||
|
||||
/** The compatibility click of an already activated press must not re-propose. */
|
||||
const pressActivated = shallowRef(false);
|
||||
|
||||
function activateFrom(event: Event) {
|
||||
picker.activate(key, event);
|
||||
element.value?.focus({ preventScroll: true });
|
||||
picker.noteFocus(key);
|
||||
}
|
||||
|
||||
function onMousedown(event: MouseEvent) {
|
||||
// Only the primary button activates; secondary buttons keep their native
|
||||
// behaviour (context menu, autoscroll).
|
||||
if (event.button !== 0) return;
|
||||
pressActivated.value = true;
|
||||
if (state.value.isDisabled) {
|
||||
// Keep focus from moving onto a disabled option.
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
picker.pressed.value = key;
|
||||
picker.activate(key, event);
|
||||
element.value?.focus({ preventScroll: true });
|
||||
picker.noteFocus(key);
|
||||
activateFrom(event);
|
||||
}
|
||||
|
||||
function onClick(event: MouseEvent) {
|
||||
// Real pointer clicks already activated on press (detail > 0). Click-only
|
||||
// activations from assistive technology arrive without a press and with
|
||||
// detail 0, and still honour cancelable clicks and rejection retries.
|
||||
if (pressActivated.value) {
|
||||
pressActivated.value = false;
|
||||
return;
|
||||
}
|
||||
if (event.detail > 0) return;
|
||||
if (state.value.isDisabled) return;
|
||||
picker.pressed.value = undefined;
|
||||
activateFrom(event);
|
||||
}
|
||||
|
||||
function releasePress() {
|
||||
if (picker.pressed.value === key) picker.pressed.value = undefined;
|
||||
}
|
||||
|
||||
function itemAttrs() {
|
||||
@@ -136,14 +165,24 @@ function itemAttrs() {
|
||||
: ((node as { $el?: HTMLElement } | null)?.$el ?? undefined);
|
||||
},
|
||||
onFocus: () => picker.noteFocus(key),
|
||||
onBlur: () => picker.noteBlur(key),
|
||||
onBlur: () => {
|
||||
pressActivated.value = false;
|
||||
releasePress();
|
||||
picker.noteBlur(key);
|
||||
},
|
||||
onMousedown,
|
||||
onClick,
|
||||
onMouseup: (event: MouseEvent) => {
|
||||
if (event.button !== 0) return;
|
||||
releasePress();
|
||||
},
|
||||
onMouseenter: () => {
|
||||
hovered.value = true;
|
||||
},
|
||||
onMouseleave: () => {
|
||||
hovered.value = false;
|
||||
picker.pressed.value = undefined;
|
||||
pressActivated.value = false;
|
||||
releasePress();
|
||||
},
|
||||
} as Record<string, unknown>);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user