fix(dropdown): preserve disabled and click semantics during submenu delays

This commit is contained in:
maoyingjie
2026-10-29 14:45:59 +08:00
parent d1528569a8
commit bf6cf0f8d1
5 changed files with 258 additions and 379 deletions
@@ -129,7 +129,14 @@ export default defineComponent({
* at once like the baseline.
*/
const hoverOpenTimer = ref<ReturnType<typeof setTimeout> | null>(null);
let duringKeydown = false;
let activationEvent: Event | undefined;
function triggerDisabled() {
const element = elementFromValue(triggerRef.value);
return (
element?.getAttribute("aria-disabled") === "true" ||
element?.hasAttribute("data-disabled") === true
);
}
function clearHoverOpen() {
if (hoverOpenTimer.value) {
clearTimeout(hoverOpenTimer.value);
@@ -137,14 +144,15 @@ export default defineComponent({
}
}
function scheduleHoverOpen() {
if (hoverOpenTimer.value || subOpen.value) return;
if (hoverOpenTimer.value || subOpen.value || triggerDisabled()) return;
if (!(props.delay > 0)) {
subOpen.value = true;
return;
}
hoverOpenTimer.value = setTimeout(() => {
hoverOpenTimer.value = null;
subOpen.value = true;
if (!triggerDisabled() && dropdownCtx?.open.value !== false)
subOpen.value = true;
}, props.delay);
}
function itemPointerMove(event: Event) {
@@ -156,19 +164,22 @@ export default defineComponent({
function itemPointerLeave() {
clearHoverOpen();
}
function globalKeydownCapture() {
duringKeydown = true;
function activationCapture(event: Event) {
activationEvent = event;
queueMicrotask(() => {
duringKeydown = false;
if (activationEvent === event) activationEvent = undefined;
});
}
onMounted(() =>
window.addEventListener("keydown", globalKeydownCapture, true),
onBeforeUnmount(clearHoverOpen);
watch(
() => dropdownCtx?.open.value,
(open) => {
if (open === false) {
clearHoverOpen();
subOpen.value = false;
}
},
);
onBeforeUnmount(() => {
window.removeEventListener("keydown", globalKeydownCapture, true);
clearHoverOpen();
});
const contentAttrs = {
label: ref<string | undefined>(),
labelledBy: ref<string | undefined>(),
@@ -361,11 +372,31 @@ export default defineComponent({
open: subOpen.value,
"onUpdate:open": (value: boolean) => {
if (value) {
if (triggerDisabled()) {
clearHoverOpen();
return;
}
const click = activationEvent;
if (click?.type === "click") {
// asChild's primitive can request open before the item's
// bubble handlers. Let the complete click chain cancel it.
queueMicrotask(() => {
if (
click.defaultPrevented ||
triggerDisabled() ||
dropdownCtx?.open.value === false
)
return;
clearHoverOpen();
subOpen.value = true;
});
return;
}
// Reka's fixed 100ms hover timer requests the open before the
// configured delay; the delay owns that open instead. Keyboard
// opens arrive during a keydown and pass through at once,
// cancelling the pending hover open like the baseline.
if (hoverOpenTimer.value && !duringKeydown) return;
if (hoverOpenTimer.value && !activationEvent) return;
clearHoverOpen();
} else clearHoverOpen();
subOpen.value = value;
@@ -392,6 +423,14 @@ export default defineComponent({
// Escape with focus on the trigger (pointer-opened
// submenu) must also close only this level.
onKeydown: handleEscape,
onKeydownCapture: [
...asArray(attrs.onKeydownCapture as Handler),
activationCapture,
],
onClickCapture: [
...asArray(attrs.onClickCapture as Handler),
activationCapture,
],
// Hover intent drives the configured open delay.
onPointermove: itemPointerMove,
onPointerleave: itemPointerLeave,
@@ -409,7 +448,6 @@ export default defineComponent({
{
...domProps,
...panelProps,
// experiment: stop forwarding side/align
...ariaProps,
class: [
dropdownCtx?.slots.value.popover(),