@coreui/react-pro
Version:
UI Components Library for React.js
43 lines (39 loc) • 1.37 kB
JavaScript
;
var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
var React = require('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] = React.useState(false);
const [error, setError] = React.useState(null);
/**
* Copies the provided text to the clipboard.
*
* @param text - The text to be copied to the clipboard.
*/
const copy = React.useCallback((text) => tslib_es6.__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 };
};
exports.useClipboard = useClipboard;
//# sourceMappingURL=useClipboard.js.map