fix: cancel swatch picker press ownership on window blur

This commit is contained in:
maoyingjie
2026-10-27 16:03:46 +08:00
parent 3a4142e891
commit 3c8473d597
5 changed files with 86 additions and 8 deletions
@@ -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<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
* 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;
@@ -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<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 });
@@ -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<string, unknown>);
}
@@ -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;