improve: replace long line of words with clipboard button (#95)

* improve: replace long line of words with clipboard button

* change module path
This commit is contained in:
UUBulb
2025-01-06 21:02:39 +08:00
committed by GitHub
parent b643a5d86f
commit a45e789178
7 changed files with 64 additions and 9 deletions
+45
View File
@@ -0,0 +1,45 @@
import { ButtonProps } from "@/components/ui/button"
import { copyToClipboard } from "@/lib/utils"
import { forwardRef } from "react"
import { useState } from "react"
import { useTranslation } from "react-i18next"
import { toast } from "sonner"
import { IconButton } from "./xui/icon-button"
interface CopyButtonProps extends ButtonProps {
text?: string
}
export const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>((props, ref) => {
const { t } = useTranslation()
const [copy, setCopy] = useState(false)
const switchState = async (text?: string) => {
if (!text) {
toast("Warning", {
description: t("EmptyText"),
})
return
}
if (!copy) {
setCopy(true)
await copyToClipboard(text)
setTimeout(() => {
setCopy(false)
}, 2 * 1000)
}
}
return (
<IconButton
{...props}
ref={ref}
variant="outline"
size="icon"
icon={copy ? "check" : "clipboard"}
onClick={() => switchState(props.text)}
/>
)
})
+1 -1
View File
@@ -23,7 +23,7 @@ export const NoteMenu = forwardRef<HTMLButtonElement, NoteMenuProps>((props, ref
const switchState = async (text?: string) => {
if (!text) {
toast("Warning", {
description: "You didn't have any note.",
description: t("EmptyNote"),
})
return
}