Skip to content

Commit

Permalink
fix(useClipboard): fix issue when permission is not defined (#3812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 27, 2024
1 parent b142638 commit a9f02dd
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/core/useClipboard/index.ts
Expand Up @@ -71,7 +71,7 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin
const timeout = useTimeoutFn(() => copied.value = false, copiedDuring)

function updateText() {
if (isClipboardApiSupported.value && permissionRead.value !== 'denied') {
if (isClipboardApiSupported.value && isAllowed(permissionRead.value)) {
navigator!.clipboard.readText().then((value) => {
text.value = value
})
Expand All @@ -86,7 +86,7 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin

async function copy(value = toValue(source)) {
if (isSupported.value && value != null) {
if (isClipboardApiSupported.value && permissionWrite.value !== 'denied')
if (isClipboardApiSupported.value && isAllowed(permissionWrite.value))
await navigator!.clipboard.writeText(value)
else
legacyCopy(value)
Expand All @@ -112,6 +112,10 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin
return document?.getSelection?.()?.toString() ?? ''
}

function isAllowed(status: PermissionState | undefined) {
return status === 'granted' || status === 'prompt'
}

return {
isSupported,
text: text as ComputedRef<string>,
Expand Down

0 comments on commit a9f02dd

Please sign in to comment.