fix: reveal Select focus after floating positioning
This commit is contained in:
@@ -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]"
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import {
|
||||
Select,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
SelectIndicator,
|
||||
SelectPopover,
|
||||
ListBox,
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
Label,
|
||||
Button,
|
||||
Modal,
|
||||
ModalTrigger,
|
||||
ModalBackdrop,
|
||||
ModalContainer,
|
||||
ModalDialog,
|
||||
ModalHeading,
|
||||
} from "@heroui-vue/components";
|
||||
const log = ref<string[]>([]);
|
||||
function record(event: AnimationEvent) {
|
||||
const element = event.target as HTMLElement;
|
||||
if (element.dataset.slot !== "select-popover") return;
|
||||
const style = getComputedStyle(element);
|
||||
log.value.push(
|
||||
JSON.stringify({
|
||||
event: event.type,
|
||||
name: event.animationName,
|
||||
elapsed: event.elapsedTime,
|
||||
entering: element.dataset.entering,
|
||||
exiting: element.dataset.exiting,
|
||||
duration: style.animationDuration,
|
||||
scale: style.getPropertyValue("--tw-enter-scale"),
|
||||
side: element.dataset.side,
|
||||
}),
|
||||
);
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex flex-col gap-4 p-8">
|
||||
<Button>Before select</Button>
|
||||
<Select class="w-64" placeholder="Choose" default-value="12">
|
||||
<Label>Lifecycle select</Label>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover @animationstart="record" @animationend="record">
|
||||
<ListBox>
|
||||
<ListBoxItem
|
||||
v-for="index in 40"
|
||||
:id="String(index)"
|
||||
:key="index"
|
||||
:text-value="`Option ${index}`"
|
||||
>
|
||||
Option {{ index }}
|
||||
<ListBoxItemIndicator />
|
||||
</ListBoxItem>
|
||||
</ListBox>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
<Button>After select</Button>
|
||||
<Modal>
|
||||
<ModalTrigger>Open parent</ModalTrigger>
|
||||
<ModalBackdrop>
|
||||
<ModalContainer>
|
||||
<ModalDialog>
|
||||
<ModalHeading>Parent</ModalHeading>
|
||||
<Select class="w-64">
|
||||
<Label>Nested select</Label>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
<SelectIndicator />
|
||||
</SelectTrigger>
|
||||
<SelectPopover>
|
||||
<ListBox><ListBoxItem id="one">One</ListBoxItem></ListBox>
|
||||
</SelectPopover>
|
||||
</Select>
|
||||
</ModalDialog>
|
||||
</ModalContainer>
|
||||
</ModalBackdrop>
|
||||
</Modal>
|
||||
<pre aria-label="Animation log">{{ log.join("\n") }}</pre>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user