UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

96 lines 4.55 kB
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; }; import { jsx as _jsx, jsxs as _jsxs } from "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/>. */ import React from 'react'; import { Button } from '../button'; import { useCopyToClipboard } from '../hooks'; import { IconButton } from '../icon-button'; import { Square2StackIconOutline } from '../icons'; import { Tooltip } from '../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.useState(null); const [isOpen, setIsOpen] = React.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*/ _jsxs(Tooltip, Object.assign({ isOpen: isOpen || timeoutId !== null, type: "simple" }, tooltipProps, { children: [_jsx(Tooltip.Trigger, { htmlAttributes: { onMouseEnter: handleOpenTooltip, onMouseLeave: handleCloseTooltip, onFocus: handleOpenTooltip, onBlur: handleCloseTooltip, }, hasButtonWrapper: true, children: isIcon ? (_jsx(IconButton, Object.assign({}, restProps.iconButtonProps, { onClick: (event) => { if (onClick) onClick(event); handleClick(); }, className: restProps.className, children: children }))) : (_jsx(Button, Object.assign({}, restProps.buttonProps, { onClick: (event) => { if (onClick) onClick(event); handleClick(); }, className: restProps.className, children: children }))) }), _jsx(Tooltip.Content, { children: content })] }))); }; export const ClipboardButton = ({ textToCopy, isDisabled, size, tooltipProps, htmlAttributes, isIconButton = true, isClean = true, }) => { const [, copyFunction] = useCopyToClipboard(); return (_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: [_jsx(Square2StackIconOutline, { className: "ndl-icon-svg" }), isIconButton !== true && 'Copy'] }))); }; //# sourceMappingURL=ClipboardButton.js.map