useCopyToClipboarduseCopyToClipboard

클립보드에 쓰고 확인 상태를 스스로 지웁니다.Write to the clipboard with a confirmation flag that clears itself.

사용법Usage

copy-row.tsx
import { Button, useCopyToClipboard } from "@horon-ai/react";

function CopyRow({ value }: { value: string }) {
  const { copied, copy } = useCopyToClipboard();
  return (
    <Button variant="glass" onClick={() => copy(value)}>
      {copied ? "Copied" : "Copy"}
    </Button>
  );
}

시그니처Signature

use-copy-to-clipboard.ts
export type CopyToClipboard = {
  copied: boolean;
  copy: (text: string) => void;
};

export function useCopyToClipboard(resetMs = 1500): CopyToClipboard

매개변수Parameters

이름Name 타입Type 기본값Default 설명Description
resetMs number 1500 쓰기에 성공한 뒤 copied가 true로 남아 있는 시간입니다(밀리초).How long copied stays true after a successful write, in milliseconds.

반환값Returns

현재 copied 플래그와 참조가 고정된 copy 함수를 돌려줍니다.The current copied flag and a stable copy function.

동작과 주의Behavior notes

  • 쓰기가 끝나면 copied가 true가 되고 resetMs가 지나면 다시 false가 됩니다. 버튼이 따로 타이머를 두지 않아도 됩니다.copied turns true when the write resolves and back to false after resetMs, so a button needs no timer of its own.
  • navigator.clipboard로 쓰기 때문에 보안 컨텍스트와 사용자 제스처가 필요합니다. 그 API가 없는 환경에서는 호출이 아무 일도 하지 않고 copied는 true가 되지 않습니다.The write goes through navigator.clipboard, so it needs a secure context and a user gesture; where that API is missing the call is a no-op and copied never turns true.
  • 쓰기가 실패하면 예외를 던지지 않고 copied를 false로 둡니다.A failed write leaves copied false instead of throwing.
  • 타이머는 컴포넌트가 사라지거나 resetMs가 바뀔 때 정리됩니다.The timer is cleared when the component unmounts or resetMs changes.