Skip to content

Commit

Permalink
fix(useClipboard): use legacy way when without permission (#3379)
Browse files Browse the repository at this point in the history
Co-authored-by: catye <catye@tencent.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
4 people committed Nov 9, 2023
1 parent 61ceb19 commit 37e866c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/core/useClipboard/index.ts
Expand Up @@ -8,6 +8,7 @@ import { useEventListener } from '../useEventListener'
import { useSupported } from '../useSupported'
import type { ConfigurableNavigator } from '../_configurable'
import { defaultNavigator } from '../_configurable'
import { usePermission } from '../usePermission'

export interface UseClipboardOptions<Source> extends ConfigurableNavigator {
/**
Expand Down Expand Up @@ -62,13 +63,15 @@ export function useClipboard(options: UseClipboardOptions<MaybeRefOrGetter<strin
} = options

const isClipboardApiSupported = useSupported(() => (navigator && 'clipboard' in navigator))
const permissionRead = usePermission('clipboard-read')
const permissionWrite = usePermission('clipboard-write')
const isSupported = computed(() => isClipboardApiSupported.value || legacy)
const text = ref('')
const copied = ref(false)
const timeout = useTimeoutFn(() => copied.value = false, copiedDuring)

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

async function copy(value = toValue(source)) {
if (isSupported.value && value != null) {
if (isClipboardApiSupported.value)
if (isClipboardApiSupported.value && permissionWrite.value !== 'denied')
await navigator!.clipboard.writeText(value)
else
legacyCopy(value)
Expand Down

0 comments on commit 37e866c

Please sign in to comment.