diff --git a/apps/docs/components/color-swatch-picker.md b/apps/docs/components/color-swatch-picker.md index 3830fa6..d08945f 100644 --- a/apps/docs/components/color-swatch-picker.md +++ b/apps/docs/components/color-swatch-picker.md @@ -249,7 +249,7 @@ Root 实例通过 `defineExpose` 提供 `element`(真实根 DOM)与 `focus() | 输入字符 | 按项名称前缀匹配并聚焦(名称差异见下) | | 边界 | 不循环;到边界后保持当前项 | -焦点与选择分离:方向键只移动焦点,`aria-selected` 与指示器只反映接受颜色。父级拒绝提案后,选择样式、`aria-selected` 与 DOM 保持接受状态,同一手势可重试。 +焦点与选择分离:方向键只移动焦点,`aria-selected` 与指示器只反映接受颜色。父级拒绝提案后,选择样式、`aria-selected` 与 DOM 保持接受状态,同一手势可重试。窗口失焦或指针取消会清除按压状态与点击去重归属,恢复后可以立即重试。 ### 可访问名称差异 diff --git a/packages/vue/src/components/color-swatch-picker/ColorSwatchPicker.vue b/packages/vue/src/components/color-swatch-picker/ColorSwatchPicker.vue index bd3b4fc..3fc6162 100644 --- a/packages/vue/src/components/color-swatch-picker/ColorSwatchPicker.vue +++ b/packages/vue/src/components/color-swatch-picker/ColorSwatchPicker.vue @@ -7,6 +7,7 @@ import { useAttrs, watch, } from "vue"; +import { useEventListener } from "@vueuse/core"; import { colorSwatchPickerVariants } from "@heroui-vue/styles"; import { useConfig } from "../../providers/context"; import { useColorState } from "../color/model"; @@ -100,6 +101,19 @@ const within = shallowRef(false); const focusVisible = shallowRef(false); const pressed = shallowRef(); +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 * value keeps its own space and precision and is never rewritten to match. @@ -282,6 +296,9 @@ watch( .join(","), ].join("|"), () => { + for (const member of ordered()) { + if (state.disabled.value || member.disabled()) member.cancelPress(); + } const usable = (key: ColorSwatchPickerKey | undefined) => key !== undefined && enabled().some((item) => item.key() === key && !itemDisabled(key)); @@ -335,6 +352,7 @@ function rootAttrs() { "data-focus-visible": (within.value && focusVisible.value) || undefined, onFocusin: onRootFocus, onFocusout: onRootFocusout, + onPointercancel: cancelPress, onKeydown: onRootKeydown, onMousedown: () => { focusVisible.value = false; diff --git a/packages/vue/src/components/color-swatch-picker/ColorSwatchPickerItem.vue b/packages/vue/src/components/color-swatch-picker/ColorSwatchPickerItem.vue index 3afec00..8d4fb14 100644 --- a/packages/vue/src/components/color-swatch-picker/ColorSwatchPickerItem.vue +++ b/packages/vue/src/components/color-swatch-picker/ColorSwatchPickerItem.vue @@ -67,10 +67,14 @@ function accessibleName() { ); } +/** The compatibility click of an already activated press must not re-propose. */ +const pressActivated = shallowRef(false); + picker.register({ key: () => key, element: () => element.value, color: () => itemColor.value, + cancelPress, textValue: () => accessibleName(), disabled: () => !!props.isDisabled, }); @@ -91,9 +95,6 @@ const state = computed(() => { 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 }); @@ -132,6 +133,11 @@ function releasePress() { if (picker.pressed.value === key) picker.pressed.value = undefined; } +function cancelPress() { + pressActivated.value = false; + releasePress(); +} + function itemAttrs() { return mergeProps(attrs, { role: "option", @@ -166,8 +172,7 @@ function itemAttrs() { }, onFocus: () => picker.noteFocus(key), onBlur: () => { - pressActivated.value = false; - releasePress(); + cancelPress(); picker.noteBlur(key); }, onMousedown, @@ -181,8 +186,7 @@ function itemAttrs() { }, onMouseleave: () => { hovered.value = false; - pressActivated.value = false; - releasePress(); + cancelPress(); }, } as Record); } diff --git a/packages/vue/src/components/color-swatch-picker/context.ts b/packages/vue/src/components/color-swatch-picker/context.ts index 478c463..efad973 100644 --- a/packages/vue/src/components/color-swatch-picker/context.ts +++ b/packages/vue/src/components/color-swatch-picker/context.ts @@ -18,6 +18,8 @@ export interface ColorSwatchPickerMember { key: () => ColorSwatchPickerKey; element: () => HTMLElement | undefined; color: () => Color; + /** Cancel this item's press and compatibility-click ownership. */ + cancelPress: () => void; /** Accessible name used for type-to-select matching. */ textValue: () => string; disabled: () => boolean; diff --git a/tests/color-swatch-picker.test.ts b/tests/color-swatch-picker.test.ts index 7c1c8fe..8e15d03 100644 --- a/tests/color-swatch-picker.test.ts +++ b/tests/color-swatch-picker.test.ts @@ -885,3 +885,57 @@ describe("ColorSwatchPicker activation lifecycle and dynamic focus", () => { 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); + }); +});