UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

151 lines 7.08 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 { CleanIconButton } from '../clean-icon-button'; import { useCopyToClipboard } from '../hooks'; import { IconButton } from '../icon-button'; import { Square2StackIconOutline } from '../icons'; import { OutlinedButton } from '../outlined-button'; import { Tooltip } from '../tooltip'; const ConfirmationButton = (_a) => { var { description, actionFeedbackText, icon, children, onClick, htmlAttributes, tooltipProps, type = 'clean-icon-button' } = _a, restProps = __rest(_a, ["description", "actionFeedbackText", "icon", "children", "onClick", "htmlAttributes", "tooltipProps", "type"]); 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 ? description : actionFeedbackText; /* Controlling the tip to make it disappear in a timely manner*/ if (type === 'clean-icon-button') { return (_jsx(CleanIconButton, Object.assign({}, restProps.cleanIconButtonProps, { 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 === 'icon-button') { return (_jsx(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 (_jsxs(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: [_jsx(Tooltip.Trigger, { hasButtonWrapper: true, htmlAttributes: { onBlur: handleCloseTooltip, onFocus: handleOpenTooltip, onMouseEnter: handleOpenTooltip, onMouseLeave: handleCloseTooltip, }, children: _jsx(OutlinedButton, Object.assign({ variant: "neutral" }, restProps.buttonProps, { onClick: (event) => { if (onClick) { onClick(event); } handleClick(); }, leadingVisual: icon, className: restProps.className, htmlAttributes: htmlAttributes, children: children })) }), _jsx(Tooltip.Content, { children: content })] }))); } }; export const ClipboardButton = ({ textToCopy, isDisabled, size, tooltipProps, htmlAttributes, type, }) => { const [, copyFunction] = 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 (_jsx(ConfirmationButton, Object.assign({ onClick: () => copyFunction(textToCopy), description: "Copy to clipboard", actionFeedbackText: "Copied" }, props, { tooltipProps: tooltipProps, className: "n-gap-token-8", icon: _jsx(Square2StackIconOutline, { className: "ndl-icon-svg" }), htmlAttributes: htmlAttributes, children: type === 'outlined-button' && 'Copy' }))); }; //# sourceMappingURL=ClipboardButton.js.map