@@ -21,20 +21,29 @@ function handleCopyClick(event: Event): void {
2121 copyToClipboard ( codeContent , button ) ;
2222}
2323
24- async function copyToClipboard ( text : string , button : HTMLElement ) : Promise < void > {
24+ /**
25+ * Copy text to clipboard with fallback for older browsers
26+ * @param text - The text content to copy to clipboard
27+ * @param button - The copy button element for showing feedback
28+ */
29+ async function copyToClipboard (
30+ text : string ,
31+ button : HTMLElement ,
32+ ) : Promise < void > {
2533 try {
2634 await navigator . clipboard . writeText ( text ) ;
2735 showSuccessMessage ( button ) ;
2836 } catch {
2937 // Fallback for older browsers
3038 const textarea = document . createElement ( 'textarea' ) ;
3139 textarea . value = text ;
32- textarea . style . cssText = 'position:fixed;left:-999999px;top:-999999px;opacity:0;' ;
40+ textarea . style . cssText =
41+ 'position:fixed;left:-999999px;top:-999999px;opacity:0;' ;
3342 document . body . appendChild ( textarea ) ;
3443 textarea . select ( ) ;
3544 const success = document . execCommand ( 'copy' ) ;
3645 document . body . removeChild ( textarea ) ;
37-
46+
3847 if ( success ) {
3948 showSuccessMessage ( button ) ;
4049 } else {
@@ -43,46 +52,60 @@ async function copyToClipboard(text: string, button: HTMLElement): Promise<void>
4352 }
4453}
4554
55+ /**
56+ * Show success feedback by transforming button to green "Copied!" state
57+ * @param button - The copy button element to show success feedback in
58+ */
4659function showSuccessMessage ( button : HTMLElement ) : void {
4760 const originalContent = button . innerHTML ;
4861 const originalClasses = button . className ;
49-
50- button . className = 'copy-code-btn bg-green-100 text-green-800 text-xs px-4 py-2 rounded-lg transition-all duration-200 flex items-center space-x-2 shadow-sm border border-green-200' ;
62+
63+ button . className =
64+ 'copy-code-btn bg-green-100 text-green-800 text-xs px-4 py-2 rounded-lg transition-all duration-200 flex items-center space-x-2 shadow-sm border border-green-200' ;
5165 button . innerHTML = `
5266 <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
5367 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
5468 </svg>
5569 <span class="font-medium">Copied!</span>
5670 ` ;
5771 button . setAttribute ( 'disabled' , 'true' ) ;
58-
72+
5973 setTimeout ( ( ) => {
6074 button . className = originalClasses ;
6175 button . innerHTML = originalContent ;
6276 button . removeAttribute ( 'disabled' ) ;
6377 } , 2000 ) ;
6478}
6579
80+ /**
81+ * Show error feedback by transforming button to red "Failed!" state
82+ * @param button - The copy button element to show error feedback in
83+ */
6684function showErrorMessage ( button : HTMLElement ) : void {
6785 const originalContent = button . innerHTML ;
6886 const originalClasses = button . className ;
69-
70- button . className = 'copy-code-btn bg-red-100 text-red-800 text-xs px-4 py-2 rounded-lg transition-all duration-200 flex items-center space-x-2 shadow-sm border border-red-200' ;
87+
88+ button . className =
89+ 'copy-code-btn bg-red-100 text-red-800 text-xs px-4 py-2 rounded-lg transition-all duration-200 flex items-center space-x-2 shadow-sm border border-red-200' ;
7190 button . innerHTML = `
7291 <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
7392 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
7493 </svg>
7594 <span class="font-medium">Failed!</span>
7695 ` ;
7796 button . setAttribute ( 'disabled' , 'true' ) ;
78-
97+
7998 setTimeout ( ( ) => {
8099 button . className = originalClasses ;
81100 button . innerHTML = originalContent ;
82101 button . removeAttribute ( 'disabled' ) ;
83102 } , 2500 ) ;
84103}
85104
105+ /**
106+ * Initialize copy functionality for all copy buttons on the page
107+ * Attaches click event handlers to elements with 'copy-code-btn' class
108+ */
86109export function initCodeCopy ( ) : void {
87110 document . querySelectorAll ( '.copy-code-btn' ) . forEach ( ( button ) => {
88111 if ( button instanceof HTMLElement ) {
0 commit comments