2020-10-31 20:16:41 +00:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import styled, { keyframes } from 'styled-components/macro';
|
|
|
|
import Fade from '@/components/elements/Fade';
|
|
|
|
import { SwitchTransition } from 'react-transition-group';
|
|
|
|
|
|
|
|
const fade = keyframes`
|
|
|
|
from { opacity: 0 }
|
|
|
|
to { opacity: 1 }
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Toast = styled.div`
|
|
|
|
${tw`fixed z-50 bottom-0 left-0 mb-4 w-full flex justify-end pr-4`};
|
|
|
|
animation: ${fade} 250ms linear;
|
|
|
|
|
|
|
|
& > div {
|
2020-11-09 01:13:33 +00:00
|
|
|
${tw`rounded px-4 py-2 text-white bg-neutral-900 border border-black opacity-75`};
|
2020-10-31 20:16:41 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-03-06 23:08:42 +00:00
|
|
|
const CopyOnClick: React.FC<{ text: any, hideTextInToast?: boolean }> = ({ text, hideTextInToast, children }) => {
|
2020-10-31 20:16:41 +00:00
|
|
|
const [ copied, setCopied ] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!copied) return;
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
setCopied(false);
|
|
|
|
}, 2500);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
}, [ copied ]);
|
|
|
|
|
|
|
|
const onCopy = useCallback(() => {
|
|
|
|
setCopied(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SwitchTransition>
|
|
|
|
<Fade timeout={250} key={copied ? 'visible' : 'invisible'}>
|
|
|
|
{copied ?
|
|
|
|
<Toast>
|
|
|
|
<div>
|
2021-03-06 23:08:42 +00:00
|
|
|
{hideTextInToast ?
|
|
|
|
<p>Copied text to clipboard.</p>
|
|
|
|
:
|
|
|
|
<p>Copied "{text}" to clipboard.</p>
|
|
|
|
}
|
2020-10-31 20:16:41 +00:00
|
|
|
</div>
|
|
|
|
</Toast>
|
|
|
|
:
|
|
|
|
<></>
|
|
|
|
}
|
|
|
|
</Fade>
|
|
|
|
</SwitchTransition>
|
2020-11-09 01:13:55 +00:00
|
|
|
<CopyToClipboard onCopy={onCopy} text={text} options={{ debug: true }} css={tw`cursor-pointer`}>
|
2020-10-31 20:16:41 +00:00
|
|
|
{children}
|
|
|
|
</CopyToClipboard>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CopyOnClick;
|