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

61 lines
1.6 KiB
TypeScript

import {
computed,
inject,
reactive,
ref,
useId,
type ComputedRef,
type InjectionKey,
type Ref,
} from "vue";
import type { AlertDialogVariants } from "@heroui-vue/styles";
import type { OverlayState } from "../../composables/useOverlayState";
export type AlertDialogPlacement = "auto" | "top" | "center" | "bottom";
export type AlertDialogStatus =
"default" | "accent" | "success" | "warning" | "danger";
export interface AlertDialogContext {
state: OverlayState;
contentId: Ref<string>;
defaultId: string;
titles: Map<symbol, string>;
placement: ComputedRef<AlertDialogPlacement>;
size: ComputedRef<AlertDialogVariants["size"]>;
/** Backdrop variant; defaults to opaque. */
variant: ComputedRef<AlertDialogVariants["variant"]>;
dismissable: ComputedRef<boolean>;
keyboardDisabled: ComputedRef<boolean>;
}
export const alertDialogKey: InjectionKey<AlertDialogContext> =
Symbol("hv-alert-dialog");
export function useAlertDialog(required = true) {
const ctx = inject(alertDialogKey, undefined);
if (!ctx && required) {
throw new Error(
"AlertDialog parts require AlertDialog or AlertDialogBackdrop.",
);
}
return ctx!;
}
export function createAlertDialogContext(
state: OverlayState,
): AlertDialogContext {
const id = "hv-alert-dialog-" + useId();
return {
state,
contentId: ref(id),
defaultId: id,
titles: reactive(new Map()),
// Alert dialogs require explicit action by default.
placement: computed(() => "auto"),
size: computed(() => "md"),
variant: computed(() => "opaque"),
dismissable: computed(() => false),
keyboardDisabled: computed(() => true),
};
}