Bläddra i källkod

Converting click to copy to styled components

Kirk Trombley 5 år sedan
förälder
incheckning
1cc54bf16d

+ 0 - 34
client/src/components/util/ClickToCopy/ClickToCopy.css

@@ -1,34 +0,0 @@
-.click-to-copy {
-  position: relative;
-  display: inline-flex;
-  justify-content: center;
-}
-
-.click-to-copy__text {
-  border-bottom: 1px dashed black;
-}
-
-.click-to-copy__tooltip {
-  background-color: black;
-  color: white;
-  text-align: center;
-  padding: 2px 8px;
-  border-radius: 8px;
-  position: absolute;
-  z-index: 1;
-  top: -115%;
-  white-space: nowrap;
-  transition: opacity 0.25s;
-  opacity: 0;
-}
-
-.click-to-copy__tooltip--visible {
-  opacity: 0.75;
-}
-
-.click-to-copy__textarea {
-  position: absolute;
-  top: -10000px;
-  z-index: -10000;
-  opacity: 0;
-}

+ 15 - 5
client/src/components/util/ClickToCopy/ClickToCopy.jsx

@@ -1,24 +1,34 @@
 import React, { useState } from "react";
+import styled from "styled-components";
 import useCopying from "./useCopying";
 import CopyingTooltip from "./CopyingTooltip";
 
+const Container = styled.div`
+  position: relative;
+  display: inline-flex;
+  justify-content: center;
+`
+
+const UnderlinedSpan = styled.span`
+  border-bottom: 1px dashed #bbb;
+`
+
 export default ({ text }) => {
   const [textareaChild, copyText] = useCopying(text);
   const [hovered, setHovered] = useState(false);
   const [copied, setCopied] = useState(false);
 
   return (
-    <div className="click-to-copy">
-      <span
-        className="click-to-copy__text"
+    <Container>
+      <UnderlinedSpan
         onClick={() => {copyText(); setCopied(true)}}
         onMouseOver={() => {setCopied(false); setHovered(true)}}
         onMouseOut={() => setHovered(false)}
       >
         {text}
-      </span>
+      </UnderlinedSpan>
       <CopyingTooltip hovered={hovered} copied={copied}/>
       {textareaChild}
-    </div>
+    </Container>
   )
 }

+ 16 - 4
client/src/components/util/ClickToCopy/CopyingTooltip.jsx

@@ -1,10 +1,22 @@
 import React from "react";
+import styled from "styled-components";
 
-const tooltipClass = "click-to-copy__tooltip";
-const tooltipVisClass = `${tooltipClass}--visible`;
+const ToolTip = styled.span`
+  background-color: black;
+  color: white;
+  text-align: center;
+  padding: 2px 8px;
+  border-radius: 8px;
+  position: absolute;
+  z-index: 1;
+  top: -115%;
+  white-space: nowrap;
+  transition: opacity 0.25s;
+  opacity: ${({ hovered }) => hovered ? 0.75 : 0};
+`
 
 export default ({ copied, hovered }) => (
-  <span className={`${tooltipClass} ${hovered ? tooltipVisClass : ""}`}>
+  <ToolTip hovered={hovered}>
     {copied ? "Copied!" : "Click to Copy"}
-  </span>
+  </ToolTip>
 );

+ 0 - 1
client/src/components/util/ClickToCopy/index.js

@@ -1,2 +1 @@
-import "./ClickToCopy.css";
 export { default } from './ClickToCopy';

+ 9 - 2
client/src/components/util/ClickToCopy/useCopying.jsx

@@ -1,4 +1,12 @@
 import React, { useRef } from "react";
+import styled from "styled-components";
+
+const InvisbleTextarea = styled.textarea`
+  position: absolute;
+  top: -10000px;
+  z-index: -10000;
+  opacity: 0;
+`
 
 export default text => {
   const textareaRef = useRef(null);
@@ -7,9 +15,8 @@ export default text => {
     document.execCommand("copy");
   };
   const hiddenTextArea = (
-    <textarea
+    <InvisbleTextarea
       ref={textareaRef}
-      className="click-to-copy__textarea"
       value={text}
       readOnly
     />)