# vue-clipboard3

> Easily copy to clipboard with Vue 3

Latest version **2.0.0** (published 2022-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-clipboard3
pnpm add vue-clipboard3
yarn add vue-clipboard3
bun add vue-clipboard3
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-03-13 |
| First published | 2020-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 143 |
| Author | Jamie Curnow |
| Maintainers | jamiecurnow |
| Keywords | vue, vue3, vue 3, composition-api, clipboard, clipboard.js |

## Links

- npm: https://www.npmjs.com/package/vue-clipboard3
- Repository: https://github.com/JamieCurnow/vue-clipboard3
- npm.io page: https://npm.io/package/vue-clipboard3

## Dependencies (1)

- [clipboard](https://npm.io/package/clipboard.md) ^2.0.6

## Recent versions

- 2.0.0 (latest) — 2022-03-13
- 1.0.1 — 2021-05-15
- 1.0.0 — 2020-12-16
- 0.0.2 — 2020-12-16
- 0.0.1 — 2020-12-16

## README

# vue-clipboard3

Easily copy to clipboard in Vue 3 (composition-api) using clipboard.js 📋

Thanks to [vue-clipboard2](https://github.com/Inndy/vue-clipboard2) for inspiration!

## Install

`yarn add vue-clipboard3`

or

`npm install --save vue-clipboard3`

## About

For use with **Vue 3** and the **Composition API**. I decided to keep in line with the Vue 3 spirit and not make a directive out of this (if you want a vue directive, please make a pull request). I think it makes more sense and provides more clarity to just use this as a method in the `setup()` function.

Keep it simple.

## Usage

### Simple

```html
<template lang="html">
  <button @click="copy">Copy!</button>
</template>

<script lang="ts">
import { defineComponent } from '@vue/composition-api'
import useClipboard from 'vue-clipboard3'

export default defineComponent({
  setup() {
    const { toClipboard } = useClipboard()

    const copy = async () => {
      try {
        await toClipboard('Any text you like')
        console.log('Copied to clipboard')
      } catch (e) {
        console.error(e)
      }
    }

    return { copy }
  }
})
</script>
```

### With ref

```html
<template lang="html">
  <div>
    <input type="text" v-model="text">
    <button @click="copy">Copy!</button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from '@vue/composition-api'
import useClipboard from 'vue-clipboard3'

export default defineComponent({
  setup() {
    const { toClipboard } = useClipboard()

    const text = ref('')

    const copy = async () => {
      try {
        await toClipboard(text.value)
        console.log('Copied to clipboard')
      } catch (e) {
        console.error(e)
      }
    }

    return { copy, text }
  }
})
</script>
```

## API

```ts
useClipboard(options: Options)
```

```ts
interface Options {
  /** Fixes IE by appending element to body. Defaults to true. */
  appendToBody: boolean
}
```

returns an object with a single key: `toClipboard`

```ts
toClipboard(text: string, container?: HTMLElement)
```

requires that you pass in at least one argument that is a string. This is the text to be copied to the clipboard. The second optional argument is a html element that will be used as the container internally when using clipboard.js.

### Contribution

PRs and issues welcome!

```shell
git clone https://github.com/JamieCurnow/vue-clipboard3.git
cd vue-clipboard3
yarn install
yarn watch
```

### License

[MIT License](https://github.com/JamieCurnow/vue-clipboard3/blob/main/LICENSE)

---
_Source: https://npm.io/package/vue-clipboard3 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
