useClipboardItems
リアクティブなクリップボードAPI。クリップボードコマンド(切り取り、コピー、貼り付け)への対応、およびシステムクリップボードとの非同期読み書き機能を提供します。Permissions APIによってクリップボードの内容へのアクセスが制限されています。ユーザーの許可がない限り、クリップボードの内容の読み取りや変更は許可されません。
デモ
お使いのブラウザはクリップボードAPIをサポートしていません。
useClipboard
との違い
useClipboard
は「テキストのみ」の関数ですが、useClipboardItems
はClipboardItemベースの関数です。useClipboardItems
を使用して、ClipboardItemでサポートされている任意のコンテンツをコピーできます。
使用方法
JavaScript
import { useClipboardItems } from '@vueuse/core'
const mime = 'text/html'
const source = ref([
new ClipboardItem({
[mime]: new Blob(['\'<b>HTML content</b>\'', { type: mime }]),
})
])
const { content, copy, copied, isSupported } = useClipboardItems({ source })
Vue
<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>
型定義
TypeScript
export interface UseClipboardItemsOptions<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 UseClipboardItemsReturn<Optional> {
isSupported: Ref<boolean>
content: ComputedRef<ClipboardItems>
copied: ComputedRef<boolean>
copy: Optional extends true
? (content?: ClipboardItems) => Promise<void>
: (text: ClipboardItems) => Promise<void>
}
/**
* Reactive Clipboard API.
*
* @see https://vueuse.dokyumento.jp/useClipboardItems
* @param options
*/
export declare function useClipboardItems(
options?: UseClipboardItemsOptions<undefined>,
): UseClipboardItemsReturn<false>
export declare function useClipboardItems(
options: UseClipboardItemsOptions<MaybeRefOrGetter<ClipboardItems>>,
): UseClipboardItemsReturn<true>