{"version":3,"file":"Modal.mjs","names":[],"sources":["../../../src/modal/Modal.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport React, { useEffect } from 'react';\nimport FocusLockModule from 'react-focus-lock';\n\nimport { type CommonProps, type HtmlAttributes } from '../_common/types';\nimport { interopDefault, useDocumentScrollToggle } from '../_common/utils';\nimport { Portal } from '../portal';\n\n// react-focus-lock is CommonJS-only, see `interopDefault`.\nconst FocusLock = interopDefault(FocusLockModule);\n\nexport enum ModalCloseReason {\n  BACKDROP_CLICK = 'backdropClick',\n  ESCAPE_KEY_DOWN = 'escapeKeyDown',\n}\n\n/**\n * @remarks\n * Where `Element` is the most general base class from which all objects in a Document inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit from Element.\n *\n * @remarks\n * Where `HtmlAttributes<\"div\">` is a type that includes all standard HTML div element attributes plus any custom data attributes (like data-testid or data-value) that follow the data-* pattern.\n */\nexport interface ModalProps {\n  /** Whether the Modal is open. */\n  isOpen: boolean;\n  /** Content to render inside the Modal. */\n  children: React.ReactNode;\n  /** Sets the size(max-width) of the Modal, use 'unset' for custom sizing. */\n  size?: 'small' | 'medium' | 'large' | 'unset';\n  /** The container where the modal will be rendered. */\n  container?: Element;\n  /** Callback function called when the modal is closed. */\n  onClose?: (\n    event: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,\n    reason?: ModalCloseReason,\n  ) => void;\n  /** Forwards the props to the modal container's wrapper - use with caution */\n  rootProps?: HtmlAttributes<'div'>;\n}\n\nexport const Modal = ({\n  isOpen: open,\n  size = 'medium',\n  onClose,\n  children,\n  container,\n  rootProps,\n  style,\n  className,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'div', ModalProps>) => {\n  const toggleScrollbar = useDocumentScrollToggle();\n  const backdropClick = React.useRef<boolean | null>(null);\n\n  const handleMouseDown = (event: React.MouseEvent<HTMLDivElement>) => {\n    // We don't want to close the dialog when clicking the dialog content.\n    // Make sure the event starts and ends on the same DOM element.\n    backdropClick.current = event.target === event.currentTarget;\n  };\n\n  useEffect(() => {\n    const handleKeyDown = (event: KeyboardEvent) => {\n      if (event.key === 'Escape') {\n        event.preventDefault();\n        onClose?.(\n          event as unknown as React.KeyboardEvent<HTMLElement>,\n          ModalCloseReason.ESCAPE_KEY_DOWN,\n        );\n      }\n    };\n\n    document.addEventListener('keydown', handleKeyDown);\n    return () => {\n      document.removeEventListener('keydown', handleKeyDown);\n    };\n  }, [onClose]);\n\n  const handleBackdropClick = (event: React.MouseEvent<HTMLDivElement>) => {\n    if (Boolean(backdropClick.current) === false) {\n      return;\n    }\n\n    backdropClick.current = null;\n    onClose?.(event, ModalCloseReason.BACKDROP_CLICK);\n  };\n\n  useEffect(() => {\n    toggleScrollbar(open);\n\n    return () => toggleScrollbar(false);\n  }, [open, toggleScrollbar]);\n\n  if (!open) {\n    return null;\n  }\n\n  const classes = classNames('ndl-modal', className, {\n    'ndl-large': size === 'large',\n    'ndl-medium': size === 'medium',\n    'ndl-small': size === 'small',\n  });\n  const rootClasses = classNames('ndl-modal-root', rootProps?.className, {\n    'ndl-modal-container': container,\n  });\n\n  return (\n    <>\n      <Portal container={container}>\n        {/** This is to separate the colored backdrop with the blur from the modal. */}\n        <div className=\"ndl-modal-backdrop\" role=\"presentation\" />\n      </Portal>\n      <Portal container={container}>\n        <FocusLock hasPositiveIndices={true} returnFocus={true}>\n          <div\n            {...rootProps}\n            className={rootClasses}\n            role=\"presentation\"\n            onClick={handleBackdropClick}\n            onMouseDown={handleMouseDown}\n          >\n            <div\n              ref={ref}\n              role=\"dialog\"\n              aria-modal=\"true\"\n              className={classes}\n              style={style}\n              {...restProps}\n              {...htmlAttributes}\n            >\n              {children}\n            </div>\n          </div>\n        </FocusLock>\n      </Portal>\n    </>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8DE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AA1CF,MAAM,YAAY,eAAe,eAAe;AAEhD,IAAY,mBAAL,yBAAA,kBAAA;CACL,iBAAA,oBAAA;CACA,iBAAA,qBAAA;;AACF,EAAA,CAAA,CAAA;AA2BA,MAAa,SAAS,SAYgB;CAZhB,IAAA,EACpB,QAAQ,MACR,OAAO,UACP,SACA,UACA,WACA,WACA,OACA,WACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,kBAAkB,wBAAwB;CAChD,MAAM,gBAAgB,MAAM,OAAuB,IAAI;CAEvD,MAAM,mBAAmB,UAA4C;EAGnE,cAAc,UAAU,MAAM,WAAW,MAAM;CACjD;CAEA,gBAAgB;EACd,MAAM,iBAAiB,UAAyB;GAC9C,IAAI,MAAM,QAAQ,UAAU;IAC1B,MAAM,eAAe;IACrB,YAAA,QAAA,YAAA,KAAA,KAAA,QACE,OAAA,eAEF;GACF;EACF;EAEA,SAAS,iBAAiB,WAAW,aAAa;EAClD,aAAa;GACX,SAAS,oBAAoB,WAAW,aAAa;EACvD;CACF,GAAG,CAAC,OAAO,CAAC;CAEZ,MAAM,uBAAuB,UAA4C;EACvE,IAAI,QAAQ,cAAc,OAAO,MAAM,OACrC;EAGF,cAAc,UAAU;EACxB,YAAA,QAAA,YAAA,KAAA,KAAA,QAAU,OAAA,eAAsC;CAClD;CAEA,gBAAgB;EACd,gBAAgB,IAAI;EAEpB,aAAa,gBAAgB,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