Giter VIP home page Giter VIP logo

vue3-clipboard's Introduction

vue3-clipboard

A simple vuejs 3 binding for clipboard.js, based on Inndy/vue-clipboard2

Install

npm install --save vue3-clipboard or use dist/vue3-clipboard.min.js without npm

Usage

For vue-cli user:

import { createApp } from 'vue'
import VueClipboard from 'vue3-clipboard'

const app = createApp({})

app.use(VueClipboard)

For standalone usage:

<script src="vue.min.js"></script>
<!-- must place this line after vue.js -->
<script src="dist/vue3-clipboard.min.js"></script>

I want to copy texts without a specific button!

Yes, you can do it by using our new method: this.$copyText, where we replace the clipboard directives with a v-on directive.

Modern browsers have some limitations like that you can't use window.open without a user interaction. So there's the same restriction on copying things! Test it before you use it. Make sure you are not using this method inside any async method.

Before using this feature, read: this issue and this page first.

It doesn't work with bootstrap modals

See clipboardjs document and this pull request, container option is available like this:

In Options API:

const container = this.$refs.container
this.$copyText((text: 'Text to copy'), container)

In Composition API:

import { copyText } from 'vue3-clipboard'
import { ref } from 'vue'

const container = ref(null)
copyText('Text to copy', container.value)

Or you can let vue3-clipboard set container to current element by doing this:

import { createApp } from 'vue'
import VueClipboard from 'vue3-clipboard'

const app = createApp({})

app.use(VueClipboard, {
  autoSetContainer: true,
  appendToBody: true,
})

Sample

<template>
  <div class="container">
    <button type="button" v-clipboard:copy="message" v-clipboard:success="onCopy" v-clipboard:error="onError">
      Copy!
    </button>
  </div>
</template>

<script>
  export default {
    setup() {
      const message = 'Hello Clipboard!'
      const onCopy = (e) => {
        alert('You just copied: ' + e.text)
      }
      const onError = (e) => {
        alert('Failed to copy texts')
      }

      return { message, onCopy, onError }
    },
  }
</script>

Sample 2

<template>
  <div class="container">
    <button type="button" @click="doCopy">Copy!</button>
  </div>
</template>

<script>
  import { copyText } from 'vue3-clipboard'

  export default {
    setup() {
      const doCopy = () => {
        copyText('Hello Clipboard', undefined, (error, event) => {
          if (error) {
            alert('Can not copy')
            console.log(error)
          } else {
            alert('Copied')
            console.log(event)
          }
        })
      }

      return { doCopy }
    },
  }
</script>

vue3-clipboard's People

Contributors

avoeffelen avatar daizhen1995 avatar hcg1023 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.