{"version":3,"file":"useConfirm-D7f4rj6F.cjs","sources":["../src/components/ConfirmDialog/ConfirmDialog.tsx","../src/components/ConfirmDialog/useConfirm.tsx"],"sourcesContent":["'use client';\nimport { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';\nimport type { CSSProperties, ReactNode } from 'react';\nimport { Dialog } from '../Dialog';\nimport type { DialogSize } from '../Dialog';\nimport { Button } from '../Button';\nimport type { ButtonColor } from '../Button';\nimport { Input } from '../Input';\nimport { cx } from '../../utils/cx';\nimport type { SxProp } from '../Layout/sx';\n\nexport interface ConfirmDialogProps {\n  /** Controlled open state. */\n  open: boolean;\n  /** Called with the next open state (e.g. on Esc, backdrop, or cancel). */\n  onOpenChange: (open: boolean) => void;\n  /** Dialog heading. */\n  title: ReactNode;\n  /** Body copy. Ignored when `children` is provided. */\n  message?: ReactNode;\n  /** Custom body; overrides `message`. */\n  children?: ReactNode;\n  /** Confirm button label. Default `'Confirm'`. */\n  confirmLabel?: string;\n  /** Cancel button label. Default `'Cancel'`. */\n  cancelLabel?: string;\n  /** Render the confirm button in the danger color. Shorthand for `color=\"danger\"`. */\n  danger?: boolean;\n  /** Confirm button colour (`primary` | `danger` | `warning` | `success` | `info`). Wins over `danger`. Default: `primary`. */\n  color?: ButtonColor;\n  /** Show a spinner on confirm and disable both buttons. */\n  loading?: boolean;\n  /**\n   * Type-to-confirm: the confirm button stays disabled until the user types\n   * this exact text. Use for destructive actions (e.g. the resource name).\n   */\n  requireText?: string;\n  /** Fired when the confirm button is clicked. */\n  onConfirm: () => void;\n  /** Fired when the cancel button is clicked (before close). */\n  onCancel?: () => void;\n  /** Hide the X close button (the dialog then closes via Cancel / Confirm, Esc and outside click unless disabled below). */\n  hideClose?: boolean;\n  /** Close on Esc (as Cancel). Default: true. */\n  closeOnEscape?: boolean;\n  /** Close on an outside / backdrop click (as Cancel). Default: true. */\n  closeOnOutsideClick?: boolean;\n  /** Width preset. Default: `sm`. */\n  size?: DialogSize;\n  className?: string;\n  /** Inline style forwarded to the underlying Dialog content panel. */\n  style?: CSSProperties;\n  /**\n   * One-off CSS forwarded to the underlying Dialog content panel, same as\n   * `Box`'s `sx`: each value may be a scalar or a responsive breakpoint map\n   * (`{ base, sm, md, lg, xl }`). Scalar-only sx merges into inline style;\n   * responsive sx injects a scoped class.\n   */\n  sx?: SxProp;\n}\n\n/**\n * ConfirmDialog — a small Dialog preset for destructive / confirmation prompts.\n * Composes the base `Dialog` and `Button`.\n */\nexport function ConfirmDialog({\n  open,\n  onOpenChange,\n  title,\n  message,\n  children,\n  confirmLabel = 'Confirm',\n  cancelLabel = 'Cancel',\n  danger = false,\n  color,\n  loading = false,\n  requireText,\n  onConfirm,\n  onCancel,\n  hideClose,\n  closeOnEscape,\n  closeOnOutsideClick,\n  size = 'sm',\n  className,\n  style,\n  sx,\n}: ConfirmDialogProps) {\n  const cancelRef = useRef<HTMLButtonElement>(null);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const inputId = useId();\n  const [typed, setTyped] = useState('');\n  useEffect(() => { if (!open) setTyped(''); }, [open]);\n  const blocked = requireText != null && typed !== requireText;\n\n  // `loading` disables the focused button, which would drop focus to <body> (and\n  // out of the dialog's focus trap). Park it on the dialog panel instead.\n  useLayoutEffect(() => {\n    const el = contentRef.current;\n    if (loading && el && !el.contains(document.activeElement)) el.focus({ preventScroll: true });\n    else if (loading && el && (document.activeElement as HTMLElement | null)?.matches(':disabled')) el.focus({ preventScroll: true });\n  }, [loading]);\n\n  const confirmColor: ButtonColor = color ?? (danger ? 'danger' : 'primary');\n  const isDanger = confirmColor === 'danger';\n\n  const handleCancel = () => {\n    onCancel?.();\n    onOpenChange(false);\n  };\n\n  return (\n    <Dialog\n      ref={contentRef}\n      open={open}\n      onOpenChange={onOpenChange}\n      size={size}\n      hideClose={hideClose}\n      closeOnEscape={closeOnEscape}\n      closeOnOutsideClick={closeOnOutsideClick}\n      title={title}\n      className={cx('cny-confirm', className)}\n      style={style}\n      sx={sx}\n      // Destructive prompts: cancel gets initial focus so Enter can't confirm by reflex.\n      onOpenAutoFocus={isDanger || requireText != null ? (e) => { e.preventDefault(); cancelRef.current?.focus(); } : undefined}\n      footer={\n        <>\n          <Button ref={cancelRef} variant=\"ghost\" color=\"primary\" onClick={handleCancel} disabled={loading}>\n            {cancelLabel}\n          </Button>\n          <Button\n            variant=\"solid\"\n            color={confirmColor}\n            loading={loading}\n            disabled={blocked}\n            onClick={onConfirm}\n          >\n            {confirmLabel}\n          </Button>\n        </>\n      }\n    >\n      {children ?? <p className=\"cny-confirm-message\">{message}</p>}\n      {requireText != null && (\n        <div className=\"cny-confirm-require\">\n          <label htmlFor={inputId} className=\"cny-confirm-require-label\">\n            Type <strong>{requireText}</strong> to confirm\n          </label>\n          <Input\n            id={inputId}\n            value={typed}\n            onChange={(e) => setTyped(e.target.value)}\n            autoComplete=\"off\"\n            disabled={loading}\n          />\n        </div>\n      )}\n    </Dialog>\n  );\n}\n","'use client';\nimport { useCallback, useState } from 'react';\nimport type { ButtonColor } from '../Button';\nimport type { ReactNode } from 'react';\nimport { ConfirmDialog } from './ConfirmDialog';\n\nexport interface ConfirmOptions {\n  title: ReactNode;\n  message?: ReactNode;\n  confirmLabel?: string;\n  cancelLabel?: string;\n  danger?: boolean;\n  /** Confirm button colour; wins over `danger`. */\n  color?: ButtonColor;\n}\n\ntype PendingState = ConfirmOptions & { resolve: (value: boolean) => void };\n\n/**\n * Imperative confirmation. Render `confirmDialog` once in the tree, then\n * `await confirm({ title, message })` to get a boolean.\n */\nexport function useConfirm() {\n  const [pending, setPending] = useState<PendingState | null>(null);\n\n  const confirm = useCallback((options: ConfirmOptions) => {\n    return new Promise<boolean>((resolve) => {\n      setPending({ ...options, resolve });\n    });\n  }, []);\n\n  const handleOpenChange = useCallback((open: boolean) => {\n    if (!open) {\n      setPending((current) => {\n        current?.resolve(false);\n        return null;\n      });\n    }\n  }, []);\n\n  const handleConfirm = useCallback(() => {\n    setPending((current) => {\n      current?.resolve(true);\n      return null;\n    });\n  }, []);\n\n  const confirmDialog: ReactNode = pending ? (\n    <ConfirmDialog\n      open\n      onOpenChange={handleOpenChange}\n      title={pending.title}\n      message={pending.message}\n      confirmLabel={pending.confirmLabel}\n      cancelLabel={pending.cancelLabel}\n      danger={pending.danger}\n      color={pending.color}\n      onConfirm={handleConfirm}\n    />\n  ) : null;\n\n  return { confirm, confirmDialog };\n}\n"],"names":["ConfirmDialog","open","onOpenChange","title","message","children","confirmLabel","cancelLabel","danger","color","loading","requireText","onConfirm","onCancel","hideClose","closeOnEscape","closeOnOutsideClick","size","className","style","sx","cancelRef","useRef","contentRef","inputId","useId","typed","setTyped","useState","useEffect","blocked","useLayoutEffect","el","_a","confirmColor","isDanger","handleCancel","jsxs","Dialog","cx","e","Fragment","jsx","Button","Input","useConfirm","pending","setPending","confirm","useCallback","options","resolve","handleOpenChange","current","handleConfirm","confirmDialog"],"mappings":"6NAiEO,SAASA,EAAc,CAC5B,KAAAC,EACA,aAAAC,EACA,MAAAC,EACA,QAAAC,EACA,SAAAC,EACA,aAAAC,EAAe,UACf,YAAAC,EAAc,SACd,OAAAC,EAAS,GACT,MAAAC,EACA,QAAAC,EAAU,GACV,YAAAC,EACA,UAAAC,EACA,SAAAC,EACA,UAAAC,EACA,cAAAC,EACA,oBAAAC,EACA,KAAAC,EAAO,KACP,UAAAC,EACA,MAAAC,EACA,GAAAC,CACF,EAAuB,CACrB,MAAMC,EAAYC,EAAAA,OAA0B,IAAI,EAC1CC,EAAaD,EAAAA,OAAuB,IAAI,EACxCE,EAAUC,EAAAA,MAAA,EACV,CAACC,EAAOC,CAAQ,EAAIC,EAAAA,SAAS,EAAE,EACrCC,EAAAA,UAAU,IAAM,CAAO5B,GAAM0B,EAAS,EAAE,CAAG,EAAG,CAAC1B,CAAI,CAAC,EACpD,MAAM6B,EAAUnB,GAAe,MAAQe,IAAUf,EAIjDoB,EAAAA,gBAAgB,IAAM,OACpB,MAAMC,EAAKT,EAAW,QAClBb,GAAWsB,GAAM,CAACA,EAAG,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