@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
94 lines • 7.09 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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlock = 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_syntax_highlighter_1 = require("react-syntax-highlighter");
const clean_icon_button_1 = require("../clean-icon-button");
const ChevronDownIconOutline_1 = require("../icons/generated/heroIcons/ChevronDownIconOutline");
const ChevronUpIconOutline_1 = require("../icons/generated/heroIcons/ChevronUpIconOutline");
const use_code_language_1 = require("./hooks/use-code-language");
const use_code_theme_1 = require("./hooks/use-code-theme");
const use_expandable_1 = require("./hooks/use-expandable");
const defaultElement = 'div';
const CodeBlock = (_a) => {
var _b, _c;
var { as, maxHeight, code, language, shouldShowLineNumbers, theme, heading, actions, isDisabled, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "maxHeight", "code", "language", "shouldShowLineNumbers", "theme", "heading", "actions", "isDisabled", "className", "style", "htmlAttributes", "ref"])
// TODO: Should not be polymorphic (at least not defaulting to a button)
;
const Component = as !== null && as !== void 0 ? as : defaultElement;
const { containerHeight, isExpanded, toggleExpand, hasOverflowingContent, ref: syntaxHighlighterWrapperRef, } = (0, use_expandable_1.useExpandable)(maxHeight, code);
const codeTheme = (0, use_code_theme_1.useCodeTheme)(theme);
const codeLanguage = (0, use_code_language_1.useCodeLanguage)(language);
const hasMaxHeight = maxHeight !== undefined;
const hasHeading = Boolean(heading);
const numberOfActions = (_b = actions === null || actions === void 0 ? void 0 : actions.length) !== null && _b !== void 0 ? _b : 0;
const shouldShowExpandButton = hasMaxHeight && hasOverflowingContent;
const rightPadding = hasHeading
? 0
: Math.max(numberOfActions * 36, (shouldShowExpandButton ? 1 : 0) * 36) + 8;
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, className: (0, classnames_1.default)('ndl-code-block-container', className), style: Object.assign({ height: containerHeight }, style) }, restProps, htmlAttributes, { children: [(0, jsx_runtime_1.jsxs)("div", { className: "n-flex n-flex-col n-h-full", children: [hasHeading && ((0, jsx_runtime_1.jsx)("div", { className: (0, classnames_1.default)('ndl-code-block-title', {
'ndl-disabled': isDisabled,
}), style: {
maxWidth: `calc(100% - ${((_c = actions === null || actions === void 0 ? void 0 : actions.length) !== null && _c !== void 0 ? _c : 0) * 36 + 20}px)`,
}, "data-testid": "ndl-code-block-title", children: heading })), (0, jsx_runtime_1.jsx)("div", { className: (0, classnames_1.default)('ndl-code-content-container', {
'ndl-disabled': isDisabled,
}), style: {
height: hasHeading ? 'calc(100% - 24px - 12px)' : '100%',
}, children: (0, jsx_runtime_1.jsx)("div", { ref: syntaxHighlighterWrapperRef, className: "ndl-highlight-wrapper", tabIndex: 0, role: "region", "aria-label": `Code content ${isDisabled ? '(disabled)' : ''}`, children: (0, jsx_runtime_1.jsx)(react_syntax_highlighter_1.PrismLight, { language: codeLanguage, style: Object.assign(Object.assign({}, codeTheme), { 'pre[class*="language-"]': {
border: 0,
color: 'var(--theme-color-neutral-text-default)',
lineHeight: '1',
padding: `0 ${rightPadding}px 0.75em 0.75em`,
width: 'fit-content',
} }), codeTagProps: { className: 'n-code' },
// Turn on 'shouldShowLineNumbers' & 'wrapLongLines' at the same time, the display is wrong
// https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/402
// wrapLongLines
showLineNumbers: shouldShowLineNumbers, children: code }) }) })] }), (0, jsx_runtime_1.jsx)("div", { className: (0, classnames_1.default)('ndl-code-block-actions', {
'ndl-disabled': isDisabled,
}), children: actions === null || actions === void 0 ? void 0 : actions.map((iconButtonProps, i) => {
var _a;
return ((0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, Object.assign({ isDisabled: isDisabled }, iconButtonProps, { description: `${(_a = iconButtonProps.description) !== null && _a !== void 0 ? _a : 'CodeBlock Action'}`, htmlAttributes: Object.assign({ 'data-testid': `ndl-action-button-${i}` }, iconButtonProps.htmlAttributes) }), i));
}) }), shouldShowExpandButton && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-code-block-expand-button", children: (0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, { description: isExpanded ? 'Collapse' : 'Expand', tooltipProps: {
root: {
shouldCloseOnReferenceClick: true,
},
}, onClick: toggleExpand, children: isExpanded ? (0, jsx_runtime_1.jsx)(ChevronUpIconOutline_1.ChevronUpIconOutline, {}) : (0, jsx_runtime_1.jsx)(ChevronDownIconOutline_1.ChevronDownIconOutline, {}) }) }))] })));
};
exports.CodeBlock = CodeBlock;
//# sourceMappingURL=CodeBlock.js.map