Back to Vueuse

useClipboard

packages/core/useClipboard/index.md

14.3.03.2 KB
Original Source

useClipboard

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.

<CourseLink href="https://vueschool.io/lessons/reactive-browser-wrappers-in-vueuse-useclipboard?friend=vueuse">Learn how to reactively save text to the clipboard with this FREE video lesson from Vue School!</CourseLink>

Usage

vue
<script setup lang="ts">
import { useClipboard } from '@vueuse/core'

const source = ref('Hello')
const { text, copy, copied, isSupported } = useClipboard({ source })
</script>

<template>
  <div v-if="isSupported">
    <button @click="copy(source)">
      <!-- by default, `copied` will be reset in 1.5s -->
      <span v-if="!copied">Copy</span>
      <span v-else>Copied!</span>
    </button>
    <p>Current copied: <code>{{ text || 'none' }}</code></p>
  </div>
  <p v-else>
    Your browser does not support Clipboard API
  </p>
</template>

Options

OptionTypeDefaultDescription
sourceMaybeRefOrGetter<string>Default content to copy when copy() is called without arguments
readbooleanfalseEnable reading clipboard content on copy/cut events
copiedDuringnumber1500Milliseconds before copied resets to false
legacybooleanfalseFallback to document.execCommand if Clipboard API unavailable

Return Values

PropertyTypeDescription
isSupportedComputedRef<boolean>Whether clipboard is supported (native or legacy)
textRef<string>Current clipboard content (when read: true)
copiedRef<boolean>true after successful copy, auto-resets
copy(text?: string) => Promise<void>Copy text to clipboard

Legacy Mode

Set legacy: true to keep the ability to copy if Clipboard API is not available. It will handle copy with execCommand as fallback.

ts
const { copy, isSupported } = useClipboard({ legacy: true })

Component Usage

vue
<template>
  <UseClipboard v-slot="{ copy, copied }" source="copy me">
    <button @click="copy()">
      {{ copied ? 'Copied' : 'Copy' }}
    </button>
  </UseClipboard>
</template>