92 lines
2.7 KiB
Vue
92 lines
2.7 KiB
Vue
<script setup lang="ts">
|
|
import { mergeProps, nextTick, onBeforeUnmount, useAttrs } from "vue";
|
|
import PopoverContent from "../popover/PopoverContent.vue";
|
|
import { useSelect } from "./context";
|
|
import type { OverlayPlacement } from "../overlay/placement";
|
|
defineOptions({ name: "SelectPopover", inheritAttrs: false });
|
|
withDefaults(
|
|
defineProps<{
|
|
placement?: OverlayPlacement;
|
|
offset?: number;
|
|
to?: string | HTMLElement;
|
|
shouldFlip?: boolean;
|
|
containerPadding?: number;
|
|
}>(),
|
|
{ placement: "bottom", offset: 8, shouldFlip: true, containerPadding: 12 },
|
|
);
|
|
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>
|
|
<template>
|
|
<PopoverContent
|
|
:placement="placement"
|
|
:offset="offset"
|
|
:to="to"
|
|
:should-flip="shouldFlip"
|
|
:container-padding="containerPadding"
|
|
v-bind="
|
|
mergeProps(attrs, {
|
|
onOpenAutoFocus: openFocus,
|
|
onAnimationstart: revealFocused,
|
|
})
|
|
"
|
|
:id="`${ctx.listId}-popover`"
|
|
role="presentation"
|
|
:class="[ctx.slots.value.popover(), attrs.class]"
|
|
data-slot="select-popover"
|
|
:aria-labelledby="
|
|
ctx.field.labelledBy() ??
|
|
ctx.trigger.value?.id ??
|
|
ctx.field.field.controlId.value
|
|
"
|
|
:style="{
|
|
'--trigger-width': 'var(--reka-popover-trigger-width)',
|
|
'--trigger-anchor-point': 'var(--reka-popper-transform-origin)',
|
|
}"
|
|
>
|
|
<slot />
|
|
</PopoverContent>
|
|
</template>
|