@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
88 lines • 4.38 kB
JavaScript
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;
};
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 classNames from 'classnames';
import React, { useEffect } from 'react';
import FocusLock from 'react-focus-lock';
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 = (_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 = 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 (Boolean(backdropClick.current) === false) {
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', className, {
'ndl-large': size === 'large',
'ndl-medium': size === 'medium',
'ndl-small': size === 'small',
});
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" }) }), _jsx(Portal, { container: container, children: _jsx(FocusLock, { hasPositiveIndices: true, returnFocus: true, children: _jsx("div", Object.assign({}, rootProps, { className: rootClasses, role: "presentation", onClick: handleBackdropClick, onMouseDown: handleMouseDown, children: _jsx("div", Object.assign({ ref: ref, role: "dialog", "aria-modal": "true", className: classes, style: style }, restProps, htmlAttributes, { children: children })) })) }) })] }));
};
//# sourceMappingURL=Modal.js.map