You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
655 B
29 lines
655 B
4 years ago
|
import { useState, useCallback } from 'react';
|
||
|
import copyToClipboard from 'copy-to-clipboard';
|
||
|
import { useTimeout } from './useTimeout';
|
||
4 years ago
|
|
||
|
/**
|
||
|
* useCopyToClipboard
|
||
|
*
|
||
4 years ago
|
* @param {number} [delay=3000] - delay in ms
|
||
4 years ago
|
*
|
||
|
* @return {[boolean, Function]}
|
||
|
*/
|
||
4 years ago
|
const DEFAULT_DELAY = 3000;
|
||
4 years ago
|
|
||
4 years ago
|
export function useCopyToClipboard(delay = DEFAULT_DELAY) {
|
||
4 years ago
|
const [copied, setCopied] = useState(false);
|
||
|
const startTimeout = useTimeout(() => setCopied(false), delay, false);
|
||
4 years ago
|
|
||
|
const handleCopy = useCallback(
|
||
|
(text) => {
|
||
4 years ago
|
setCopied(true);
|
||
|
startTimeout();
|
||
|
copyToClipboard(text);
|
||
4 years ago
|
},
|
||
|
[startTimeout],
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
4 years ago
|
return [copied, handleCopy];
|
||
4 years ago
|
}
|