@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
117 lines • 7.45 kB
JavaScript
;
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;
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlockWrapper = 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 button_1 = require("../button");
const icons_1 = require("../icons");
const defaultImports_1 = require("./defaultImports");
exports.CodeBlockWrapper = react_1.default.forwardRef(function CodeBlockWrapper({ as, maxHeight, code, headerTitle, children, isDisabled, actions, shouldShowExpandButton, setShouldShowExpandButton, className, style, htmlAttributes, }, ref) {
var _a;
const Component = as !== null && as !== void 0 ? as : 'div';
const syntaxHighlighterWrapperRef = (0, react_1.useRef)(null);
const [containerHeight, setContainerHeight] = (0, react_1.useState)(`${maxHeight}px`);
const [isExpanded, setIsExpanded] = (0, react_1.useState)(maxHeight === undefined);
(0, react_1.useEffect)(() => {
var _a, _b, _c, _d;
if (((_b = (_a = syntaxHighlighterWrapperRef.current) === null || _a === void 0 ? void 0 : _a.scrollHeight) !== null && _b !== void 0 ? _b : 0) <=
((_d = (_c = syntaxHighlighterWrapperRef.current) === null || _c === void 0 ? void 0 : _c.clientHeight) !== null && _d !== void 0 ? _d : 0)) {
setIsExpanded(true);
}
else {
setIsExpanded(false);
}
}, []);
(0, react_1.useLayoutEffect)(() => {
var _a, _b, _c, _d;
if (((_b = (_a = syntaxHighlighterWrapperRef.current) === null || _a === void 0 ? void 0 : _a.clientHeight) !== null && _b !== void 0 ? _b : 0) >=
((_d = (_c = syntaxHighlighterWrapperRef.current) === null || _c === void 0 ? void 0 : _c.scrollHeight) !== null && _d !== void 0 ? _d : 0)) {
setContainerHeight(`fit-content`);
setShouldShowExpandButton(false);
}
else {
setContainerHeight(`${maxHeight}px`);
setShouldShowExpandButton(true);
}
}, [maxHeight, code, setShouldShowExpandButton]);
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, className: (0, defaultImports_1.classNames)('ndl-code-block-container', className), style: Object.assign({ height: containerHeight }, style) }, htmlAttributes, { children: [(0, jsx_runtime_1.jsxs)("div", { className: "n-flex n-flex-col n-h-full", children: [Boolean(headerTitle) === true && ((0, jsx_runtime_1.jsx)("div", { className: (0, defaultImports_1.classNames)('ndl-code-block-title', {
'ndl-disabled': isDisabled,
}), style: {
maxWidth: `calc(100% - ${((_a = actions === null || actions === void 0 ? void 0 : actions.length) !== null && _a !== void 0 ? _a : 0) * 36 + 20}px)`,
}, "data-testid": "ndl-code-block-title", children: headerTitle })), (0, jsx_runtime_1.jsxs)("div", { className: (0, defaultImports_1.classNames)('ndl-code-content-container', {
'ndl-disabled': isDisabled,
}), style: {
height: Boolean(headerTitle) === true
? 'calc(100% - 24px - 12px)'
: '100%',
}, children: [(0, jsx_runtime_1.jsx)("div", { className: "ndl-code-pseudo-element" }), (0, jsx_runtime_1.jsx)("div", { ref: syntaxHighlighterWrapperRef, className: "ndl-highlight-wrapper", role: "textbox", "aria-label": "code-snippet", tabIndex: 0, children: children }), (0, jsx_runtime_1.jsx)("div", { className: "ndl-code-pseudo-element" })] })] }), (0, jsx_runtime_1.jsx)("div", { className: (0, defaultImports_1.classNames)('ndl-code-block-actions', {
'ndl-disabled': isDisabled,
}), children: actions === null || actions === void 0 ? void 0 : actions.map((iconButtonProps, i) => {
const { htmlAttributes, ariaLabel } = iconButtonProps, restIconButtonProps = __rest(iconButtonProps, ["htmlAttributes", "ariaLabel"]);
return ((0, jsx_runtime_1.jsx)(button_1.IconButton, Object.assign({ isClean: true, ariaLabel: `${ariaLabel || 'CodeBlock Action'}`, isDisabled: isDisabled, htmlAttributes: Object.assign({ 'data-testid': `ndl-action-button-${i}` }, htmlAttributes) }, restIconButtonProps), i));
}) }), shouldShowExpandButton && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-code-block-expand-button", children: (0, jsx_runtime_1.jsx)(button_1.IconButton, { ariaLabel: "Expand/Collapse", onClick: () => {
if (isExpanded) {
setContainerHeight(`${maxHeight}px`);
setIsExpanded(false);
}
else {
setContainerHeight(`fit-content`);
setIsExpanded(true);
}
}, isClean: true, children: isExpanded ? (0, jsx_runtime_1.jsx)(icons_1.ChevronUpIconOutline, {}) : (0, jsx_runtime_1.jsx)(icons_1.ChevronDownIconOutline, {}) }) }))] })));
});
//# sourceMappingURL=CodeBlockWrapper.js.map