fix: cancel swatch picker press ownership on window blur
This commit is contained in:
@@ -249,7 +249,7 @@ Root 实例通过 `defineExpose` 提供 `element`(真实根 DOM)与 `focus()
|
|||||||
| 输入字符 | 按项名称前缀匹配并聚焦(名称差异见下) |
|
| 输入字符 | 按项名称前缀匹配并聚焦(名称差异见下) |
|
||||||
| 边界 | 不循环;到边界后保持当前项 |
|
| 边界 | 不循环;到边界后保持当前项 |
|
||||||
|
|
||||||
焦点与选择分离:方向键只移动焦点,`aria-selected` 与指示器只反映接受颜色。父级拒绝提案后,选择样式、`aria-selected` 与 DOM 保持接受状态,同一手势可重试。
|
焦点与选择分离:方向键只移动焦点,`aria-selected` 与指示器只反映接受颜色。父级拒绝提案后,选择样式、`aria-selected` 与 DOM 保持接受状态,同一手势可重试。窗口失焦或指针取消会清除按压状态与点击去重归属,恢复后可以立即重试。
|
||||||
|
|
||||||
### 可访问名称差异
|
### 可访问名称差异
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
useAttrs,
|
useAttrs,
|
||||||
watch,
|
watch,
|
||||||
} from "vue";
|
} from "vue";
|
||||||
|
import { useEventListener } from "@vueuse/core";
|
||||||
import { colorSwatchPickerVariants } from "@heroui-vue/styles";
|
import { colorSwatchPickerVariants } from "@heroui-vue/styles";
|
||||||
import { useConfig } from "../../providers/context";
|
import { useConfig } from "../../providers/context";
|
||||||
import { useColorState } from "../color/model";
|
import { useColorState } from "../color/model";
|
||||||
@@ -100,6 +101,19 @@ const within = shallowRef(false);
|
|||||||
const focusVisible = shallowRef(false);
|
const focusVisible = shallowRef(false);
|
||||||
const pressed = shallowRef<ColorSwatchPickerKey>();
|
const pressed = shallowRef<ColorSwatchPickerKey>();
|
||||||
|
|
||||||
|
function cancelPress() {
|
||||||
|
pressed.value = undefined;
|
||||||
|
for (const member of ordered()) member.cancelPress();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Window blur need not blur the active element. Cancel both visual press state
|
||||||
|
// and click suppression at the owner; VueUse removes the listener on unmount.
|
||||||
|
useEventListener(
|
||||||
|
() => rootElement.value?.ownerDocument.defaultView,
|
||||||
|
"blur",
|
||||||
|
cancelPress,
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selection compares derived hexa keys like the fixed source. The accepted
|
* Selection compares derived hexa keys like the fixed source. The accepted
|
||||||
* value keeps its own space and precision and is never rewritten to match.
|
* value keeps its own space and precision and is never rewritten to match.
|
||||||
@@ -282,6 +296,9 @@ watch(
|
|||||||
.join(","),
|
.join(","),
|
||||||
].join("|"),
|
].join("|"),
|
||||||
() => {
|
() => {
|
||||||
|
for (const member of ordered()) {
|
||||||
|
if (state.disabled.value || member.disabled()) member.cancelPress();
|
||||||
|
}
|
||||||
const usable = (key: ColorSwatchPickerKey | undefined) =>
|
const usable = (key: ColorSwatchPickerKey | undefined) =>
|
||||||
key !== undefined &&
|
key !== undefined &&
|
||||||
enabled().some((item) => item.key() === key && !itemDisabled(key));
|
enabled().some((item) => item.key() === key && !itemDisabled(key));
|
||||||
@@ -335,6 +352,7 @@ function rootAttrs() {
|
|||||||
"data-focus-visible": (within.value && focusVisible.value) || undefined,
|
"data-focus-visible": (within.value && focusVisible.value) || undefined,
|
||||||
onFocusin: onRootFocus,
|
onFocusin: onRootFocus,
|
||||||
onFocusout: onRootFocusout,
|
onFocusout: onRootFocusout,
|
||||||
|
onPointercancel: cancelPress,
|
||||||
onKeydown: onRootKeydown,
|
onKeydown: onRootKeydown,
|
||||||
onMousedown: () => {
|
onMousedown: () => {
|
||||||
focusVisible.value = false;
|
focusVisible.value = false;
|
||||||
|
|||||||
@@ -67,10 +67,14 @@ function accessibleName() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The compatibility click of an already activated press must not re-propose. */
|
||||||
|
const pressActivated = shallowRef(false);
|
||||||
|
|
||||||
picker.register({
|
picker.register({
|
||||||
key: () => key,
|
key: () => key,
|
||||||
element: () => element.value,
|
element: () => element.value,
|
||||||
color: () => itemColor.value,
|
color: () => itemColor.value,
|
||||||
|
cancelPress,
|
||||||
textValue: () => accessibleName(),
|
textValue: () => accessibleName(),
|
||||||
disabled: () => !!props.isDisabled,
|
disabled: () => !!props.isDisabled,
|
||||||
});
|
});
|
||||||
@@ -91,9 +95,6 @@ const state = computed<ColorSwatchPickerItemRenderProps>(() => {
|
|||||||
|
|
||||||
provide(colorSwatchPickerItemKey, { key, state });
|
provide(colorSwatchPickerItemKey, { key, state });
|
||||||
|
|
||||||
/** The compatibility click of an already activated press must not re-propose. */
|
|
||||||
const pressActivated = shallowRef(false);
|
|
||||||
|
|
||||||
function activateFrom(event: Event) {
|
function activateFrom(event: Event) {
|
||||||
picker.activate(key, event);
|
picker.activate(key, event);
|
||||||
element.value?.focus({ preventScroll: true });
|
element.value?.focus({ preventScroll: true });
|
||||||
@@ -132,6 +133,11 @@ function releasePress() {
|
|||||||
if (picker.pressed.value === key) picker.pressed.value = undefined;
|
if (picker.pressed.value === key) picker.pressed.value = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cancelPress() {
|
||||||
|
pressActivated.value = false;
|
||||||
|
releasePress();
|
||||||
|
}
|
||||||
|
|
||||||
function itemAttrs() {
|
function itemAttrs() {
|
||||||
return mergeProps(attrs, {
|
return mergeProps(attrs, {
|
||||||
role: "option",
|
role: "option",
|
||||||
@@ -166,8 +172,7 @@ function itemAttrs() {
|
|||||||
},
|
},
|
||||||
onFocus: () => picker.noteFocus(key),
|
onFocus: () => picker.noteFocus(key),
|
||||||
onBlur: () => {
|
onBlur: () => {
|
||||||
pressActivated.value = false;
|
cancelPress();
|
||||||
releasePress();
|
|
||||||
picker.noteBlur(key);
|
picker.noteBlur(key);
|
||||||
},
|
},
|
||||||
onMousedown,
|
onMousedown,
|
||||||
@@ -181,8 +186,7 @@ function itemAttrs() {
|
|||||||
},
|
},
|
||||||
onMouseleave: () => {
|
onMouseleave: () => {
|
||||||
hovered.value = false;
|
hovered.value = false;
|
||||||
pressActivated.value = false;
|
cancelPress();
|
||||||
releasePress();
|
|
||||||
},
|
},
|
||||||
} as Record<string, unknown>);
|
} as Record<string, unknown>);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ export interface ColorSwatchPickerMember {
|
|||||||
key: () => ColorSwatchPickerKey;
|
key: () => ColorSwatchPickerKey;
|
||||||
element: () => HTMLElement | undefined;
|
element: () => HTMLElement | undefined;
|
||||||
color: () => Color;
|
color: () => Color;
|
||||||
|
/** Cancel this item's press and compatibility-click ownership. */
|
||||||
|
cancelPress: () => void;
|
||||||
/** Accessible name used for type-to-select matching. */
|
/** Accessible name used for type-to-select matching. */
|
||||||
textValue: () => string;
|
textValue: () => string;
|
||||||
disabled: () => boolean;
|
disabled: () => boolean;
|
||||||
|
|||||||
@@ -885,3 +885,57 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => {
|
|||||||
expect(disabled.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
|
expect(disabled.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe("ColorSwatchPicker activation cancellation boundaries", () => {
|
||||||
|
it("clears pressed state when the browser window loses focus", async () => {
|
||||||
|
const w = picker();
|
||||||
|
press(items(w)[0]!);
|
||||||
|
await w.vm.$nextTick();
|
||||||
|
expect(items(w)[0]!.attributes("data-pressed")).toBe("true");
|
||||||
|
window.dispatchEvent(new Event("blur"));
|
||||||
|
await w.vm.$nextTick();
|
||||||
|
expect(items(w)[0]!.attributes("data-pressed")).toBeUndefined();
|
||||||
|
});
|
||||||
|
it("allows click-only retry after a press is cancelled by window blur", async () => {
|
||||||
|
const w = picker({ modelValue: COLORS[0] });
|
||||||
|
const root = w.getComponent(ColorSwatchPicker);
|
||||||
|
press(items(w)[1]!);
|
||||||
|
await w.vm.$nextTick();
|
||||||
|
expect(root.emitted("update:modelValue")).toHaveLength(1);
|
||||||
|
window.dispatchEvent(new Event("blur"));
|
||||||
|
items(w)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
|
||||||
|
await w.vm.$nextTick();
|
||||||
|
expect(root.emitted("update:modelValue")).toHaveLength(2);
|
||||||
|
});
|
||||||
|
it("keeps cancellation scoped to the targeted picker", async () => {
|
||||||
|
const first = picker();
|
||||||
|
const second = picker({ modelValue: COLORS[0] });
|
||||||
|
press(items(first)[1]!);
|
||||||
|
press(items(second)[1]!);
|
||||||
|
await second.vm.$nextTick();
|
||||||
|
first.get("[data-slot=color-swatch-picker]").element.dispatchEvent(new Event("pointercancel", { bubbles: true }));
|
||||||
|
await second.vm.$nextTick();
|
||||||
|
expect(items(second)[1]!.attributes("data-pressed")).toBe("true");
|
||||||
|
// Cancelling the first owner must not clear the second's click suppression.
|
||||||
|
items(second)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
|
||||||
|
await second.vm.$nextTick();
|
||||||
|
expect(second.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toHaveLength(1);
|
||||||
|
window.dispatchEvent(new Event("blur"));
|
||||||
|
await second.vm.$nextTick();
|
||||||
|
expect(items(second)[1]!.attributes("data-pressed")).toBeUndefined();
|
||||||
|
items(second)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
|
||||||
|
await second.vm.$nextTick();
|
||||||
|
expect(second.getComponent(ColorSwatchPicker).emitted("update:modelValue")).toHaveLength(2);
|
||||||
|
});
|
||||||
|
it("allows repeated click-only proposals after controlled rejection", async () => {
|
||||||
|
const w = picker({ modelValue: COLORS[0] });
|
||||||
|
const root = w.getComponent(ColorSwatchPicker);
|
||||||
|
for (let attempt = 0; attempt < 2; attempt++) {
|
||||||
|
items(w)[1]!.element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 0 }));
|
||||||
|
await w.vm.$nextTick();
|
||||||
|
expect(items(w)[1]!.attributes("aria-selected")).toBe("false");
|
||||||
|
}
|
||||||
|
expect(root.emitted("update:modelValue")).toHaveLength(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user