Files
heroui-vue/packages/vue/src/components/popover/context.ts
T

36 lines
943 B
TypeScript

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({}),
};
}