File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
src/components/CopyToClipboard Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 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+ */
125export function copyText ( text : string ) {
226 if ( typeof navigator !== 'undefined' && navigator . clipboard ?. writeText ) {
327 try {
You can’t perform that action at this time.
0 commit comments