snort/src/useCopy.ts

21 lines
458 B
TypeScript
Raw Normal View History

2023-01-06 15:22:02 +00:00
import { useState } from 'react'
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)
} catch (error) {
setError(true)
}
setTimeout(() => setCopied(false), timeout);
};
return { error, copied, copy };
};