@coreui/react-pro
Version:
UI Components Library for React.js
41 lines (38 loc) • 1.33 kB
JavaScript
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
import { useState, useCallback } from 'react';
/**
* useClipboard Hook
*
* Provides functionality to copy text to the clipboard and track the copy status.
*
* @returns An object containing the copy function, copy status, and any error encountered.
*/
const useClipboard = () => {
const [isCopied, setIsCopied] = useState(false);
const [error, setError] = useState(null);
/**
* Copies the provided text to the clipboard.
*
* @param text - The text to be copied to the clipboard.
*/
const copy = useCallback((text) => __awaiter(void 0, void 0, void 0, function* () {
if (!(navigator === null || navigator === void 0 ? void 0 : navigator.clipboard)) {
setError(new Error('Clipboard API is not available'));
return;
}
try {
yield navigator.clipboard.writeText(text);
setIsCopied(true);
setError(null);
// Reset the isCopied state after 2 seconds
setTimeout(() => setIsCopied(false), 2000);
}
catch (_error) {
setError(_error);
setIsCopied(false);
}
}), []);
return { copy, isCopied, error };
};
export { useClipboard };
//# sourceMappingURL=useClipboard.js.map