snort/src/useCopy.ts

21 lines
462 B
TypeScript
Raw Normal View History

import { useState } from "react";
2023-01-06 15:22:02 +00:00
export const useCopy = (timeout = 2000) => {
const [error, setError] = useState(false);
const [copied, setCopied] = useState(false);
2023-01-16 17:48:25 +00:00
const copy = async (text: string) => {
2023-01-06 15:22:02 +00:00
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setError(false);
2023-01-06 15:22:02 +00:00
} catch (error) {
setError(true);
2023-01-06 15:22:02 +00:00
}
setTimeout(() => setCopied(false), timeout);
};
return { error, copied, copy };
};