useClipboard
リアクティブなクリップボードAPI。クリップボードコマンド(切り取り、コピー、貼り付け)への対応、およびシステムクリップボードとの非同期読み書き機能を提供します。Permissions APIによってクリップボードの内容へのアクセスが制御されます。ユーザーの許可がない限り、クリップボードの内容の読み取りや変更は許可されません。
Vue Schoolの無料ビデオレッスンで、テキストをリアクティブにクリップボードに保存する方法を学びましょう!デモ
お使いのブラウザはClipboard APIをサポートしていません
使用方法
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>
Clipboard APIが利用できない場合でもコピー機能を維持するには、legacy: true
に設定します。execCommandをフォールバックとして使用してコピー処理を行います。
コンポーネントでの使用方法
この関数は、
@vueuse/components
パッケージを介して、レンダーレスコンポーネントバージョンも提供します。使用方法の詳細はこちら。
Vue
<template>
<UseClipboard v-slot="{ copy, copied }" source="copy me">
<button @click="copy()">
{{ copied ? 'Copied' : 'Copy' }}
</button>
</UseClipboard>
</template>
型定義
型定義を表示
TypeScript
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: Ref<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.dokyumento.jp/useClipboard
* @param options
*/
export declare function useClipboard(
options?: UseClipboardOptions<undefined>,
): UseClipboardReturn<false>
export declare function useClipboard(
options: UseClipboardOptions<MaybeRefOrGetter<string>>,
): UseClipboardReturn<true>
ソースコード
コントリビューター
変更ログ
v12.0.0-beta.1
2024年11月21日v10.9.0
2024年2月27日v10.6.0
2023年11月9日v10.0.0-beta.4
2023年4月13日4d757
- feat(types)!: MaybeComputedRef
をMaybeRefOrGetter
に名前変更0a72b
- feat(toValue): resolveUnref
をtoValue
に名前変更v9.4.0
2022年10月25日