mirror of
https://github.com/strapi/strapi.git
synced 2025-08-03 06:18:37 +00:00
34 lines
965 B
JavaScript
34 lines
965 B
JavaScript
![]() |
import { useCallback } from 'react';
|
||
|
|
||
|
export const useClipboard = () => {
|
||
|
const copy = useCallback(async (value) => {
|
||
|
try {
|
||
|
// only strings and numbers casted to strings can be copied to clipboard
|
||
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
||
|
throw new Error(`Cannot copy typeof ${typeof value} to clipboard, must be a string`);
|
||
|
}
|
||
|
// empty strings are also considered invalid
|
||
|
else if (value === '') {
|
||
|
throw new Error(`Cannot copy empty string to clipboard.`);
|
||
|
}
|
||
|
|
||
|
const stringifiedValue = value.toString();
|
||
|
|
||
|
await navigator.clipboard.writeText(stringifiedValue);
|
||
|
|
||
|
return true;
|
||
|
} catch (error) {
|
||
|
/**
|
||
|
* Realistically this isn't useful in production as there's nothing the user can do.
|
||
|
*/
|
||
|
if (process.env.NODE_ENV === 'development') {
|
||
|
console.warn('Copy failed', error);
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}, []);
|
||
|
|
||
|
return { copy };
|
||
|
};
|