Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useClipboard.md
1---2category: Browser3---45# useClipboard67Reactive [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/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](https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API). Without user permission, reading or altering the clipboard contents is not permitted.89<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>1011## Usage1213```vue14<script setup lang="ts">15import { useClipboard } from '@vueuse/core'1617const source = ref('Hello')18const { text, copy, copied, isSupported } = useClipboard({ source })19</script>2021<template>22<div v-if="isSupported">23<button @click="copy(source)">24<!-- by default, `copied` will be reset in 1.5s -->25<span v-if="!copied">Copy</span>26<span v-else>Copied!</span>27</button>28<p>Current copied: <code>{{ text || 'none' }}</code></p>29</div>30<p v-else>31Your browser does not support Clipboard API32</p>33</template>34```3536### Options3738| Option | Type | Default | Description |39| -------------- | -------------------------- | ------- | ----------------------------------------------------------------- |40| `source` | `MaybeRefOrGetter<string>` | — | Default content to copy when `copy()` is called without arguments |41| `read` | `boolean` | `false` | Enable reading clipboard content on copy/cut events |42| `copiedDuring` | `number` | `1500` | Milliseconds before `copied` resets to `false` |43| `legacy` | `boolean` | `false` | Fallback to `document.execCommand` if Clipboard API unavailable |4445### Return Values4647| Property | Type | Description |48| ------------- | ---------------------------------- | ------------------------------------------------- |49| `isSupported` | `ComputedRef<boolean>` | Whether clipboard is supported (native or legacy) |50| `text` | `Ref<string>` | Current clipboard content (when `read: true`) |51| `copied` | `Ref<boolean>` | `true` after successful copy, auto-resets |52| `copy` | `(text?: string) => Promise<void>` | Copy text to clipboard |5354### Legacy Mode5556Set `legacy: true` to keep the ability to copy if [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) is not available. It will handle copy with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) as fallback.5758```ts59const { copy, isSupported } = useClipboard({ legacy: true })60```6162## Component Usage6364```vue65<template>66<UseClipboard v-slot="{ copy, copied }" source="copy me">67<button @click="copy()">68{{ copied ? 'Copied' : 'Copy' }}69</button>70</UseClipboard>71</template>72```7374## Type Declarations7576```ts77export interface UseClipboardOptions<Source> extends ConfigurableNavigator {78/**79* Enabled reading for clipboard80*81* @default false82*/83read?: boolean84/**85* Copy source86*/87source?: Source88/**89* Milliseconds to reset state of `copied` ref90*91* @default 150092*/93copiedDuring?: number94/**95* Whether fallback to document.execCommand('copy') if clipboard is undefined.96*97* @default false98*/99legacy?: boolean100}101export interface UseClipboardReturn<Optional> extends Supportable {102text: Readonly<ShallowRef<string>>103copied: Readonly<ShallowRef<boolean>>104copy: Optional extends true105? (text?: string) => Promise<void>106: (text: string) => Promise<void>107}108/**109* Reactive Clipboard API.110*111* @see https://vueuse.org/useClipboard112* @param options113*114* @__NO_SIDE_EFFECTS__115*/116export declare function useClipboard(117options?: UseClipboardOptions<undefined>,118): UseClipboardReturn<false>119export declare function useClipboard(120options: UseClipboardOptions<MaybeRefOrGetter<string>>,121): UseClipboardReturn<true>122```123