"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { useCopyToClipboard } from "@suphark/ui/hooks/use-copy-to-clipboard"; import { cn } from "@suphark/ui/lib/utils"; import type { ComponentProps, ReactNode } from "react"; import { ActionConfirm, ActionCopy } from "../semantic-icons"; export interface CopyButtonProps extends Omit, "onClick" | "children" | "value"> { /** ข้อความที่จะคัดลอก */ value: string; /** ป้ายบนปุ่ม — ไม่ส่ง = ปุ่มไอคอนล้วน (ใช้ `label` เป็น aria-label) */ children?: ReactNode; /** aria-label/title ของปุ่ม (ค่าเริ่มต้น `UiText.copy`) */ label?: string; /** aria-label/title หลังคัดลอกแล้ว (ค่าเริ่มต้น `UiText.copied`) */ copiedLabel?: string; /** toast ของ `useCopyToClipboard` — ค่าเริ่มต้น `false` เพราะปุ่มนี้แสดง ✓ อยู่แล้ว */ toast?: boolean | { success?: string; failed?: string }; /** ms ที่ไอคอน ✓ ค้างไว้ (ค่าเริ่มต้น 2000) */ timeout?: number; onCopied?: (value: string) => void; } /** * ปุ่มคัดลอกข้อความ: ไอคอนคัดลอก → ✓ ชั่วครู่หลังคัดลอก (แอปไม่ต้องเขียน state/clipboard เอง) * ใช้ `useCopyToClipboard` ที่มี fallback `execCommand` สำหรับ webview ที่บล็อก clipboard API * * // ปุ่มไอคอน * คัดลอกลิงก์ */ export function CopyButton({ value, children, label, copiedLabel, toast = false, timeout = 2000, onCopied, variant = "ghost", size, className, disabled, ...props }: CopyButtonProps) { const text = useUiText(); const { isCopied, copyToClipboard } = useCopyToClipboard({ toast, timeout }); const idle = label ?? text.copy; const done = copiedLabel ?? text.copied; const title = isCopied ? done : idle; return ( ); }