@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
105 lines • 5.58 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Modal = exports.ModalCloseReason = void 0;
const jsx_runtime_1 = require("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/>.
*/
const react_1 = __importStar(require("react"));
const react_focus_lock_1 = __importDefault(require("react-focus-lock"));
const defaultImports_1 = require("../_common/defaultImports");
const utils_1 = require("../_common/utils");
const portal_1 = require("../portal");
var ModalCloseReason;
(function (ModalCloseReason) {
ModalCloseReason["BACKDROP_CLICK"] = "backdropClick";
ModalCloseReason["ESCAPE_KEY_DOWN"] = "escapeKeyDown";
})(ModalCloseReason || (exports.ModalCloseReason = ModalCloseReason = {}));
exports.Modal = (0, react_1.forwardRef)(function Modal({ isOpen: open, size = 'medium', onClose, children, container, rootProps, modalProps, htmlAttributes, }, ref) {
const toggleScrollbar = (0, utils_1.useDocumentScrollToggle)();
const backdropClick = react_1.default.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;
};
(0, react_1.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);
};
(0, react_1.useEffect)(() => {
toggleScrollbar(open);
return () => toggleScrollbar(false);
}, [open, toggleScrollbar]);
if (!open) {
return null;
}
const classes = (0, defaultImports_1.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 = (0, defaultImports_1.classNames)('ndl-modal-root', rootProps === null || rootProps === void 0 ? void 0 : rootProps.className, {
'ndl-modal-container': container,
});
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(portal_1.Portal, { container: container, children: (0, jsx_runtime_1.jsx)("div", { className: "ndl-modal-backdrop", role: "presentation", "data-testid": "ndl-modal-backdrop" }) }), (0, jsx_runtime_1.jsx)(portal_1.Portal, { container: container, children: (0, jsx_runtime_1.jsx)(react_focus_lock_1.default, { hasPositiveIndices: true, returnFocus: true, children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: rootClasses, onClick: handleBackdropClick, role: "presentation" }, htmlAttributes, { children: (0, jsx_runtime_1.jsx)("div", Object.assign({ className: "ndl-modal-wrapper", "data-testid": "ndl-modal-wrapper" }, rootProps, { onMouseDown: handleMouseDown, role: "presentation", children: (0, jsx_runtime_1.jsx)("div", Object.assign({}, modalProps, { role: "dialog", "aria-modal": "true", className: classes, ref: ref, children: children })) })) })) }) })] }));
});
//# sourceMappingURL=Modal.js.map