Skip to content
On this page

useClipboard

Clipboard_API

State

The useClipboard function exposes the following reactive state:

typescript
import { useClipboard } from '@elonehoo/pistachio'

const { text, supported, write, read } = useClipboard()
import { useClipboard } from '@elonehoo/pistachio'

const { text, supported, write, read } = useClipboard()
StateTypeDescription
supportedbooleanReturns true if the browser has navigator.clipboard(opens new window)
textRef<string | undefined>Reactive text in clipboard, also updates the clipboard if changed
dataRef<DataTransfer | undefined>Reactive data in clipboard, it does not update the clipboard on change

Example

click the button to copy a random number

Check your dev tools to see what has been copied to your clipboard

You can also change the clipboard

Current clipboard:

vue
<script setup lang="ts">
import { useClipboard } from '@elonehoo/pistachio'
const { text, writeText } = useClipboard()

function copy() {
  writeText(Math.random().toString())
  console.log(text.value)
}
</script>

<template>
  <div>
    <p>click the button to copy a random number</p>
    <button @click="copy">
      copy
    </button>
    <p>Check your dev tools to see what has been copied to your clipboard</p>

    <p>You can also change the clipboard</p>
    <input v-model="text">

    <div>
      <p>Current clipboard:</p>
      <p>{{ text }}</p>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useClipboard } from '@elonehoo/pistachio'
const { text, writeText } = useClipboard()

function copy() {
  writeText(Math.random().toString())
  console.log(text.value)
}
</script>

<template>
  <div>
    <p>click the button to copy a random number</p>
    <button @click="copy">
      copy
    </button>
    <p>Check your dev tools to see what has been copied to your clipboard</p>

    <p>You can also change the clipboard</p>
    <input v-model="text">

    <div>
      <p>Current clipboard:</p>
      <p>{{ text }}</p>
    </div>
  </div>
</template>

Released under the MIT License.