UNPKG

@loke/ui

Version:
138 lines (133 loc) • 7.26 kB
"use client"; // src/components/alert-dialog/alert-dialog.tsx import { composeEventHandlers } from "@loke/ui/compose-events"; import { useComposedRefs } from "@loke/ui/compose-refs"; import { createContextScope } from "@loke/ui/context"; import * as DialogPrimitive from "@loke/ui/dialog"; import { createDialogScope } from "@loke/ui/dialog"; import { createSlottable } from "@loke/ui/slot"; import { forwardRef, useEffect, useRef } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; var ROOT_NAME = "AlertDialog", [createAlertDialogContext, createAlertDialogScope] = createContextScope(ROOT_NAME, [createDialogScope]), useDialogScope = createDialogScope(), AlertDialog = /* @__PURE__ */ Object.assign((props) => { let { __scopeAlertDialog, ...alertDialogProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Root, { ...dialogScope, ...alertDialogProps, modal: !0 }); }, { displayName: ROOT_NAME }), TRIGGER_NAME = "AlertDialogTrigger", AlertDialogTrigger = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...triggerProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Trigger, { ...dialogScope, ...triggerProps, ref: forwardedRef }); }), { displayName: TRIGGER_NAME }), PORTAL_NAME = "AlertDialogPortal", AlertDialogPortal = /* @__PURE__ */ Object.assign((props) => { let { __scopeAlertDialog, ...portalProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Portal, { ...dialogScope, ...portalProps }); }, { displayName: PORTAL_NAME }), OVERLAY_NAME = "AlertDialogOverlay", AlertDialogOverlay = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...overlayProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Overlay, { ...dialogScope, ...overlayProps, ref: forwardedRef }); }), { displayName: OVERLAY_NAME }), CONTENT_NAME = "AlertDialogContent", [AlertDialogContentProvider, useAlertDialogContentContext] = createAlertDialogContext(CONTENT_NAME), Slottable = createSlottable("AlertDialogContent"), AlertDialogContent = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, children, ...contentProps } = props, dialogScope = useDialogScope(__scopeAlertDialog), contentRef = useRef(null), composedRefs = useComposedRefs(forwardedRef, contentRef), cancelRef = useRef(null); return /* @__PURE__ */ jsx(DialogPrimitive.WarningProvider, { contentName: CONTENT_NAME, docsSlug: "alert-dialog", titleName: TITLE_NAME, children: /* @__PURE__ */ jsx(AlertDialogContentProvider, { cancelRef, scope: __scopeAlertDialog, children: /* @__PURE__ */ jsxs(DialogPrimitive.Content, { role: "alertdialog", ...dialogScope, ...contentProps, onInteractOutside: (event) => event.preventDefault(), onOpenAutoFocus: composeEventHandlers(contentProps.onOpenAutoFocus, (event) => { event.preventDefault(), cancelRef.current?.focus({ preventScroll: !0 }); }), onPointerDownOutside: (event) => event.preventDefault(), ref: composedRefs, children: [ /* @__PURE__ */ jsx(Slottable, { children }), process.env.NODE_ENV === "development" && /* @__PURE__ */ jsx(DescriptionWarning, { contentRef }) ] }) }) }); }), { displayName: CONTENT_NAME }), TITLE_NAME = "AlertDialogTitle", AlertDialogTitle = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...titleProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Title, { ...dialogScope, ...titleProps, ref: forwardedRef }); }), { displayName: TITLE_NAME }), DESCRIPTION_NAME = "AlertDialogDescription", AlertDialogDescription = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...descriptionProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Description, { ...dialogScope, ...descriptionProps, ref: forwardedRef }); }), { displayName: DESCRIPTION_NAME }), ACTION_NAME = "AlertDialogAction", AlertDialogAction = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...actionProps } = props, dialogScope = useDialogScope(__scopeAlertDialog); return /* @__PURE__ */ jsx(DialogPrimitive.Close, { ...dialogScope, ...actionProps, ref: forwardedRef }); }), { displayName: ACTION_NAME }), CANCEL_NAME = "AlertDialogCancel", AlertDialogCancel = /* @__PURE__ */ Object.assign(/* @__PURE__ */ forwardRef((props, forwardedRef) => { let { __scopeAlertDialog, ...cancelProps } = props, { cancelRef } = useAlertDialogContentContext(CANCEL_NAME, __scopeAlertDialog), dialogScope = useDialogScope(__scopeAlertDialog), ref = useComposedRefs(forwardedRef, cancelRef); return /* @__PURE__ */ jsx(DialogPrimitive.Close, { ...dialogScope, ...cancelProps, ref }); }), { displayName: CANCEL_NAME }), ARIA_ID_SEPARATOR = /\s+/, DescriptionWarning = ({ contentRef }) => { let MESSAGE = `\`${CONTENT_NAME}\` requires a description for the component to be accessible for screen reader users. You can add a description to the \`${CONTENT_NAME}\` by passing a \`${DESCRIPTION_NAME}\` component as a child, which also benefits sighted users by adding visible context to the dialog. Alternatively, you can use your own component as a description by assigning it an \`id\` and passing the same value to the \`aria-describedby\` prop in \`${CONTENT_NAME}\`. If the description is confusing or duplicative for sighted users, you can use the \`@loke/ui/visually-hidden\` primitive as a wrapper around your description component. For more information, see https://design.loke.global/docs/components/alert-dialog`; return useEffect(() => { let timer = window.setTimeout(() => { if (!contentRef.current?.getAttribute("aria-describedby")?.split(ARIA_ID_SEPARATOR).some((id) => id && document.getElementById(id))) console.warn(MESSAGE); }, 0); return () => window.clearTimeout(timer); }, [MESSAGE, contentRef]), null; }, Root2 = AlertDialog, Trigger2 = AlertDialogTrigger, Portal2 = AlertDialogPortal, Overlay2 = AlertDialogOverlay, Content2 = AlertDialogContent, Action = AlertDialogAction, Cancel = AlertDialogCancel, Title2 = AlertDialogTitle, Description2 = AlertDialogDescription; export { Action, AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogOverlay, AlertDialogPortal, AlertDialogTitle, AlertDialogTrigger, Cancel, Content2 as Content, Description2 as Description, Overlay2 as Overlay, Portal2 as Portal, Root2 as Root, Title2 as Title, Trigger2 as Trigger, createAlertDialogScope };