// // Copyright 2026 DXOS.org // // @import-as-namespace import { Dialog as DialogPrimitive, useDialogContext } from '@ark-ui/react/dialog'; import { useEnvironmentContext } from '@ark-ui/react/environment'; import React, { forwardRef, useId } from 'react'; import { Button, type ButtonProps } from '../Button/Button.tsx'; import * as Dialog from '../Dialog/Dialog.tsx'; // // Root // type AlertDialogRootProps = Omit; /** * A Dialog with `role=alertdialog` that only its own controls close. It opens with focus on a control marked * `DIALOG_AUTOFOCUS_ATTRIBUTE`, else on `Cancel`, the least destructive choice. */ const AlertDialogRoot = ({ ids, initialFocusEl, ...props }: AlertDialogRootProps) => { const id = useId(); const { getRootNode } = useEnvironmentContext(); // The same root zag resolves the dialog in, so a dialog portalled into a shadow root or another document still finds them. const byId = (elementId: string): HTMLElement | null => { const root = getRootNode(); return 'getElementById' in root ? root.getElementById(elementId) : null; }; const contentId = ids?.content ?? `dx-alert-dialog-${id}-content`; const cancelId = ids?.closeTrigger ?? `dx-alert-dialog-${id}-cancel`; return ( byId(contentId)?.querySelector(`[${Dialog.DIALOG_AUTOFOCUS_ATTRIBUTE}]`) ?? byId(cancelId)) } /> ); }; AlertDialogRoot.displayName = 'AlertDialog.Root'; // // Cancel // type AlertDialogCancelProps = ButtonProps; /** A Button that closes the dialog without acting; the caller names it (Next ships no translated labels). */ const AlertDialogCancel = forwardRef((props, forwardedRef) => (