File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import React , { useState } from 'react' ;
2+ import { FaCopy , FaCheck } from 'react-icons/fa' ;
3+ interface Props { code : string ; language ?: string ; }
4+ const CopyableCode : React . FC < Props > = ( { code, language = '' } ) => {
5+ const [ copied , setCopied ] = useState ( false ) ;
6+ const handleCopy = async ( ) => { await navigator . clipboard . writeText ( code ) ; setCopied ( true ) ; setTimeout ( ( ) => setCopied ( false ) , 2000 ) ; } ;
7+ return (
8+ < div className = 'relative group' >
9+ < pre className = 'bg-gray-900 text-gray-100 rounded-xl p-4 overflow-x-auto text-sm' > < code className = { 'language-' + language } > { code } </ code > </ pre >
10+ < button onClick = { handleCopy }
11+ className = 'absolute top-2 right-2 p-2 bg-gray-700 hover:bg-gray-600 rounded-lg text-gray-300 opacity-0 group-hover:opacity-100 transition-opacity' >
12+ { copied ? < FaCheck className = 'text-green-400' /> : < FaCopy /> }
13+ </ button >
14+ </ div >
15+ ) ;
16+ } ;
17+ export default CopyableCode ;
You can’t perform that action at this time.
0 commit comments