bug fixed + optimizations

This commit is contained in:
daniel31x13
2024-09-12 13:47:18 -04:00
parent 906779010e
commit 7bd3872195
6 changed files with 31 additions and 70 deletions
+20 -24
View File
@@ -1,34 +1,30 @@
import React from "react";
import React, { useState } from "react";
type Props = {
text: string;
};
const CopyButton = ({ text }: Props) => {
const CopyButton: React.FC<Props> = ({ text }) => {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 1000);
} catch (err) {
console.log(err);
}
};
return (
<div
className="bi-copy text-xl text-neutral btn btn-sm btn-square btn-ghost"
onClick={() => {
try {
navigator.clipboard.writeText(text).then(() => {
const copyIcon = document.querySelector(".bi-copy");
if (copyIcon) {
copyIcon.classList.remove("bi-copy");
copyIcon.classList.add("bi-check2");
copyIcon.classList.add("text-success");
}
setTimeout(() => {
if (copyIcon) {
copyIcon.classList.remove("bi-check2");
copyIcon.classList.remove("text-success");
copyIcon.classList.add("bi-copy");
}
}, 1000);
});
} catch (err) {
console.log(err);
}
}}
className={`text-xl text-neutral btn btn-sm btn-square btn-ghost ${
copied ? "bi-check2 text-success" : "bi-copy"
}`}
onClick={handleCopy}
></div>
);
};