refactor: useCopy

This commit is contained in:
zerosoul
2022-05-25 14:12:51 +08:00
parent a787941983
commit 1e20f79856
3 changed files with 27 additions and 11 deletions
+14 -5
View File
@@ -1,7 +1,17 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import { copyImageToClipboard } from "copy-image-clipboard";
import toast from "react-hot-toast";
const useCopy = (config) => {
const { enableToast = true } = config || {};
const [copied, setCopied] = useState(false);
useEffect(() => {
if (copied && enableToast) {
toast.success("Copied!");
}
}, [copied]);
const useCopy = () => {
const copyToClipboard = (str) => {
const el = document.createElement("textarea");
el.value = str;
@@ -23,10 +33,9 @@ const useCopy = () => {
return success;
};
const [copied, setCopied] = useState(false);
const copy = (text, isImage = false) => {
let inter = 0;
console.log("copy", text, isImage);
if (!copied) {
if (!isImage) {
setCopied(copyToClipboard(text));
@@ -47,7 +56,7 @@ const useCopy = () => {
};
};
return [copied, copy];
return { copied, copy };
};
export default useCopy;