|
@@ -1,6 +1,5 @@
|
|
|
-import React, { useState } from "react";
|
|
|
+import React, { useState, useRef } from "react";
|
|
|
import styled from "styled-components";
|
|
|
-import useCopying from "./useCopying";
|
|
|
|
|
|
const Container = styled.div`
|
|
|
position: relative;
|
|
@@ -26,24 +25,42 @@ const ToolTip = styled.span`
|
|
|
opacity: ${({ show }) => show ? 0.75 : 0};
|
|
|
`
|
|
|
|
|
|
+const InvisbleTextarea = styled.textarea`
|
|
|
+ position: absolute;
|
|
|
+ top: -10000px;
|
|
|
+ z-index: -10000;
|
|
|
+ opacity: 0;
|
|
|
+`
|
|
|
+
|
|
|
export default ({ text }) => {
|
|
|
- const [textareaChild, copyText] = useCopying(text);
|
|
|
+ const textareaRef = useRef(null);
|
|
|
const [hovered, setHovered] = useState(false);
|
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
|
|
+ const onClick = () => {
|
|
|
+ textareaRef.current.select();
|
|
|
+ document.execCommand("copy");
|
|
|
+ setCopied(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ const onHover = () => {
|
|
|
+ setCopied(false);
|
|
|
+ setHovered(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ const onUnhover = () => {
|
|
|
+ setHovered(false);
|
|
|
+ }
|
|
|
+
|
|
|
return (
|
|
|
<Container>
|
|
|
- <UnderlinedSpan
|
|
|
- onClick={() => {copyText(); setCopied(true)}}
|
|
|
- onMouseOver={() => {setCopied(false); setHovered(true)}}
|
|
|
- onMouseOut={() => setHovered(false)}
|
|
|
- >
|
|
|
+ <UnderlinedSpan onClick={onClick} onMouseOver={onHover} onMouseOut={onUnhover}>
|
|
|
{text}
|
|
|
</UnderlinedSpan>
|
|
|
<ToolTip show={hovered}>
|
|
|
{copied ? "Copied!" : "Click to Copy"}
|
|
|
</ToolTip>
|
|
|
- {textareaChild}
|
|
|
+ <InvisbleTextarea ref={textareaRef} value={text} readOnly />
|
|
|
</Container>
|
|
|
)
|
|
|
}
|