@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
238 lines • 14.4 kB
JavaScript
"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.Dialog = 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 clean_icon_button_1 = require("../clean-icon-button");
const filled_button_1 = require("../filled-button");
const ExclamationCircleIconOutline_1 = require("../icons/generated/heroIcons/ExclamationCircleIconOutline");
const ExclamationTriangleIconOutline_1 = require("../icons/generated/heroIcons/ExclamationTriangleIconOutline");
const InformationCircleIconOutline_1 = require("../icons/generated/heroIcons/InformationCircleIconOutline");
const XMarkIconOutline_1 = require("../icons/generated/heroIcons/XMarkIconOutline");
const modal_1 = require("../modal");
const outlined_button_1 = require("../outlined-button");
const text_button_1 = require("../text-button");
const typography_1 = require("../typography");
const dialog_context_1 = require("./dialog-context");
const dialog_types_1 = require("./dialog-types");
const convertCloseReason = (reason) => {
const dialogReason = reason;
if (Object.values(dialog_types_1.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: ((0, jsx_runtime_1.jsx)(ExclamationCircleIconOutline_1.ExclamationCircleIconOutline, { htmlAttributes: {
'aria-hidden': undefined,
'aria-label': 'Danger',
strokeWidth: 3,
} })),
info: ((0, jsx_runtime_1.jsx)(InformationCircleIconOutline_1.InformationCircleIconOutline, { htmlAttributes: {
'aria-hidden': undefined,
'aria-label': 'Information',
strokeWidth: 3,
} })),
warning: ((0, jsx_runtime_1.jsx)(ExclamationTriangleIconOutline_1.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 = (0, react_1.useId)();
const generatedHeaderId = (0, react_1.useId)();
const [descriptionId, setDescriptionId] = (0, react_1.useState)(generatedDescriptionId);
const [headerId, setHeaderId] = (0, react_1.useState)(generatedHeaderId);
const handleCloseClick = (e) => {
e.preventDefault();
onClose === null || onClose === void 0 ? void 0 : onClose(e, dialog_types_1.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 !== undefined ? convertCloseReason(reason) : undefined);
};
const hasHeaderChild = react_1.default.useMemo(() => {
const childrenArray = react_1.default.Children.toArray(children);
return childrenArray.some((child) => react_1.default.isValidElement(child) && child.type === Header);
}, [children]);
const hasDescriptionChild = react_1.default.useMemo(() => {
const childrenArray = react_1.default.Children.toArray(children);
return childrenArray.some((child) => react_1.default.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 = (0, classnames_1.default)('ndl-dialog', className, {
'ndl-with-close-button': !hasDisabledCloseButton,
'ndl-with-icon': !!variant,
});
return ((0, jsx_runtime_1.jsx)(modal_1.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: (0, jsx_runtime_1.jsxs)(dialog_context_1.DialogContext.Provider, { value: {
descriptionId,
headerId,
setDescriptionId,
setHeaderId,
variant,
}, children: [!hasDisabledCloseButton && ((0, jsx_runtime_1.jsx)(clean_icon_button_1.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: (0, jsx_runtime_1.jsx)(XMarkIconOutline_1.XMarkIconOutline, {}) })), (0, jsx_runtime_1.jsxs)("div", { className: "ndl-dialog-content-wrapper", children: [variant && ((0, jsx_runtime_1.jsx)("div", { className: `ndl-dialog-type-icon ndl-${variant}`, "data-testid": "ndl-dialog-type-icon", children: typeIcons[variant] })), (0, jsx_runtime_1.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 } = (0, dialog_context_1.useDialogContext)();
const classes = (0, classnames_1.default)('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_1.default.Children.map(children, (element) => {
if (!react_1.default.isValidElement(element)) {
return;
}
if (element.type === outlined_button_1.OutlinedButton ||
element.type === filled_button_1.FilledButton ||
element.type === text_button_1.TextButton) {
const buttonElement = element;
if (buttonElement.props.variant === 'primary' ||
buttonElement.props.variant === undefined) {
return react_1.default.cloneElement(buttonElement, Object.assign(Object.assign({}, buttonElement.props), { variant: 'danger' }));
}
}
return element;
});
}
return ((0, jsx_runtime_1.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 } = (0, dialog_context_1.useDialogContext)();
const classes = (0, classnames_1.default)('ndl-dialog-header', className);
const Component = as !== null && as !== void 0 ? as : 'span';
(0, react_1.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 ((0, jsx_runtime_1.jsx)(typography_1.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 = (0, classnames_1.default)('ndl-dialog-subtitle', className);
const Component = as !== null && as !== void 0 ? as : 'div';
return ((0, jsx_runtime_1.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 } = (0, dialog_context_1.useDialogContext)();
const classes = (0, classnames_1.default)('ndl-dialog-description', className);
const Component = as !== null && as !== void 0 ? as : 'div';
(0, react_1.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 ((0, jsx_runtime_1.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 = (0, classnames_1.default)('ndl-dialog-content', className);
const Component = as !== null && as !== void 0 ? as : 'div';
return ((0, jsx_runtime_1.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 = (0, classnames_1.default)('ndl-dialog-image', className);
return ((0, jsx_runtime_1.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,
});
exports.Dialog = Dialog;
//# sourceMappingURL=Dialog.js.map