UNPKG

@veracity/vui

Version:

Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.

73 lines (71 loc) 2.21 kB
import { cs } from "../utils/styles.js"; import vui from "../core/vui.js"; import { Button } from "../button/button.js"; import { Tooltip } from "../tooltip/tooltip.js"; import { useCopyToClipboard } from "./useCopyToClipboard.js"; import { useEffect, useRef, useState } from "react"; import { jsx } from "react/jsx-runtime"; //#region src/copyToClipboard/copyToClipboard.tsx const defaultIcon = "uiCopy"; const checkedIcon = "uiCheck"; const getCopyValue = (props) => { if (props.label !== void 0) return props.label; return props.copyText; }; /** * A helper button component. * * Copies the provided text to clipboard. * * Copying is available only for a secure environment (https://), otherwise the button is disabled. * */ const CopyToClipboard = vui((props, ref) => { const value = getCopyValue(props); const { disabled, copyText, label, className, icon = defaultIcon, size = "sm", variant = "tertiary", intent = "brand", showTooltip = true, ...rest } = props; const [state, setState] = useState("idle"); const resetTimerRef = useRef(void 0); useEffect(() => () => { if (resetTimerRef.current !== void 0) clearTimeout(resetTimerRef.current); }, []); const texts = { idle: { label: `Copy ${value}`, icon }, copied: { label: `Copied ${value}`, icon: checkedIcon } }; const { copy, isCopyDisabled } = useCopyToClipboard(value, { showToast: false }); const button = /* @__PURE__ */ jsx(Button, { "aria-label": texts[state].label, className: cs("vui-copy-to-clipboard", className), disabled: disabled || isCopyDisabled, icon: texts[state].icon, intent, onClick: () => { copy(); setState("copied"); if (resetTimerRef.current !== void 0) clearTimeout(resetTimerRef.current); resetTimerRef.current = setTimeout(() => { setState("idle"); resetTimerRef.current = void 0; }, 1e3); }, ref, size, variant, ...rest }); return showTooltip ? /* @__PURE__ */ jsx(Tooltip, { text: texts[state].label, children: button }) : button; }); CopyToClipboard.displayName = "CopyToClipboard"; //#endregion export { CopyToClipboard, CopyToClipboard as default }; globalThis.__vuiVersion__ = "5.4.0-alpha" //# sourceMappingURL=copyToClipboard.js.map