@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
160 lines • 8.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.ClipboardButton = 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 = __importDefault(require("react"));
const clean_icon_button_1 = require("../clean-icon-button");
const hooks_1 = require("../hooks");
const icon_button_1 = require("../icon-button");
const Square2StackIconOutline_1 = require("../icons/generated/heroIcons/Square2StackIconOutline");
const kbd_1 = require("../kbd");
const outlined_button_1 = require("../outlined-button");
const tooltip_1 = require("../tooltip");
const ConfirmationButton = (_a) => {
var { descriptionKbdProps, description, actionFeedbackText, icon, children, onClick, htmlAttributes, tooltipProps, type = 'clean-icon-button' } = _a, restProps = __rest(_a, ["descriptionKbdProps", "description", "actionFeedbackText", "icon", "children", "onClick", "htmlAttributes", "tooltipProps", "type"]);
const [timeoutId, setTimeoutId] = react_1.default.useState(null);
const [isOpen, setIsOpen] = react_1.default.useState(false);
const handleClick = () => {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
const newTimeoutId = window.setTimeout(() => {
setTimeoutId(null);
}, 2000);
setTimeoutId(newTimeoutId);
};
const handleCloseTooltip = () => {
setIsOpen(false);
};
const handleOpenTooltip = () => {
setIsOpen(true);
};
const content = timeoutId === null ? description : actionFeedbackText;
/* Controlling the tip to make it disappear in a timely manner*/
if (type === 'clean-icon-button') {
return ((0, jsx_runtime_1.jsx)(clean_icon_button_1.CleanIconButton, Object.assign({}, restProps.cleanIconButtonProps, { description: content, descriptionKbdProps: descriptionKbdProps, tooltipProps: {
root: Object.assign(Object.assign({}, tooltipProps), { isOpen: isOpen || timeoutId !== null }),
trigger: {
htmlAttributes: {
onBlur: handleCloseTooltip,
onFocus: handleOpenTooltip,
onMouseEnter: handleOpenTooltip,
onMouseLeave: handleCloseTooltip,
},
},
}, onClick: (event) => {
if (onClick) {
onClick(event);
}
handleClick();
}, className: restProps.className, htmlAttributes: htmlAttributes, children: icon })));
}
else if (type === 'icon-button') {
return ((0, jsx_runtime_1.jsx)(icon_button_1.IconButton, Object.assign({}, restProps.iconButtonProps, { description: content, tooltipProps: {
root: Object.assign(Object.assign({}, tooltipProps), { isOpen: isOpen || timeoutId !== null }),
trigger: {
htmlAttributes: {
onBlur: handleCloseTooltip,
onFocus: handleOpenTooltip,
onMouseEnter: handleOpenTooltip,
onMouseLeave: handleCloseTooltip,
},
},
}, onClick: (event) => {
if (onClick) {
onClick(event);
}
handleClick();
}, className: restProps.className, htmlAttributes: htmlAttributes, children: icon })));
}
else if (type === 'outlined-button') {
return ((0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, Object.assign({ type: "simple", isOpen: isOpen || timeoutId !== null }, tooltipProps, { onOpenChange: (open) => {
var _a;
if (open) {
handleOpenTooltip();
}
else {
handleCloseTooltip();
}
(_a = tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.onOpenChange) === null || _a === void 0 ? void 0 : _a.call(tooltipProps, open);
}, children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, { hasButtonWrapper: true, htmlAttributes: {
'aria-label': content,
onBlur: handleCloseTooltip,
onFocus: handleOpenTooltip,
onMouseEnter: handleOpenTooltip,
onMouseLeave: handleCloseTooltip,
}, children: (0, jsx_runtime_1.jsx)(outlined_button_1.OutlinedButton, Object.assign({ variant: "neutral" }, restProps.buttonProps, { onClick: (event) => {
if (onClick) {
onClick(event);
}
handleClick();
}, leadingVisual: icon, className: restProps.className, htmlAttributes: htmlAttributes, children: children })) }), (0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip.Content, { children: [content, descriptionKbdProps && (0, jsx_runtime_1.jsx)(kbd_1.Kbd, Object.assign({}, descriptionKbdProps))] })] })));
}
};
const ClipboardButton = ({ textToCopy, descriptionKbdProps, isDisabled, size, tooltipProps, htmlAttributes, type, }) => {
const [, copyFunction] = (0, hooks_1.useCopyToClipboard)();
const isOutlinedButton = type === 'outlined-button';
const isIconButton = type === 'icon-button';
const props = isOutlinedButton
? {
outlinedButtonProps: {
isDisabled,
size,
},
type: 'outlined-button',
}
: isIconButton
? {
iconButtonProps: {
description: 'Copy to clipboard',
isDisabled,
size,
},
type: 'icon-button',
}
: {
cleanIconButtonProps: {
description: 'Copy to clipboard',
isDisabled,
size,
},
type: 'clean-icon-button',
};
return ((0, jsx_runtime_1.jsx)(ConfirmationButton, Object.assign({ onClick: () => copyFunction(textToCopy), description: "Copy to clipboard", actionFeedbackText: "Copied", descriptionKbdProps: descriptionKbdProps }, props, { tooltipProps: tooltipProps, className: "n-gap-token-8", icon: (0, jsx_runtime_1.jsx)(Square2StackIconOutline_1.Square2StackIconOutline, { className: "ndl-icon-svg" }), htmlAttributes: Object.assign({ 'aria-live': 'polite' }, htmlAttributes), children: type === 'outlined-button' && 'Copy' })));
};
exports.ClipboardButton = ClipboardButton;
//# sourceMappingURL=ClipboardButton.js.map