mirror of
https://github.com/strapi/strapi.git
synced 2025-08-02 13:58:18 +00:00

Co-authored-by: Fernando Chávez <fc9chavez@gmail.com> Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com> Co-authored-by: markkaylor <mark.kaylor@strapi.io> Co-authored-by: Mark Kaylor <mark.kaylor@strapi.io> Co-authored-by: Madhuri Sandbhor <madhurisandbhor@gmail.com> Co-authored-by: Jackson Hammond <malts18@gmail.com> Co-authored-by: yurimutti <muttiyuri@gmail.com> Co-authored-by: Fernando Chávez <fernando.chavez@strapi.io> Co-authored-by: GJunior <samaritanojr006@gmail.com> Co-authored-by: Milan K <57148574+mkcy3@users.noreply.github.com> Co-authored-by: Rémi de Juvigny <remi.dejuvigny@strapi.io> Co-authored-by: boris.shulyak <borysshulyak@gmail.com> Co-authored-by: Alec Winter <34278963+nolliebigspin@users.noreply.github.com> Co-authored-by: Boris Shulyak <55171497+BorysShulyak@users.noreply.github.com> Co-authored-by: 0xATHERIS <ath3ris@proton.me> Co-authored-by: Andrew <112592168+fletch-r@users.noreply.github.com> Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com> Co-authored-by: Dallas Clark <542948+dallasclark@users.noreply.github.com> Co-authored-by: Okorare Tega <contact@tegacreatives.com> Co-authored-by: Júlíus Guðni <julius@extis.one> Co-authored-by: Simone <startae14@gmail.com>
34 lines
982 B
TypeScript
34 lines
982 B
TypeScript
import { useCallback } from 'react';
|
|
|
|
export const useClipboard = () => {
|
|
const copy = useCallback(async (value: string | number) => {
|
|
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 };
|
|
};
|