Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useClipboard): use legacy way when without permission #3379

Merged
merged 7 commits into from Nov 9, 2023
Merged
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