UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

162 lines 9.09 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 (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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dialog = exports.useIsInsideDialog = exports.DialogCloseReason = 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 defaultImports_1 = require("../_common/defaultImports"); const button_1 = require("../button"); const icons_1 = require("../icons"); const modal_1 = require("../modal"); // Should extend ModalCloseReason var DialogCloseReason; (function (DialogCloseReason) { DialogCloseReason["ESCAPE_KEY_DOWN"] = "escapeKeyDown"; DialogCloseReason["CLOSE_BUTTON_CLICK"] = "closeButtonClick"; })(DialogCloseReason || (exports.DialogCloseReason = DialogCloseReason = {})); const DialogContext = react_1.default.createContext(null); const useDialogContext = () => { const value = (0, react_1.useContext)(DialogContext); if (!value) { throw new Error('Accessing `useDialogContext` outside of the context provider'); } return value; }; const useIsInsideDialog = () => { const value = (0, react_1.useContext)(DialogContext); return Boolean(value); }; exports.useIsInsideDialog = useIsInsideDialog; const convertCloseReason = (reason) => { const dialogReason = reason; if (Object.values(DialogCloseReason).includes(dialogReason)) { return dialogReason; } throw new Error(`Unknown reason '${reason}' received from popup`); }; /* Stroke width on regular icons is 1.5px */ const typeIcons = { warning: (0, jsx_runtime_1.jsx)(icons_1.ExclamationTriangleIconOutline, { strokeWidth: 3 }), danger: (0, jsx_runtime_1.jsx)(icons_1.ExclamationCircleIconOutline, { strokeWidth: 3 }), info: (0, jsx_runtime_1.jsx)(icons_1.InformationCircleIconOutline, { strokeWidth: 3 }), }; const Dialog = ({ isOpen, onClose, children, type, size, container, rootProps, hasDisabledCloseButton = false, modalProps, htmlAttributes, }) => { const handleCloseClick = (e) => { e.preventDefault(); onClose === null || onClose === void 0 ? void 0 : onClose(e, DialogCloseReason.CLOSE_BUTTON_CLICK); }; const handleClose = (e, reason) => { // Dialog should not be closed when backdrop is clicked if (reason === modal_1.ModalCloseReason.BACKDROP_CLICK) { return; } onClose === null || onClose === void 0 ? void 0 : onClose(e, reason ? convertCloseReason(reason) : undefined); }; if (!isOpen) { return null; } const classes = (0, defaultImports_1.classNames)('ndl-dialog', modalProps === null || modalProps === void 0 ? void 0 : modalProps.className, { 'ndl-with-icon': !!type, 'ndl-with-close-button': !hasDisabledCloseButton, }); const contentWrapperClasses = (0, defaultImports_1.classNames)('n-flex n-flex-nowrap n-h-full n-gap-token-9', { 'n-mt-token-8': !!type, }); return ((0, jsx_runtime_1.jsx)(modal_1.Modal, { isOpen: isOpen, onClose: handleClose, container: container, rootProps: rootProps, modalProps: Object.assign(Object.assign({}, (modalProps || {})), { className: classes, role: 'dialog' }), size: size, htmlAttributes: htmlAttributes, children: (0, jsx_runtime_1.jsxs)(DialogContext.Provider, { value: { type }, children: [!hasDisabledCloseButton && ((0, jsx_runtime_1.jsx)(button_1.IconButton, { isClean: true, size: "medium", className: "ndl-dialog-close", onClick: handleCloseClick, ariaLabel: "Close dialog", htmlAttributes: { 'data-testid': 'ndl-dialog-close', role: 'button', }, children: (0, jsx_runtime_1.jsx)(icons_1.XMarkIconOutline, {}) })), (0, jsx_runtime_1.jsxs)("div", { className: contentWrapperClasses, children: [type && ((0, jsx_runtime_1.jsx)("div", { className: `ndl-dialog-type-icon ndl-${type}`, "data-testid": "ndl-dialog-type-icon", children: typeIcons[type] })), (0, jsx_runtime_1.jsx)("div", { className: "n-flex n-flex-col n-flex-1 n-w-full", children: children })] })] }) })); }; exports.Dialog = Dialog; const Actions = react_1.default.forwardRef(function Actions({ as, children, className, style, htmlAttributes, }, ref) { const { type } = useDialogContext(); const classes = (0, defaultImports_1.classNames)('ndl-dialog-actions', className); const Component = as || 'div'; let childrenToRender = children; // For danger dialogs, we want to convert primary buttons to danger buttons by default if (type === 'danger') { childrenToRender = react_1.default.Children.map(children, (element) => { if (!react_1.default.isValidElement(element)) return; const { color } = element.props; if (!color || color === 'primary') { return react_1.default.cloneElement(element, Object.assign(Object.assign({}, element.props), { color: 'danger' })); } return element; }); } return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: childrenToRender }))); }); exports.Dialog.Actions = Actions; const Header = react_1.default.forwardRef(function Header({ as, children, className, style, htmlAttributes, }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-dialog-header', className); const Component = as || 'h4'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); exports.Dialog.Header = Header; const Subtitle = react_1.default.forwardRef(function Subtitle({ as, children, className, style, htmlAttributes, }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-dialog-subtitle n-body-large', className); const Component = as || 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); exports.Dialog.Subtitle = Subtitle; const Description = react_1.default.forwardRef(function Description({ as, children, className, style, htmlAttributes, }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-dialog-description n-body-medium', className); const Component = as || 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); exports.Dialog.Description = Description; const Content = react_1.default.forwardRef(function Content({ as, children, className, style, htmlAttributes, }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-dialog-content', className); const Component = as || 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); exports.Dialog.Content = Content; const Image = react_1.default.forwardRef(function Image({ src, alt, className, style, htmlAttributes }, ref) { const classes = (0, defaultImports_1.classNames)('ndl-dialog-image', className); return ((0, jsx_runtime_1.jsx)("img", Object.assign({ src: src, alt: alt, className: classes, style: style, ref: ref }, htmlAttributes))); }); exports.Dialog.Image = Image; //# sourceMappingURL=Dialog.js.map