UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

32 lines (31 loc) 1.03 kB
"use client"; let react = require("react"); //#region packages/@mantine/hooks/src/use-clipboard/use-clipboard.ts function useClipboard(options = { timeout: 2e3 }) { const [error, setError] = (0, react.useState)(null); const [copied, setCopied] = (0, react.useState)(false); const [copyTimeout, setCopyTimeout] = (0, react.useState)(null); const handleCopyResult = (value) => { window.clearTimeout(copyTimeout); setCopyTimeout(window.setTimeout(() => setCopied(false), options.timeout)); setCopied(value); }; const copy = (value) => { if ("clipboard" in navigator) navigator.clipboard.writeText(value).then(() => handleCopyResult(true)).catch((err) => setError(err)); else setError(/* @__PURE__ */ new Error("useClipboard: navigator.clipboard is not supported")); }; const reset = () => { setCopied(false); setError(null); window.clearTimeout(copyTimeout); }; return { copy, reset, error, copied }; } //#endregion exports.useClipboard = useClipboard; //# sourceMappingURL=use-clipboard.cjs.map