fix: reveal Select focus after floating positioning

This commit is contained in:
maoyingjie
2026-10-23 22:47:36 +08:00
parent 2b569451b6
commit 50f561a758
9 changed files with 176 additions and 4 deletions
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { mergeProps, useAttrs } from "vue";
import { mergeProps, nextTick, onBeforeUnmount, useAttrs } from "vue";
import PopoverContent from "../popover/PopoverContent.vue";
import { useSelect } from "./context";
import type { OverlayPlacement } from "../overlay/placement";
@@ -16,10 +16,46 @@ withDefaults(
);
const ctx = useSelect(),
attrs = useAttrs();
let frame: number | undefined;
let allowScroll = false;
let disposed = false;
function revealFocused(event?: AnimationEvent) {
if (event && event.target !== event.currentTarget) return;
const element = event?.currentTarget as HTMLElement | undefined;
const active = ctx.trigger.value?.ownerDocument
.activeElement as HTMLElement | null;
if (
allowScroll &&
ctx.open.value &&
active?.dataset.slot === "list-box-item" &&
active.closest('[role="listbox"]')?.id === ctx.listId &&
(!element || element.contains(active))
)
active.scrollIntoView?.({ block: "nearest", inline: "nearest" });
}
onBeforeUnmount(() => {
disposed = true;
if (frame !== undefined) cancelAnimationFrame(frame);
});
function openFocus(event: Event) {
if (frame !== undefined) {
cancelAnimationFrame(frame);
frame = undefined;
}
allowScroll = false;
if (!event.defaultPrevented) {
event.preventDefault();
allowScroll = true;
void ctx.focusCollection();
// Floating positioning finishes after mount autofocus. Reveal the accepted
// focus after layout too, including the reduced-motion path with no event.
void nextTick(() => {
if (!disposed && typeof requestAnimationFrame !== "undefined")
frame = requestAnimationFrame(() => {
frame = undefined;
revealFocused();
});
});
}
}
</script>
@@ -30,7 +66,12 @@ function openFocus(event: Event) {
:to="to"
:should-flip="shouldFlip"
:container-padding="containerPadding"
v-bind="mergeProps(attrs, { onOpenAutoFocus: openFocus })"
v-bind="
mergeProps(attrs, {
onOpenAutoFocus: openFocus,
onAnimationstart: revealFocused,
})
"
:id="`${ctx.listId}-popover`"
role="presentation"
:class="[ctx.slots.value.popover(), attrs.class]"