📄 useClipboard¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 7 |
| 📦 Imports | 13 |
| ⚡ Async/Await Patterns | 3 |
| 🟢 Vue Composition API | 1 |
| 📐 Interfaces | 2 |
| 📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useClipboard/index.ts
📦 Imports¶
| Name | Source |
|---|---|
MaybeRefOrGetter |
vue |
ShallowRef |
vue |
ConfigurableNavigator |
../_configurable |
Supportable |
../types |
useTimeoutFn |
@vueuse/shared |
computed |
vue |
shallowReadonly |
vue |
shallowRef |
vue |
toValue |
vue |
defaultNavigator |
../_configurable |
useEventListener |
../useEventListener |
usePermission |
../usePermission |
useSupported |
../useSupported |
Async/Await Patterns¶
| Type | Function | Await Expressions | Promise Chains |
|---|---|---|---|
| async-function | updateText |
navigator!.clipboard.readText() | none |
| async-function | copy |
navigator!.clipboard.write([clipboardItem]), resolvedValue() | none |
| promise-chain | createClipboardItem |
none | value().then |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
computed |
computed | none | none |
Functions¶
useClipboard(options: UseClipboardOptions<undefined>): UseClipboardReturn<false>¶
Reactive Clipboard API.
Parameters:
optionsany: No description
See: https://vueuse.org/useClipboard
Tags: @__NO_SIDE_EFFECTS__
Raw JSDoc
Code
Internal helpers¶
Declared inside another function in this file.
updateText(): Promise<void>¶
Returns: Promise<void>
Calls:
isAllowednavigator!.clipboard.readTextlegacyRead
Code
copy(value: ClipboardValue): Promise<void>¶
Parameters:
valueClipboardValue
Returns: Promise<void>
Calls:
toValue (from vue)isAllowedcreateClipboardItemnavigator!.clipboard.writelegacyCopyresolvedValuetimeout.start
Internal Comments:
Code
async function copy(value?: ClipboardValue) {
const resolvedValue = value ?? toValue(source)
if (isSupported.value && resolvedValue != null) {
copyPending.value = true
let useLegacy = !(isClipboardApiSupported.value && isAllowed(permissionWrite.value))
if (!useLegacy) {
try {
const clipboardItem = createClipboardItem(resolvedValue)
await navigator!.clipboard.write([clipboardItem])
}
catch {
useLegacy = true
}
}
if (useLegacy) {
if (typeof resolvedValue === 'string') {
text.value = resolvedValue
legacyCopy(resolvedValue)
}
else {
// For async functions in legacy mode, resolve and copy
const currentId = ++lastLegacyId
const resolvedText = await resolvedValue()
if (resolvedText != null && currentId === lastLegacyId) {
text.value = resolvedText
legacyCopy(resolvedText)
}
}
}
copied.value = true
timeout.start()
copyPending.value = false
}
}
createClipboardItem(value: ClipboardValue): ClipboardItem¶
Parameters:
valueClipboardValue
Returns: ClipboardItem
Calls:
value().then
Code
function createClipboardItem(value: ClipboardValue): ClipboardItem {
if (typeof value === 'string') {
text.value = value
return new ClipboardItem({ 'text/plain': value })
}
else {
return new ClipboardItem({
'text/plain': value().then((resolvedText = '') => {
text.value = resolvedText
return new Blob([resolvedText], { type: 'text/plain' })
}),
})
}
}
legacyCopy(value: string): void¶
Parameters:
valuestring
Returns: void
Calls:
document.createElementta.setAttributedocument.body.appendChildta.selectdocument.execCommandta.remove
Code
legacyRead(): string¶
Returns: string
Calls:
document?.getSelection?.()?.toString
isAllowed(status: PermissionState | undefined): boolean¶
Parameters:
statusPermissionState | undefined
Returns: boolean
Code
Interfaces¶
UseClipboardOptions<Source>¶
Interface Code
export interface UseClipboardOptions<Source> extends ConfigurableNavigator {
/**
* Enabled reading for clipboard
*
* @default false
*/
read?: boolean
/**
* Copy source
*/
source?: Source
/**
* Milliseconds to reset state of `copied` ref
*
* @default 1500
*/
copiedDuring?: number
/**
* Whether fallback to document.execCommand('copy') if clipboard is undefined.
*
* @default false
*/
legacy?: boolean
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
read |
boolean |
✓ | not shown |
source |
Source |
✓ | not shown |
copiedDuring |
number |
✓ | not shown |
legacy |
boolean |
✓ | not shown |
UseClipboardReturn<Optional>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
text |
Readonly<ShallowRef<string>> |
✗ | not shown |
copied |
Readonly<ShallowRef<boolean>> |
✗ | not shown |
copyPending |
Readonly<ShallowRef<boolean>> |
✗ | not shown |
copy |
Optional extends true ? (text?: ClipboardValue) => Promise<void> : (text: Cli... |
✗ | not shown |
Type Aliases¶
ClipboardValue¶
Generated by Syntax Scribe