mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
feat(clipboard): replace navigator.clipboard with vueuse useClipboard (#33)
This commit is contained in:
committed by
GitHub
parent
e1d79d7fe7
commit
4532e09ac0
@@ -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(() => {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user