react-use
Version:
Collection of React Hooks
62 lines (61 loc) • 2.21 kB
JavaScript
import writeText from 'copy-to-clipboard';
import { useCallback } from 'react';
import useMountedState from './useMountedState';
import useSetState from './useSetState';
var useCopyToClipboard = function () {
var isMounted = useMountedState();
var _a = useSetState({
value: undefined,
error: undefined,
noUserInteraction: true,
}), state = _a[0], setState = _a[1];
var copyToClipboard = useCallback(function (value) {
if (!isMounted()) {
return;
}
var noUserInteraction;
var normalizedValue;
try {
// only strings and numbers casted to strings can be copied to clipboard
if (typeof value !== 'string' && typeof value !== 'number') {
var error = new Error("Cannot copy typeof " + typeof value + " to clipboard, must be a string");
if (process.env.NODE_ENV === 'development')
console.error(error);
setState({
value: value,
error: error,
noUserInteraction: true,
});
return;
}
// empty strings are also considered invalid
else if (value === '') {
var error = new Error("Cannot copy empty string to clipboard.");
if (process.env.NODE_ENV === 'development')
console.error(error);
setState({
value: value,
error: error,
noUserInteraction: true,
});
return;
}
normalizedValue = value.toString();
noUserInteraction = writeText(normalizedValue);
setState({
value: normalizedValue,
error: undefined,
noUserInteraction: noUserInteraction,
});
}
catch (error) {
setState({
value: normalizedValue,
error: error,
noUserInteraction: noUserInteraction,
});
}
}, []);
return [state, copyToClipboard];
};
export default useCopyToClipboard;