feat: handle and display clipboard error in useCopyable
This commit is contained in:
@@ -8,13 +8,24 @@ export const useCopyable = (
|
|||||||
const icon = ref("i-heroicons-clipboard-document");
|
const icon = ref("i-heroicons-clipboard-document");
|
||||||
const label = ref("Copy");
|
const label = ref("Copy");
|
||||||
|
|
||||||
|
const toast = useToast();
|
||||||
|
|
||||||
const copy = async () => {
|
const copy = async () => {
|
||||||
|
try {
|
||||||
if (typeof valueOrCallback === "function") {
|
if (typeof valueOrCallback === "function") {
|
||||||
await valueOrCallback();
|
await valueOrCallback();
|
||||||
} else {
|
} else {
|
||||||
const value = unref(valueOrCallback);
|
const value = unref(valueOrCallback);
|
||||||
await navigator.clipboard.writeText(value);
|
await navigator.clipboard.writeText(value);
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
toast.add({
|
||||||
|
title: "Failed to copy to clipboard",
|
||||||
|
color: "error",
|
||||||
|
icon: "i-lucide-circle-x",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
icon.value = "i-heroicons-clipboard-document-check";
|
icon.value = "i-heroicons-clipboard-document-check";
|
||||||
label.value = "Copied!";
|
label.value = "Copied!";
|
||||||
|
|||||||
Reference in New Issue
Block a user