GithubHelp home page GithubHelp logo

stormbrains / vue-clipboard3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamiecurnow/vue-clipboard3

0.0 0.0 0.0 14 KB

Easily copy to clipboard in Vue 3

License: MIT License

TypeScript 100.00%

vue-clipboard3's Introduction

vue-clipboard3

Easily copy to clipboard in Vue 3 (composition-api) using clipboard.js ๐Ÿ“‹

Thanks to 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

<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

<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

useClipboard(options: Options)
interface Options {
  /** Fixes IE by appending element to body. Defaults to true. */
  appendToBody: boolean
}

returns an object with a single key: toClipboard

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!

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

License

MIT License

vue-clipboard3's People

Contributors

jamiecurnow 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.