1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-05 15:35:46 +01:00
metamask-extension/ui/hooks/useCopyToClipboard.js

29 lines
713 B
JavaScript
Raw Normal View History

import { useState, useCallback } from 'react';
import copyToClipboard from 'copy-to-clipboard';
import { SECOND } from '../../shared/constants/time';
import { useTimeout } from './useTimeout';
/**
* useCopyToClipboard
*
* @param {number} [delay=3000] - delay in ms
* @returns {[boolean, Function]}
*/
const DEFAULT_DELAY = SECOND * 3;
2020-11-03 00:41:28 +01:00
export function useCopyToClipboard(delay = DEFAULT_DELAY) {
const [copied, setCopied] = useState(false);
const startTimeout = useTimeout(() => setCopied(false), delay, false);
const handleCopy = useCallback(
(text) => {
setCopied(true);
startTimeout();
copyToClipboard(text);
},
[startTimeout],
);
return [copied, handleCopy];
}