{"version":3,"file":"Dialog.mjs","sources":["../../../src/components/Dialog/Dialog.tsx"],"sourcesContent":["import {\n  Description,\n  DialogPanel,\n  DialogTitle,\n  Dialog as HeadlessDialog,\n  Transition,\n} from '@headlessui/react';\nimport clsx from 'clsx';\nimport type { KeyboardEvent, ReactNode } from 'react';\nimport React from 'react';\n\nimport Button from '../Button';\nimport ButtonGroup from '../ButtonGroup';\nimport Heading from '../Heading';\nimport Text from '../Text';\n\nimport styles from './Dialog.module.css';\n\ntype DialogProps = {\n  // Component API\n  /**\n   * The question or message the dialog asks the user to respond to. Accepts formatted content,\n   * such as text with emphasis or a short list.\n   */\n  children: ReactNode;\n  /**\n   * Additional classnames passed in for styling.\n   *\n   * This is applied to the dialog's surface, not the overlay behind it.\n   */\n  className?: string;\n  /**\n   * Label for the secondary button, which cancels.\n   *\n   * **Default is `\"Cancel\"`**.\n   */\n  cancelLabel?: string;\n  /**\n   * Label for the primary button, which runs the default action. Name the action, for example\n   * `\"Discard changes\"` or `\"Delete file\"`, rather than `\"OK\"` or `\"Yes\"`.\n   */\n  confirmLabel: string;\n  /**\n   * Called when the user cancels: the secondary button, the ESC key, or a click on the overlay.\n   * Use this to hide the dialog.\n   */\n  onCancel: () => void;\n  /**\n   * Called when the user takes the default action: the primary button, or the Enter key while\n   * focus isn't on another button or a link. Use this to act and hide the dialog.\n   */\n  onConfirm: () => void;\n  /**\n   * Whether the dialog is visible.\n   */\n  open: boolean;\n  /**\n   * Short title for the dialog, which also names it for screen readers. Use a verb-noun question\n   * or statement, for example `\"Discard your changes?\"`.\n   */\n  title: string;\n  // Design API\n  /**\n   * Status (color) of the primary button. Use `\"critical\"` when the default action destroys\n   * something or can't be undone.\n   *\n   * **Default is `\"default\"`**.\n   */\n  confirmVariant?: 'default' | 'critical';\n};\n\n/**\n * Elements that already respond to Enter themselves, so the dialog leaves Enter to them.\n */\nconst OWNS_ENTER_KEY = [\n  'button',\n  'input[type=\"button\"]',\n  'input[type=\"image\"]',\n  'input[type=\"reset\"]',\n  'input[type=\"submit\"]',\n  'a[href]',\n  'textarea',\n  'select',\n  'summary',\n  '[contenteditable]:not([contenteditable=\"false\"])',\n].join(', ');\n\n/**\n * BETA: This component is still a work in progress and is subject to change.\n *\n * `import {Dialog} from \"@chanzuckerberg/eds\";`\n *\n * A small, focused prompt that asks the user to confirm or cancel an action. Use it in place of\n * the browser's `window.confirm()`, which can't be styled or given formatted content.\n *\n * ## Usage\n *\n * * Ask one question with one default action. For anything with more content or more choices,\n *   use `Modal`.\n * * The primary button runs the default action and receives focus when the dialog opens, so\n *   Enter confirms. The secondary button cancels, as do ESC and a click on the overlay.\n * * Set `confirmVariant=\"critical\"` when the action is destructive.\n *\n * ### Confirming before a `Modal` closes\n *\n * `Modal`'s `warnOnClose` uses `window.confirm()`. To ask with a `Dialog` instead, leave\n * `warnOnClose` off and decide in the modal's `onClose`: close straight away when nothing would\n * be lost, otherwise open the `Dialog`. Render the `Dialog` inside `Modal.Body` so it stacks\n * above the modal.\n *\n * ## Content & Accessibility\n *\n * * Use a verb-noun question for the title, and a primary label that names the action.\n * * Keep the body to a sentence or two that says what happens next.\n * * The dialog is announced as an `alertdialog`, named by its title and described by its body.\n */\nexport const Dialog = ({\n  cancelLabel = 'Cancel',\n  children,\n  className,\n  confirmLabel,\n  confirmVariant = 'default',\n  onCancel,\n  onConfirm,\n  open,\n  title,\n}: DialogProps) => {\n  const confirmButtonRef = React.useRef<HTMLButtonElement>(null);\n\n  const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n    if (\n      event.key !== 'Enter' ||\n      // an element in the body already handled Enter itself. Headless UI skips handlers on\n      // prevented events too, but this doesn't rely on that\n      event.defaultPrevented ||\n      event.nativeEvent.isComposing ||\n      (event.target as HTMLElement).closest(OWNS_ENTER_KEY)\n    ) {\n      return;\n    }\n    event.preventDefault();\n    onConfirm();\n  };\n\n  return (\n    <Transition\n      as={React.Fragment}\n      enter={styles['dialog__transition--enter']}\n      enterFrom={styles['dialog__transition--enterFrom']}\n      enterTo={styles['dialog__transition--enterTo']}\n      leave={styles['dialog__transition--leave']}\n      leaveFrom={styles['dialog__transition--leaveFrom']}\n      leaveTo={styles['dialog__transition--leaveTo']}\n      show={open}\n    >\n      <HeadlessDialog\n        className={styles['dialog']}\n        initialFocus={confirmButtonRef}\n        // ESC and overlay clicks close through here, and both mean cancel.\n        onClose={onCancel}\n        role=\"alertdialog\"\n      >\n        <div className={styles['dialog__overlay']} />\n        <DialogPanel\n          className={clsx(styles['dialog__content'], className)}\n          onKeyDown={handleKeyDown}\n        >\n          <DialogTitle as={React.Fragment}>\n            <Heading\n              as=\"h2\"\n              className={styles['dialog__title']}\n              preset=\"title-md\"\n            >\n              {title}\n            </Heading>\n          </DialogTitle>\n          <Description as={React.Fragment}>\n            <Text as=\"div\" className={styles['dialog__body']} preset=\"body-md\">\n              {children}\n            </Text>\n          </Description>\n          <ButtonGroup className={styles['dialog__footer']}>\n            <Button\n              onClick={onConfirm}\n              rank=\"primary\"\n              ref={confirmButtonRef}\n              size=\"md\"\n              variant={confirmVariant}\n            >\n              {confirmLabel}\n            </Button>\n            <Button onClick={onCancel} rank=\"secondary\" size=\"md\">\n              {cancelLabel}\n            </Button>\n          </ButtonGroup>\n        </DialogPanel>\n      </HeadlessDialog>\n    </Transition>\n  );\n};\n\nDialog.displayName = 'Dialog';\n"],"names":["React","HeadlessDialog"],"mappings":";;;;;;;;;AAuEA;;AAEG;AACH,MAAM,cAAc,GAAG;IACrB,QAAQ;IACR,sBAAsB;IACtB,qBAAqB;IACrB,qBAAqB;IACrB,sBAAsB;IACtB,SAAS;IACT,UAAU;IACV,QAAQ;IACR,SAAS;IACT,kDAAkD;AACnD,CAAA,CAAC,IAAI,CAAC,IAAI,CAAC;AAEZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BG;AACI,MAAM,MAAM,GAAG,CAAC,EACrB,WAAW,GAAG,QAAQ,EACtB,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,cAAc,GAAG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