diff --git a/src/components/IconGrid/InfoPanel.tsx b/src/components/IconGrid/InfoPanel.tsx index 0339fde..dbc7ee3 100644 --- a/src/components/IconGrid/InfoPanel.tsx +++ b/src/components/IconGrid/InfoPanel.tsx @@ -1,10 +1,22 @@ import React, { useRef } from "react"; -import { useRecoilValue } from "recoil"; +import { useRecoilValue, useSetRecoilState } from "recoil"; import { motion } from "framer-motion"; import { saveAs } from "file-saver"; -import { styleQueryAtom, iconSizeAtom, iconColorAtom } from "../../state/atoms"; -import { Icon, ArrowUpRightCircle, Copy } from "phosphor-react"; +import { + styleQueryAtom, + iconSizeAtom, + iconColorAtom, + iconPreviewOpenAtom, +} from "../../state/atoms"; +import useTransientState from "../../hooks/useTransientState"; +import { + Icon, + ArrowUpRightCircle, + Copy, + Prohibit, + CheckCircle, +} from "phosphor-react"; const infoVariants = { open: { @@ -34,26 +46,43 @@ const InfoPanel: React.FC = (props) => { const weight = useRecoilValue(styleQueryAtom); const size = useRecoilValue(iconSizeAtom); const color = useRecoilValue(iconColorAtom); + const setOpen = useSetRecoilState(iconPreviewOpenAtom); + const [copied, setCopied] = useTransientState(false, 2000); const ref = useRef(null); - const htmlString = ``; - const reactString = `<${Icon.displayName} size={${size}} color="${color}" ${ - weight === "regular" ? "" : `weight="${weight}" ` - }/>`; + const snippets = { + html: ``, + react: `<${Icon.displayName} size={${size}} color="${color}" ${ + weight === "regular" ? "" : `weight="${weight}" ` + }/>`, + }; - const handleCopySnippet = (data: string) => { + const handleCopySnippet = ( + event: React.MouseEvent, + type: "html" | "react" + ) => { + event.currentTarget.blur(); + setCopied(type); + const data = snippets[type]; data && navigator.clipboard.writeText(data); }; - const handleDownloadSVG = () => { + const handleDownloadSVG = ( + event: React.MouseEvent + ) => { + event.currentTarget.blur(); if (!ref.current?.outerHTML) return; const blob = new Blob([ref.current.outerHTML]); saveAs(blob, `${name}.svg`); }; - const handleCopySVG = () => { + const handleCopySVG = ( + event: React.MouseEvent + ) => { + event.currentTarget.blur(); + setCopied("svg"); ref.current && navigator.clipboard.writeText(ref.current.outerHTML); }; @@ -79,24 +108,32 @@ const InfoPanel: React.FC = (props) => {
HTML/CSS
-            {htmlString}
+            {snippets.html}
             
           
React
-            {reactString}
+            {snippets.react}
             
           
@@ -117,16 +154,34 @@ const InfoPanel: React.FC = (props) => { style={{ color: isDark ? "white" : "black" }} onClick={handleCopySVG} > - {" "} - Copy SVG + {copied === "svg" ? ( + + ) : ( + + )} + {copied === "svg" ? "Copied!" : "Copy SVG"} +
+ setOpen(false)} + /> +
); };