Skip to content

Commit 9511abb

Browse files
JohudoDmitrii Zuev
andauthored
docs(CopyToClipboard): added copyText jsdoc (#2460)
Co-authored-by: Dmitrii Zuev <[email protected]>
1 parent e920264 commit 9511abb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/components/CopyToClipboard/copyText.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* Copies text to the browser's clipboard.
3+
* Uses the modern `navigator.clipboard` API, with a fallback to `document.execCommand` when unavailable.
4+
*
5+
* @param {string} text Text to copy to the clipboard
6+
* @returns {Promise<void>} Promise that resolves on successful copy or rejects on error
7+
* @throws {Error} Throws an error if neither `navigator.clipboard` nor `document` is available
8+
*
9+
* @example
10+
* ```typescript
11+
* // Copy simple text
12+
* copyText('Hello, World!')
13+
* .then(() => console.log('Text copied'))
14+
* .catch(error => console.error('Copy error:', error));
15+
*
16+
* // Using with async/await
17+
* try {
18+
* await copyText('Text to copy');
19+
* console.log('Successfully copied');
20+
* } catch (error) {
21+
* console.error('Failed to copy:', error);
22+
* }
23+
* ```
24+
*/
125
export function copyText(text: string) {
226
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
327
try {

0 commit comments

Comments
 (0)