Skip to content

useClipboard

Category
Export Size
1.44 kB
Last Changed
2 months ago
Related

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.

Learn how to reactively save text to the clipboard with this FREE video lesson from Vue School!

Demo

Clipboard Permission: read prompt | write granted

Current copied: none

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>

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

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

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

Type Declarations

Show Type Declarations
ts
export interface 
UseClipboardOptions
<
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
/** * Whether fallback to document.execCommand('copy') if clipboard is undefined. * * @default false */
legacy
?: boolean
} export interface
UseClipboardReturn
<
Optional
> {
isSupported
:
ComputedRef
<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 * * @__NO_SIDE_EFFECTS__ */ export declare function
useClipboard
(
options
?:
UseClipboardOptions
<undefined>,
):
UseClipboardReturn
<false>
export declare function
useClipboard
(
options
:
UseClipboardOptions
<
MaybeRefOrGetter
<string>>,
):
UseClipboardReturn
<true>

Source

SourceDemoDocs

Contributors

Anthony Fu
SerKo
Anthony Fu
Jelf
Sanxiaozhizi
NoiseFan
IlyaL
IlyaL
Бєляєв Віталій
Robin
Fernando Fernández
Alex Liu
Indrek Ardel
Mr.Hope
Alexzvn
Cat1007
丶远方
Olusola Olawale
Lumdzeehol
Lumdzeehol
Curt Grimes
wheat
洪布斯
karma
Shinigami
Alex Kozack
Antério Vieira

Changelog

v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
a54c4 - fix: unhandled rejection on read permission prompt (#4615)
v12.5.0 on
01acd - feat: should fall back to legacy clipboard when read/write fails (#4512)
v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
6860f - fix(useClipboard,useClipboardItems): avoid running "copied" timeout during initialization (#4299)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.9.0 on
a9f02 - fix: fix issue when permission is not defined (#3812)
v10.6.0 on
71b46 - feat: UseClipboard component (#3359)
37e86 - fix: use legacy way when without permission (#3379)

Released under the MIT License.

Build faster with AI
New Masterclass to help you leverage AI in your Vue workflow
Get Early Access