feat(clipboard): replace navigator.clipboard with vueuse useClipboard (#33)

This commit is contained in:
Sylvain Marroufin
2022-02-17 17:48:13 +01:00
committed by GitHub
parent e1d79d7fe7
commit 4532e09ac0
2 changed files with 8 additions and 9 deletions

View File

@@ -91,6 +91,7 @@
</template>
<script setup>
import { useClipboard } from '@vueuse/core'
import $ui from '#build/ui'
const nuxtApp = useNuxtApp()
@@ -363,14 +364,9 @@ function toKebabCase (str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
}
const copied = ref(false)
const { copy, copied } = useClipboard({ copiedDuring: 2000 })
const onCopy = () => {
navigator.clipboard.writeText(code.value).then(() => {
copied.value = true
setTimeout(() => {
copied.value = false
}, 2000)
})
copy(code.value)
}
const code = computed(() => {

View File

@@ -1,13 +1,16 @@
import { defineNuxtPlugin } from '#app'
import { useClipboard } from '@vueuse/core'
import { ClipboardPlugin } from '../types'
export default defineNuxtPlugin((nuxtApp) => {
const { copy: copyToClipboard, isSupported } = useClipboard()
function copy (text: string, success: { title?: string, description?: string } = {}, failure: { title?: string, description?: string } = {}) {
if (!navigator?.clipboard) {
if (!isSupported) {
return
}
navigator.clipboard.writeText(text).then(() => {
copyToClipboard(text).then(() => {
if (!success.title && !success.description) {
return
}