Skip to content

Commit

Permalink
fix(useClipboard): 馃悰 use legacy way when without permission
Browse files Browse the repository at this point in the history
  • Loading branch information
catye committed Sep 6, 2023
1 parent bf1d823 commit f04e403
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/core/useClipboard/index.ts
Original file line number Diff line number Diff line change
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) {
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)
await navigator!.clipboard.writeText(value)
else
legacyCopy(value)
Expand Down

0 comments on commit f04e403

Please sign in to comment.