Skip to content
On this page

useClipboard

Category
Last Changed
3 months ago

Reactive Clipboard API. Provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard. Access to the contents of the clipboard is gated behind the Permissions API without user permission, reading or altering the clipboard contents is not permitted.

Demo

Your browser does not support Clipboard API

Usage

import { useClipboard } from '@vueuse/core'

const source = ref('Hello')
const { text, copy, copied, isSupported } = useClipboard({ source })
import { useClipboard } from '@vueuse/core'

const source = ref('Hello')
const { text, copy, copied, isSupported } = useClipboard({ source })
<button @click='copy'>
  <!-- by default, `copied` will be reset in 1.5s -->
  <span v-if='!copied'>Copy</span>
  <span v-else>Copied!</span>
</button>
<button @click='copy'>
  <!-- by default, `copied` will be reset in 1.5s -->
  <span v-if='!copied'>Copy</span>
  <span v-else>Copied!</span>
</button>

Type Declarations

export interface ClipboardOptions<Source> extends ConfigurableNavigator {
  /**
   * Enabled reading for clipboard
   *
   * @default false
   */
  read?: boolean
  /**
   * Copy source
   */
  source?: Source
  /**
   * Milliseconds to reset state of `copied` ref
   *
   * @default 1500
   */
  copiedDuring?: number
}
export interface ClipboardReturn<Optional> {
  isSupported: boolean
  text: ComputedRef<string>
  copied: ComputedRef<boolean>
  copy: Optional extends true
    ? (text?: string) => Promise<void>
    : (text: string) => Promise<void>
}
/**
 * Reactive Clipboard API.
 *
 * @see https://vueuse.org/useClipboard
 * @param options
 */
export declare function useClipboard(
  options?: ClipboardOptions<undefined>
): ClipboardReturn<false>
export declare function useClipboard(
  options: ClipboardOptions<MaybeRef<string>>
): ClipboardReturn<true>
export interface ClipboardOptions<Source> extends ConfigurableNavigator {
  /**
   * Enabled reading for clipboard
   *
   * @default false
   */
  read?: boolean
  /**
   * Copy source
   */
  source?: Source
  /**
   * Milliseconds to reset state of `copied` ref
   *
   * @default 1500
   */
  copiedDuring?: number
}
export interface ClipboardReturn<Optional> {
  isSupported: boolean
  text: ComputedRef<string>
  copied: ComputedRef<boolean>
  copy: Optional extends true
    ? (text?: string) => Promise<void>
    : (text: string) => Promise<void>
}
/**
 * Reactive Clipboard API.
 *
 * @see https://vueuse.org/useClipboard
 * @param options
 */
export declare function useClipboard(
  options?: ClipboardOptions<undefined>
): ClipboardReturn<false>
export declare function useClipboard(
  options: ClipboardOptions<MaybeRef<string>>
): ClipboardReturn<true>

Source

SourceDemoDocs

Contributors

Anthony Fu
Sanxiaozhizi
Shinigami
Alex Kozack
Antério Vieira

Changelog

v7.0.0 on 11/20/2021
77a87 - fix!: disable read by default (#941)
v4.3.6 on 3/12/2021
8fd2c - feat: improved api, new copied state
useClipboard has loaded