Shadcn Hooks

useClipboard

A hook to copy text to clipboard

Loading...

Installation

npx shadcn@latest add @shadcnhooks/use-clipboard

API

export interface UseClipboardOptions {
  /**
   * Enabled reading for clipboard
   *
   * @default false
   */
  read?: boolean

  /**
   * Copy source
   */
  source?: string

  /**
   * Milliseconds to reset state of `copied` ref
   *
   * @default 1500
   */
  copiedDuring?: number

  /**
   * Whether fallback to document.execCommand('copy') if clipboard is undefined.
   *
   * @default false
   */
  legacy?: boolean
}

export interface UseClipboardReturn {
  isSupported: boolean
  text: string
  copied: boolean
  copy: (text?: string) => Promise<void>
}

type PermissionState = 'granted' | 'denied' | 'prompt' | undefined

/**
 * A hook to copy text to clipboard
 * @param options - The options for the clipboard
 * @returns The clipboard state and methods
 */
export function useClipboard(options?: UseClipboardOptions): UseClipboardReturn

Credits

Last updated on

On this page