UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

134 lines 7.24 kB
import { jsx as _jsx, 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, { useContext } from 'react'; import { classNames } from '../_common/defaultImports'; import { IconButton } from '../icon-button'; import { ExclamationCircleIconOutline, ExclamationTriangleIconOutline, InformationCircleIconOutline, XMarkIconOutline, } from '../icons'; import { Modal, ModalCloseReason } from '../modal'; // Should extend ModalCloseReason export var DialogCloseReason; (function (DialogCloseReason) { DialogCloseReason["ESCAPE_KEY_DOWN"] = "escapeKeyDown"; DialogCloseReason["CLOSE_BUTTON_CLICK"] = "closeButtonClick"; })(DialogCloseReason || (DialogCloseReason = {})); const DialogContext = React.createContext(null); const useDialogContext = () => { const value = useContext(DialogContext); if (!value) { throw new Error('Accessing `useDialogContext` outside of the context provider'); } return value; }; export const useIsInsideDialog = () => { const value = useContext(DialogContext); return Boolean(value); }; 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: _jsx(ExclamationTriangleIconOutline, { strokeWidth: 3 }), danger: _jsx(ExclamationCircleIconOutline, { strokeWidth: 3 }), info: _jsx(InformationCircleIconOutline, { strokeWidth: 3 }), }; export 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 === ModalCloseReason.BACKDROP_CLICK) { return; } onClose === null || onClose === void 0 ? void 0 : onClose(e, reason ? convertCloseReason(reason) : undefined); }; if (!isOpen) { return null; } const classes = classNames('ndl-dialog', modalProps === null || modalProps === void 0 ? void 0 : modalProps.className, { 'ndl-with-icon': !!type, 'ndl-with-close-button': !hasDisabledCloseButton, }); const contentWrapperClasses = classNames('n-flex n-flex-nowrap n-h-full n-gap-token-9', { 'n-mt-token-8': !!type, }); return (_jsx(Modal, { isOpen: isOpen, onClose: handleClose, container: container, rootProps: rootProps, modalProps: Object.assign(Object.assign({}, modalProps), { className: classes, role: 'dialog' }), size: size, htmlAttributes: htmlAttributes, children: _jsxs(DialogContext.Provider, { value: { type }, children: [!hasDisabledCloseButton && (_jsx(IconButton, { isClean: true, size: "medium", className: "ndl-dialog-close", onClick: handleCloseClick, ariaLabel: "Close dialog", htmlAttributes: { 'data-testid': 'ndl-dialog-close', role: 'button', }, children: _jsx(XMarkIconOutline, {}) })), _jsxs("div", { className: contentWrapperClasses, children: [type && (_jsx("div", { className: `ndl-dialog-type-icon ndl-${type}`, "data-testid": "ndl-dialog-type-icon", children: typeIcons[type] })), _jsx("div", { className: "n-flex n-flex-col n-flex-1 n-w-full", children: children })] })] }) })); }; const Actions = React.forwardRef(function Actions({ as, children, className, style, htmlAttributes, }, ref) { const { type } = useDialogContext(); const classes = 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.Children.map(children, (element) => { if (!React.isValidElement(element)) return; const { color } = element.props; if (!color || color === 'primary') { return React.cloneElement(element, Object.assign(Object.assign({}, element.props), { color: 'danger' })); } return element; }); } return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: childrenToRender }))); }); Dialog.Actions = Actions; const Header = React.forwardRef(function Header({ as, children, className, style, htmlAttributes, }, ref) { const classes = classNames('ndl-dialog-header', className); const Component = as || 'h4'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); Dialog.Header = Header; const Subtitle = React.forwardRef(function Subtitle({ as, children, className, style, htmlAttributes, }, ref) { const classes = classNames('ndl-dialog-subtitle n-body-large', className); const Component = as || 'div'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); Dialog.Subtitle = Subtitle; const Description = React.forwardRef(function Description({ as, children, className, style, htmlAttributes, }, ref) { const classes = classNames('ndl-dialog-description n-body-medium', className); const Component = as || 'div'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); Dialog.Description = Description; const Content = React.forwardRef(function Content({ as, children, className, style, htmlAttributes, }, ref) { const classes = classNames('ndl-dialog-content', className); const Component = as || 'div'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, htmlAttributes, { children: children }))); }); Dialog.Content = Content; const Image = React.forwardRef(function Image({ src, alt, className, style, htmlAttributes }, ref) { const classes = classNames('ndl-dialog-image', className); return (_jsx("img", Object.assign({ src: src, alt: alt, className: classes, style: style, ref: ref }, htmlAttributes))); }); Dialog.Image = Image; //# sourceMappingURL=Dialog.js.map