feat: migrate Modal Popover and Tooltip with shared overlay foundation
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { defineComponent, h, provide } from "vue";
|
||||
import { PopoverRoot } from "reka-ui";
|
||||
import { useOverlayState } from "../../composables/useOverlayState";
|
||||
import { overlayNodes, overlayComponentName } from "../overlay/OverlayAction";
|
||||
import { createPopoverContext, popoverKey } from "./context";
|
||||
import PopoverTrigger from "./PopoverTrigger.vue";
|
||||
export default defineComponent({
|
||||
name: "Popover",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
isNonModal: { type: Boolean, default: false },
|
||||
},
|
||||
emits: {
|
||||
"update:open": (value: boolean) => typeof value === "boolean",
|
||||
"update:isOpen": (value: boolean) => typeof value === "boolean",
|
||||
openChange: (value: boolean) => typeof value === "boolean",
|
||||
},
|
||||
setup(props, { slots, emit }) {
|
||||
const state = useOverlayState({
|
||||
isOpen: () => props.open ?? props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
onOpenChange(v) {
|
||||
emit("update:open", v);
|
||||
emit("update:isOpen", v);
|
||||
emit("openChange", v);
|
||||
},
|
||||
});
|
||||
const context = createPopoverContext(state);
|
||||
provide(popoverKey, context);
|
||||
return () =>
|
||||
h(
|
||||
PopoverRoot,
|
||||
{
|
||||
open: state.isOpen.value,
|
||||
modal: !(context.contentNonModal.value ?? props.isNonModal),
|
||||
"onUpdate:open": state.setOpen,
|
||||
},
|
||||
{
|
||||
default: () => {
|
||||
let found = false;
|
||||
return overlayNodes(
|
||||
slots.default?.({
|
||||
isOpen: state.isOpen.value,
|
||||
close: state.close,
|
||||
}),
|
||||
).map((node) => {
|
||||
const name = overlayComponentName(node);
|
||||
if (name === "PopoverTrigger") found = true;
|
||||
if (
|
||||
!found &&
|
||||
(name === "Button" ||
|
||||
node.type === "button" ||
|
||||
node.type === "a")
|
||||
) {
|
||||
found = true;
|
||||
return h(
|
||||
PopoverTrigger,
|
||||
{ asChild: true, key: node.key ?? undefined },
|
||||
{ default: () => node },
|
||||
);
|
||||
}
|
||||
return node;
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
import { PopoverArrow as RekaArrow } from "reka-ui";
|
||||
import OverlayArrowShape from "../overlay/OverlayArrowShape";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<RekaArrow v-bind="$attrs" as-child :width="12" :height="12">
|
||||
<OverlayArrowShape marker="popover-overlay-arrow">
|
||||
<slot />
|
||||
</OverlayArrowShape>
|
||||
</RekaArrow>
|
||||
</template>
|
||||
@@ -0,0 +1,147 @@
|
||||
<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="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"
|
||||
: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>
|
||||
@@ -0,0 +1,32 @@
|
||||
<script setup lang="ts">
|
||||
import { useAttrs, onBeforeUpdate, onUnmounted } from "vue";
|
||||
import { usePopover } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(defineProps<{ as?: string }>(), { as: "div" });
|
||||
const ctx = usePopover();
|
||||
const attrs = useAttrs();
|
||||
const keys = ["aria-label", "aria-labelledby", "aria-describedby", "role"];
|
||||
function sync() {
|
||||
const next = Object.fromEntries(
|
||||
keys.filter((key) => key in attrs).map((key) => [key, attrs[key]]),
|
||||
);
|
||||
const previous = ctx.dialogAttrs.value;
|
||||
if (keys.some((key) => next[key] !== previous[key]))
|
||||
ctx.dialogAttrs.value = next;
|
||||
}
|
||||
sync();
|
||||
onBeforeUpdate(sync);
|
||||
onUnmounted(() => {
|
||||
ctx.dialogAttrs.value = {};
|
||||
});
|
||||
</script>
|
||||
<template>
|
||||
<component
|
||||
:is="as"
|
||||
v-bind="$attrs"
|
||||
class="hv-popover__dialog"
|
||||
data-slot="popover-dialog"
|
||||
>
|
||||
<slot :close="ctx.state.close" />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,25 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, useId, watchEffect, onUnmounted } from "vue";
|
||||
import { usePopover } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string; id?: string }>(), {
|
||||
as: "h2",
|
||||
});
|
||||
const ctx = usePopover(),
|
||||
key = Symbol(),
|
||||
generated = "hv-popover-heading-" + useId();
|
||||
const id = computed(() => props.id ?? generated);
|
||||
watchEffect(() => ctx.titles.set(key, id.value));
|
||||
onUnmounted(() => ctx.titles.delete(key));
|
||||
</script>
|
||||
<template>
|
||||
<component
|
||||
:is="as"
|
||||
v-bind="$attrs"
|
||||
:id="id"
|
||||
class="hv-popover__heading"
|
||||
data-slot="popover-heading"
|
||||
>
|
||||
<slot />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,46 @@
|
||||
<script setup lang="ts">
|
||||
import { watch } from "vue";
|
||||
import {
|
||||
PopoverAnchor,
|
||||
injectPopoverRootContext,
|
||||
useForwardExpose,
|
||||
} from "reka-ui";
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { usePopover } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "div" },
|
||||
);
|
||||
const ctx = usePopover(),
|
||||
root = injectPopoverRootContext();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
watch(
|
||||
currentElement,
|
||||
(el) => {
|
||||
root.triggerElement.value = el;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
root.triggerId = ctx.triggerId;
|
||||
</script>
|
||||
<template>
|
||||
<PopoverAnchor as-child>
|
||||
<OverlayAction
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:id="($attrs.id as string | undefined) ?? ctx.triggerId"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="isDisabled"
|
||||
:activate="ctx.state.toggle"
|
||||
class="hv-popover__trigger"
|
||||
data-slot="popover-trigger"
|
||||
aria-haspopup="dialog"
|
||||
:aria-expanded="ctx.state.isOpen.value"
|
||||
:aria-controls="ctx.state.isOpen.value ? ctx.contentId.value : undefined"
|
||||
>
|
||||
<slot />
|
||||
</OverlayAction>
|
||||
</PopoverAnchor>
|
||||
</template>
|
||||
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
inject,
|
||||
reactive,
|
||||
ref,
|
||||
shallowRef,
|
||||
useId,
|
||||
type InjectionKey,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
export interface PopoverContext {
|
||||
state: OverlayState;
|
||||
contentId: Ref<string>;
|
||||
triggerId: string;
|
||||
contentNonModal: Ref<boolean | undefined>;
|
||||
titles: Map<symbol, string>;
|
||||
dialogAttrs: Ref<Record<string, unknown>>;
|
||||
}
|
||||
export const popoverKey: InjectionKey<PopoverContext> = Symbol("hv-popover");
|
||||
export function usePopover() {
|
||||
const ctx = inject(popoverKey);
|
||||
if (!ctx) throw new Error("Popover parts require Popover.");
|
||||
return ctx;
|
||||
}
|
||||
export function createPopoverContext(state: OverlayState): PopoverContext {
|
||||
const id = "hv-popover-" + useId();
|
||||
return {
|
||||
state,
|
||||
contentId: ref(id + "-content"),
|
||||
triggerId: id + "-trigger",
|
||||
contentNonModal: ref(undefined),
|
||||
titles: reactive(new Map()),
|
||||
dialogAttrs: shallowRef({}),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user