Skip to content

Commit

Permalink
feat(clipboard): replace navigator.clipboard with vueuse `useClipbo…
Browse files Browse the repository at this point in the history
…ard` (#33)
  • Loading branch information
smarroufin committed Feb 17, 2022
1 parent e1d79d7 commit 4532e09
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 3 additions & 7 deletions docs/pages/components/[component].vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
</template>
<script setup>
import { useClipboard } from '@vueuse/core'
import $ui from '#build/ui'
const nuxtApp = useNuxtApp()
Expand Down Expand Up @@ -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(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/plugins/clipboard.client.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

1 comment on commit 4532e09

@vercel
Copy link

@vercel vercel bot commented on 4532e09 Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxtlabs.vercel.app
nuxthq-ui.vercel.app
ui-nuxtlabs.vercel.app

Please sign in to comment.