149 lines
4.5 KiB
Vue
149 lines
4.5 KiB
Vue
<script setup lang="ts">
|
|
import { computed, provide, watchEffect, ref, watch, nextTick } from "vue";
|
|
import {
|
|
PopoverPortal,
|
|
PopoverContent as RekaContent,
|
|
useForwardExpose,
|
|
type PopoverContentEmits,
|
|
injectPopoverRootContext,
|
|
} from "reka-ui";
|
|
import { useConfig } from "../../providers/context";
|
|
import { surfaceContextKey } from "../surface/context";
|
|
import { resolvePlacement, type OverlayPlacement } from "../overlay/placement";
|
|
import { useOverlayAnimation } from "../overlay/animation";
|
|
import { usePopover } from "./context";
|
|
defineOptions({ inheritAttrs: false });
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
as?: string;
|
|
id?: string;
|
|
placement?: OverlayPlacement;
|
|
offset?: number;
|
|
crossOffset?: number;
|
|
shouldFlip?: boolean;
|
|
containerPadding?: number;
|
|
to?: string | HTMLElement;
|
|
isNonModal?: boolean;
|
|
isDismissable?: boolean;
|
|
isKeyboardDismissDisabled?: boolean;
|
|
}>(),
|
|
{
|
|
as: "div",
|
|
placement: "bottom",
|
|
offset: 8,
|
|
crossOffset: 0,
|
|
shouldFlip: true,
|
|
containerPadding: 12,
|
|
isNonModal: undefined,
|
|
isDismissable: true,
|
|
isKeyboardDismissDisabled: false,
|
|
},
|
|
);
|
|
const emit = defineEmits<PopoverContentEmits>();
|
|
const ctx = usePopover(),
|
|
config = useConfig(),
|
|
root = injectPopoverRootContext();
|
|
const originalId = ctx.contentId.value;
|
|
watchEffect(() => {
|
|
ctx.contentId.value = props.id ?? originalId;
|
|
root.contentId = ctx.contentId.value;
|
|
ctx.contentNonModal.value = props.isNonModal;
|
|
});
|
|
provide(surfaceContextKey, { variant: computed(() => "default" as const) });
|
|
const position = computed(() =>
|
|
resolvePlacement(props.placement, config?.dir.value),
|
|
);
|
|
const { forwardRef } = useForwardExpose();
|
|
const contentElement = ref<HTMLElement | null>(null);
|
|
let focusCancelled = false;
|
|
function openFocus(event: Event) {
|
|
emit("openAutoFocus", event);
|
|
focusCancelled = event.defaultPrevented;
|
|
}
|
|
watch(
|
|
contentElement,
|
|
(element, _, onCleanup) => {
|
|
let stale = false;
|
|
onCleanup(() => {
|
|
stale = true;
|
|
});
|
|
void nextTick(() => {
|
|
if (
|
|
!stale &&
|
|
element?.isConnected &&
|
|
ctx.state.isOpen.value &&
|
|
!focusCancelled &&
|
|
!element.contains(element.ownerDocument.activeElement)
|
|
)
|
|
element.focus({ preventScroll: true });
|
|
});
|
|
},
|
|
{ flush: "post" },
|
|
);
|
|
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
|
ctx.state.isOpen,
|
|
contentElement,
|
|
true,
|
|
);
|
|
function outside(event: PopoverContentEmits["interactOutside"][0]) {
|
|
emit("interactOutside", event);
|
|
if (!props.isDismissable) event.preventDefault();
|
|
}
|
|
function escape(event: KeyboardEvent) {
|
|
emit("escapeKeyDown", event);
|
|
if (props.isKeyboardDismissDisabled) event.preventDefault();
|
|
}
|
|
</script>
|
|
<template>
|
|
<PopoverPortal :to="to">
|
|
<RekaContent
|
|
:ref="forwardRef"
|
|
v-bind="$attrs"
|
|
as-child
|
|
:id="ctx.contentId.value"
|
|
:side="position.side"
|
|
:align="position.align"
|
|
:side-offset="offset"
|
|
:align-offset="crossOffset"
|
|
:side-flip="shouldFlip"
|
|
:collision-padding="containerPadding"
|
|
class="hv-popover"
|
|
:data-slot="$attrs['data-slot'] ?? 'popover-content'"
|
|
:data-entering="entering || undefined"
|
|
:data-exiting="exiting || undefined"
|
|
:lang="($attrs.lang as string | undefined) ?? config?.locale.value"
|
|
@animationend="onAnimationEnd"
|
|
@interact-outside="outside"
|
|
@escape-key-down="escape"
|
|
@pointer-down-outside="emit('pointerDownOutside', $event)"
|
|
@focus-outside="emit('focusOutside', $event)"
|
|
@open-auto-focus="openFocus"
|
|
@close-auto-focus="emit('closeAutoFocus', $event)"
|
|
>
|
|
<component
|
|
:is="as"
|
|
ref="contentElement"
|
|
:role="($attrs.role as string | undefined) ?? 'dialog'"
|
|
:aria-label="
|
|
($attrs['aria-label'] as string | undefined) ??
|
|
(ctx.dialogAttrs.value['aria-label'] as string | undefined)
|
|
"
|
|
:aria-describedby="
|
|
($attrs['aria-describedby'] as string | undefined) ??
|
|
(ctx.dialogAttrs.value['aria-describedby'] as string | undefined)
|
|
"
|
|
:aria-labelledby="
|
|
'aria-labelledby' in $attrs
|
|
? ($attrs['aria-labelledby'] as string)
|
|
: (ctx.dialogAttrs.value['aria-labelledby'] as
|
|
string | undefined) ||
|
|
Array.from(ctx.titles.values()).join(' ') ||
|
|
ctx.triggerId
|
|
"
|
|
>
|
|
<slot :close="ctx.state.close" :is-open="ctx.state.isOpen.value" />
|
|
</component>
|
|
</RekaContent>
|
|
</PopoverPortal>
|
|
</template>
|