bug fixed + optimizations
This commit is contained in:
+20
-24
@@ -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>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user