Files
ncd-fe/packages/pl-fe/src/utils/copy.ts
marcin mikołajczak 7609a7e2a7 pl-fe: remove trends reducers
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-11-09 13:58:55 +01:00

32 lines
615 B
TypeScript

const copy = (text: string, onSuccess?: () => void) => {
if (navigator.clipboard) {
navigator.clipboard.writeText(text);
if (onSuccess) {
onSuccess();
}
} else {
const textarea = document.createElement('textarea');
textarea.textContent = text;
textarea.style.position = 'fixed';
document.body.appendChild(textarea);
try {
textarea.select();
document.execCommand('copy');
} catch {
// Do nothing
} finally {
document.body.removeChild(textarea);
if (onSuccess) {
onSuccess();
}
}
}
};
export { copy as default };