Skip to content

Commit 17f8653

Browse files
committed
fix: Remove console error and improve copy-code utility cross-browser compatibility
- Replace console.error with user-friendly visual feedback - Add secure context check for modern clipboard API - Enhance fallback method for older browsers and HTTP contexts - Add mobile device support with setSelectionRange - Implement visual success/error states on copy buttons - Improve error handling with graceful degradation Fixes console noise when copy operation fails and ensures copy functionality works across all browsers including Firefox, Safari, and older browser versions.
1 parent e92069c commit 17f8653

File tree

1 file changed

+64
-7
lines changed

1 file changed

+64
-7
lines changed

src/utils/copy-code.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function handleCopyClick(event: Event): void {
1414
const codeContent = button.getAttribute('data-code');
1515

1616
if (!codeContent) {
17-
console.error('No code content found to copy');
17+
showErrorFeedback(button, 'No content to copy');
1818
return;
1919
}
2020

@@ -28,37 +28,94 @@ async function copyToClipboard(
2828
text: string,
2929
button: HTMLElement,
3030
): Promise<void> {
31+
// Check if clipboard API is available and we're in a secure context
32+
if (navigator.clipboard && window.isSecureContext) {
33+
try {
34+
await navigator.clipboard.writeText(text);
35+
showSuccessMessage(button);
36+
return;
37+
} catch (error) {
38+
// Fall through to legacy method
39+
}
40+
}
41+
42+
// Fallback for older browsers or insecure contexts
3143
try {
32-
await navigator.clipboard.writeText(text);
33-
showSuccessMessage(button);
34-
} catch {
35-
// Fallback for older browsers
3644
const textarea = document.createElement('textarea');
3745
textarea.value = text;
3846
textarea.style.cssText =
39-
'position:fixed;left:-999999px;top:-999999px;opacity:0;';
47+
'position:fixed;left:-999999px;top:-999999px;opacity:0;pointer-events:none;';
4048

4149
document.body.appendChild(textarea);
50+
textarea.focus();
4251
textarea.select();
4352

53+
// For mobile devices
54+
textarea.setSelectionRange(0, 99999);
55+
4456
const success = document.execCommand('copy');
4557
document.body.removeChild(textarea);
4658

47-
if (success) showSuccessMessage(button);
59+
if (success) {
60+
showSuccessMessage(button);
61+
} else {
62+
showErrorFeedback(button, 'Copy failed - please copy manually');
63+
}
64+
} catch (error) {
65+
showErrorFeedback(button, 'Copy not supported in this browser');
4866
}
4967
}
5068

5169
/**
5270
* Show success message
5371
*/
5472
function showSuccessMessage(button: HTMLElement): void {
73+
// Update button text temporarily
74+
const originalText = button.textContent;
75+
button.textContent = 'Copied!';
76+
button.style.backgroundColor = '#10b981';
77+
button.style.color = 'white';
78+
79+
setTimeout(() => {
80+
button.textContent = originalText;
81+
button.style.backgroundColor = '';
82+
button.style.color = '';
83+
}, 2000);
84+
85+
// Also check for success message element
5586
const successMessage = button.nextElementSibling as HTMLElement;
5687
if (successMessage?.classList.contains('copy-success-message')) {
5788
successMessage.classList.remove('hidden');
5889
setTimeout(() => successMessage.classList.add('hidden'), 2000);
5990
}
6091
}
6192

93+
/**
94+
* Show error feedback to user
95+
*/
96+
function showErrorFeedback(button: HTMLElement, message: string): void {
97+
// Update button text temporarily
98+
const originalText = button.textContent;
99+
button.textContent = 'Failed';
100+
button.style.backgroundColor = '#ef4444';
101+
button.style.color = 'white';
102+
103+
setTimeout(() => {
104+
button.textContent = originalText;
105+
button.style.backgroundColor = '';
106+
button.style.color = '';
107+
}, 2000);
108+
109+
// Show tooltip or alert as fallback
110+
if ('title' in button) {
111+
const originalTitle = button.title;
112+
button.title = message;
113+
setTimeout(() => {
114+
button.title = originalTitle;
115+
}, 3000);
116+
}
117+
}
118+
62119
/**
63120
* Initialize copy code functionality
64121
*/

0 commit comments

Comments
 (0)