UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

128 lines 6.31 kB
"use strict"; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; 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 classnames_1 = __importDefault(require("classnames")); const react_1 = __importStar(require("react")); const react_focus_lock_1 = __importDefault(require("react-focus-lock")); 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 = {})); const Modal = (_a) => { var { isOpen: open, size = 'medium', onClose, children, container, rootProps, style, className, htmlAttributes, ref } = _a, restProps = __rest(_a, ["isOpen", "size", "onClose", "children", "container", "rootProps", "style", "className", "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 (Boolean(backdropClick.current) === false) { 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, classnames_1.default)('ndl-modal', className, { 'ndl-large': size === 'large', 'ndl-medium': size === 'medium', 'ndl-small': size === 'small', }); const rootClasses = (0, classnames_1.default)('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" }) }), (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({}, rootProps, { className: rootClasses, role: "presentation", onClick: handleBackdropClick, onMouseDown: handleMouseDown, children: (0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, role: "dialog", "aria-modal": "true", className: classes, style: style }, restProps, htmlAttributes, { children: children })) })) }) })] })); }; exports.Modal = Modal; //# sourceMappingURL=Modal.js.map