UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

76 lines 3.99 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import React, { forwardRef, useEffect } from 'react'; import FocusLock from 'react-focus-lock'; import { classNames } from '../_common/defaultImports'; import { useDocumentScrollToggle } from '../_common/utils'; import { Portal } from '../portal'; export var ModalCloseReason; (function (ModalCloseReason) { ModalCloseReason["BACKDROP_CLICK"] = "backdropClick"; ModalCloseReason["ESCAPE_KEY_DOWN"] = "escapeKeyDown"; })(ModalCloseReason || (ModalCloseReason = {})); export const Modal = forwardRef(function Modal({ isOpen: open, size = 'medium', onClose, children, container, rootProps, modalProps, htmlAttributes, }, ref) { const toggleScrollbar = useDocumentScrollToggle(); const backdropClick = React.useRef(null); const handleMouseDown = (event) => { // We don't want to close the dialog when clicking the dialog content. // Make sure the event starts and ends on the same DOM element. backdropClick.current = event.target === event.currentTarget; }; useEffect(() => { const handleKeyDown = (e) => { if (e.key === 'Escape') { e.preventDefault(); onClose === null || onClose === void 0 ? void 0 : onClose(e, ModalCloseReason.ESCAPE_KEY_DOWN); } }; document.addEventListener('keydown', handleKeyDown); return () => { document.removeEventListener('keydown', handleKeyDown); }; }, [onClose]); const handleBackdropClick = (e) => { if (!backdropClick.current) { return; } backdropClick.current = null; onClose === null || onClose === void 0 ? void 0 : onClose(e, ModalCloseReason.BACKDROP_CLICK); }; useEffect(() => { toggleScrollbar(open); return () => toggleScrollbar(false); }, [open, toggleScrollbar]); if (!open) { return null; } const classes = classNames('ndl-modal', modalProps === null || modalProps === void 0 ? void 0 : modalProps.className, { 'ndl-small': size === 'small', 'ndl-medium': size === 'medium', 'ndl-large': size === 'large', }); const rootClasses = classNames('ndl-modal-root', rootProps === null || rootProps === void 0 ? void 0 : rootProps.className, { 'ndl-modal-container': container, }); return (_jsxs(_Fragment, { children: [_jsx(Portal, { container: container, children: _jsx("div", { className: "ndl-modal-backdrop", role: "presentation", "data-testid": "ndl-modal-backdrop" }) }), _jsx(Portal, { container: container, children: _jsx(FocusLock, { hasPositiveIndices: true, returnFocus: true, children: _jsx("div", Object.assign({ className: rootClasses, onClick: handleBackdropClick, role: "presentation" }, htmlAttributes, { children: _jsx("div", Object.assign({ className: "ndl-modal-wrapper", "data-testid": "ndl-modal-wrapper" }, rootProps, { onMouseDown: handleMouseDown, role: "presentation", children: _jsx("div", Object.assign({}, modalProps, { role: "dialog", "aria-modal": "true", className: classes, ref: ref, children: children })) })) })) }) })] })); }); //# sourceMappingURL=Modal.js.map