UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

199 lines 11.9 kB
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, 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, useId, useState } from 'react'; import { CleanIconButton } from '../clean-icon-button'; import { FilledButton } from '../filled-button'; import { ExclamationCircleIconOutline } from '../icons/generated/heroIcons/ExclamationCircleIconOutline'; import { ExclamationTriangleIconOutline } from '../icons/generated/heroIcons/ExclamationTriangleIconOutline'; import { InformationCircleIconOutline } from '../icons/generated/heroIcons/InformationCircleIconOutline'; import { XMarkIconOutline } from '../icons/generated/heroIcons/XMarkIconOutline'; import { Modal, ModalCloseReason } from '../modal'; import { OutlinedButton } from '../outlined-button'; import { TextButton } from '../text-button'; import { Typography } from '../typography'; import { DialogContext, useDialogContext } from './dialog-context'; import { DialogCloseReason, } from './dialog-types'; 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 = { danger: (_jsx(ExclamationCircleIconOutline, { htmlAttributes: { 'aria-hidden': undefined, 'aria-label': 'Danger', strokeWidth: 3, } })), info: (_jsx(InformationCircleIconOutline, { htmlAttributes: { 'aria-hidden': undefined, 'aria-label': 'Information', strokeWidth: 3, } })), warning: (_jsx(ExclamationTriangleIconOutline, { htmlAttributes: { 'aria-hidden': undefined, 'aria-label': 'Warning', strokeWidth: 3, } })), }; const DialogComponent = (_a) => { var _b, _c; var { isOpen, onClose, children, variant, size, container, rootProps, hasDisabledCloseButton = false, htmlAttributes, className, style, ref } = _a, restProps = __rest(_a, ["isOpen", "onClose", "children", "variant", "size", "container", "rootProps", "hasDisabledCloseButton", "htmlAttributes", "className", "style", "ref"]); const generatedDescriptionId = useId(); const generatedHeaderId = useId(); const [descriptionId, setDescriptionId] = useState(generatedDescriptionId); const [headerId, setHeaderId] = useState(generatedHeaderId); 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 !== undefined ? convertCloseReason(reason) : undefined); }; const hasHeaderChild = React.useMemo(() => { const childrenArray = React.Children.toArray(children); return childrenArray.some((child) => React.isValidElement(child) && child.type === Header); }, [children]); const hasDescriptionChild = React.useMemo(() => { const childrenArray = React.Children.toArray(children); return childrenArray.some((child) => React.isValidElement(child) && child.type === Description); }, [children]); const ariaLabelledBy = (_b = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-labelledby']) !== null && _b !== void 0 ? _b : (hasHeaderChild ? headerId : undefined); const ariaDescribedBy = (_c = htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-describedby']) !== null && _c !== void 0 ? _c : (hasDescriptionChild ? descriptionId : undefined); const dialogRole = variant === 'warning' || variant === 'danger' ? 'alertdialog' : 'dialog'; if (!isOpen) { return null; } if ((htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes['aria-label']) === undefined && !ariaLabelledBy) { console.warn('The Dialog is missing aria-label and Header. Please add one of them for accessibility.'); } const classes = classNames('ndl-dialog', className, { 'ndl-with-close-button': !hasDisabledCloseButton, 'ndl-with-icon': !!variant, }); return (_jsx(Modal, Object.assign({ isOpen: isOpen, onClose: handleClose, container: container, rootProps: rootProps, size: size, htmlAttributes: Object.assign(Object.assign({}, htmlAttributes), { 'aria-describedby': ariaDescribedBy, 'aria-labelledby': ariaLabelledBy, role: dialogRole }), className: classes, style: style, ref: ref }, restProps, { children: _jsxs(DialogContext.Provider, { value: { descriptionId, headerId, setDescriptionId, setHeaderId, variant, }, children: [!hasDisabledCloseButton && (_jsx(CleanIconButton, { size: "medium", className: "ndl-dialog-close", onClick: handleCloseClick, description: null, htmlAttributes: { 'aria-label': 'Close', // TODO v5: remove data-testid 'data-testid': 'ndl-dialog-close', }, children: _jsx(XMarkIconOutline, {}) })), _jsxs("div", { className: "ndl-dialog-content-wrapper", children: [variant && (_jsx("div", { className: `ndl-dialog-type-icon ndl-${variant}`, "data-testid": "ndl-dialog-type-icon", children: typeIcons[variant] })), _jsx("div", { className: "n-flex n-flex-col n-flex-1 n-w-full", children: children })] })] }) }))); }; const Actions = (_a) => { var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]); const { variant } = useDialogContext(); const classes = classNames('ndl-dialog-actions', className); const Component = as !== null && as !== void 0 ? as : 'div'; let childrenToRender = children; // For danger dialogs, we want to convert primary buttons to danger buttons by default if (variant === 'danger') { childrenToRender = React.Children.map(children, (element) => { if (!React.isValidElement(element)) { return; } if (element.type === OutlinedButton || element.type === FilledButton || element.type === TextButton) { const buttonElement = element; if (buttonElement.props.variant === 'primary' || buttonElement.props.variant === undefined) { return React.cloneElement(buttonElement, Object.assign(Object.assign({}, buttonElement.props), { variant: 'danger' })); } } return element; }); } return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: childrenToRender }))); }; const Header = (_a) => { var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]); const { headerId, setHeaderId } = useDialogContext(); const classes = classNames('ndl-dialog-header', className); const Component = as !== null && as !== void 0 ? as : 'span'; useEffect(() => { if (Boolean(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id) === true) { setHeaderId === null || setHeaderId === void 0 ? void 0 : setHeaderId(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id); } }, [htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id, setHeaderId]); return (_jsx(Typography, Object.assign({ as: Component, className: classes, ref: ref, style: style, variant: "title-2" }, restProps, { htmlAttributes: Object.assign(Object.assign({}, htmlAttributes), { id: headerId }), children: children }))); }; const Subtitle = (_a) => { var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]); const classes = classNames('ndl-dialog-subtitle', className); const Component = as !== null && as !== void 0 ? as : 'div'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children }))); }; const Description = (_a) => { var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]); const { descriptionId, setDescriptionId } = useDialogContext(); const classes = classNames('ndl-dialog-description', className); const Component = as !== null && as !== void 0 ? as : 'div'; useEffect(() => { if (Boolean(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id) === true) { setDescriptionId === null || setDescriptionId === void 0 ? void 0 : setDescriptionId(htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id); } }, [htmlAttributes === null || htmlAttributes === void 0 ? void 0 : htmlAttributes.id, setDescriptionId]); return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { id: descriptionId, children: children }))); }; const Content = (_a) => { var { as, children, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "htmlAttributes", "ref"]); const classes = classNames('ndl-dialog-content', className); const Component = as !== null && as !== void 0 ? as : 'div'; return (_jsx(Component, Object.assign({ className: classes, style: style, ref: ref }, restProps, htmlAttributes, { children: children }))); }; const Image = (_a) => { var { src, alt, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["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 }, restProps, htmlAttributes))); }; const Dialog = Object.assign(DialogComponent, { Actions, Content, Description, Header, Image, Subtitle, }); export { Dialog }; //# sourceMappingURL=Dialog.js.map