Skip to main content

@rtorcato/browser-common / clipboard

clipboard

Functions​

isClipboardApiAvailable()​

isClipboardApiAvailable(): boolean

Defined in: clipboard/index.ts:10

Checks if the Clipboard API is available in the browser.

Example​

import { isClipboardApiAvailable } from '@rtorcato/browser-common/clipboard'
if (isClipboardApiAvailable()) showCopyButton()

Returns​

boolean

True if available, false otherwise.


readFromClipboard()​

readFromClipboard(): Promise<string | null>

Defined in: clipboard/index.ts:25

Reads text from the clipboard, if supported by the browser.

Example​

import { readFromClipboard } from '@rtorcato/browser-common/clipboard'
const text = await readFromClipboard()

Returns​

Promise<string | null>

The clipboard text, or null if not available.

Remarks​

Requires HTTPS or a user-gesture-initiated handler in most browsers.


copyElementTextToClipboard()​

copyElementTextToClipboard(element): Promise<boolean>

Defined in: clipboard/index.ts:46

Copies the text content of a DOM element to the clipboard.

Example​

import { copyElementTextToClipboard } from '@rtorcato/browser-common/clipboard'
await copyElementTextToClipboard(document.querySelector('#snippet')!)

Parameters​

element​

Element

The element whose text to copy.

Returns​

Promise<boolean>

True if the copy succeeded, false otherwise.


copyToClipboard()​

copyToClipboard(text): Promise<boolean>

Defined in: clipboard/index.ts:61

Copies a string to the clipboard, if supported by the browser.

Example​

import { copyToClipboard } from '@rtorcato/browser-common/clipboard'
await copyToClipboard('hello')

Parameters​

text​

string

The text to copy.

Returns​

Promise<boolean>

True if the copy succeeded, false otherwise.

Remarks​

Requires HTTPS or a user-gesture-initiated handler in most browsers.