UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

103 lines 5.05 kB
"use strict"; 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 button_1 = require("../button"); const hooks_1 = require("../hooks"); const icon_button_1 = require("../icon-button"); const icons_1 = require("../icons"); const tooltip_1 = require("../tooltip"); const ConfirmationButton = (_a) => { var { hoverText, feedbackText, isIcon, tooltipProps, children, onClick } = _a, restProps = __rest(_a, ["hoverText", "feedbackText", "isIcon", "tooltipProps", "children", "onClick"]); 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 ? hoverText : feedbackText; return ( /* Controlling the tip to make it disappear in a timely manner*/ (0, jsx_runtime_1.jsxs)(tooltip_1.Tooltip, Object.assign({ isOpen: isOpen || timeoutId !== null, type: "simple" }, tooltipProps, { children: [(0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Trigger, { htmlAttributes: { onMouseEnter: handleOpenTooltip, onMouseLeave: handleCloseTooltip, onFocus: handleOpenTooltip, onBlur: handleCloseTooltip, }, hasButtonWrapper: true, children: isIcon ? ((0, jsx_runtime_1.jsx)(icon_button_1.IconButton, Object.assign({}, restProps.iconButtonProps, { onClick: (event) => { if (onClick) onClick(event); handleClick(); }, className: restProps.className, children: children }))) : ((0, jsx_runtime_1.jsx)(button_1.Button, Object.assign({}, restProps.buttonProps, { onClick: (event) => { if (onClick) onClick(event); handleClick(); }, className: restProps.className, children: children }))) }), (0, jsx_runtime_1.jsx)(tooltip_1.Tooltip.Content, { children: content })] }))); }; const ClipboardButton = ({ textToCopy, isDisabled, size, tooltipProps, htmlAttributes, isIconButton = true, isClean = true, }) => { const [, copyFunction] = (0, hooks_1.useCopyToClipboard)(); return ((0, jsx_runtime_1.jsxs)(ConfirmationButton, Object.assign({ onClick: () => copyFunction(textToCopy), hoverText: "Copy to clipboard", feedbackText: "Copied", isIcon: isIconButton }, (isIconButton === true ? { iconButtonProps: { ariaLabel: 'Copy to clipboard', isClean: isClean, isDisabled: isDisabled, size: size, }, } : { buttonProps: { color: 'neutral', fill: 'outlined', isDisabled: isDisabled, size: size, }, }), { tooltipProps: tooltipProps, className: "n-gap-token-4" }, htmlAttributes, { children: [(0, jsx_runtime_1.jsx)(icons_1.Square2StackIconOutline, { className: "ndl-icon-svg" }), isIconButton !== true && 'Copy'] }))); }; exports.ClipboardButton = ClipboardButton; //# sourceMappingURL=ClipboardButton.js.map