mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-24 00:40:34 +01:00
chore: add more composables (#138)
This commit is contained in:
committed by
GitHub
parent
fef93f3198
commit
fd4b608150
30
src/runtime/composables/useCopyToClipboard.ts
Normal file
30
src/runtime/composables/useCopyToClipboard.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useClipboard } from '@vueuse/core'
|
||||
import { useToast } from './useToast'
|
||||
|
||||
export function useCopyToClipboard () {
|
||||
const { copy: copyToClipboard, isSupported } = useClipboard()
|
||||
const toast = useToast()
|
||||
|
||||
function copy (text: string, success: { title?: string, description?: string } = {}, failure: { title?: string, description?: string } = {}) {
|
||||
if (!isSupported) {
|
||||
return
|
||||
}
|
||||
|
||||
copyToClipboard(text).then(() => {
|
||||
if (!success.title && !success.description) {
|
||||
return
|
||||
}
|
||||
|
||||
toast.success(success)
|
||||
}, function (e) {
|
||||
toast.error({
|
||||
...failure,
|
||||
description: failure.description || e.message
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
copy
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user