@@ -45,28 +45,41 @@ document.addEventListener('DOMContentLoaded', function () {
4545if ( navigator && navigator . clipboard ) {
4646 const codeBlocks = document . querySelectorAll (
4747 ':is([class^="language-"] pre.highlight, .code-box pre code)' ,
48- )
48+ ) ;
49+
50+ const copyIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>` ;
51+ const copiedIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/></svg>` ;
4952
5053 codeBlocks . forEach ( ( codeBlock ) => {
51- const button = document . createElement ( 'button' )
52- const codeElement = codeBlock . querySelector ( 'code' ) || codeBlock
53- const container = codeBlock . parentElement
54+ const button = document . createElement ( 'button' ) ;
55+ const codeElement = codeBlock . querySelector ( 'code' ) || codeBlock ;
56+ const container = codeBlock . parentElement ;
5457
55- container . style . position = 'relative'
56- container . appendChild ( button )
57- button . innerText = 'copy'
58+ button . classList . add ( 'copy-button' ) ;
59+ container . appendChild ( button ) ;
60+ button . innerHTML = copyIcon
5861
5962 button . addEventListener ( 'mousedown' , async ( ) => {
60- const originalText = button . innerText
63+ const originalIcon = copyIcon ; // Store the original icon
6164
62- await navigator . clipboard . writeText ( codeElement . innerText )
63- button . innerText = 'copied!'
65+ try {
66+ await navigator . clipboard . writeText ( codeElement . innerText ) ;
6467
65- setTimeout ( ( ) => {
66- button . innerText = originalText
67- } , 1000 )
68- } )
69- } )
68+ button . classList . add ( 'copied' ) ;
69+ button . innerHTML = copiedIcon ;
70+
71+ setTimeout ( ( ) => {
72+ button . classList . remove ( 'copied' ) ;
73+ button . innerHTML = originalIcon ;
74+ } , 1000 ) ;
75+ } catch ( err ) {
76+ console . error ( 'Failed to copy text: ' , err ) ;
77+ setTimeout ( ( ) => {
78+ button . innerHTML = originalIcon ; // Revert to original icon
79+ } , 1000 ) ;
80+ }
81+ } ) ;
82+ } ) ;
7083}
7184
7285// Check for reduced motion setting
0 commit comments