|
| 1 | +import { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +function CopyButton({ text }) { |
| 5 | + const [copied, setCopied] = useState(false); |
| 6 | + |
| 7 | + const handleCopy = () => { |
| 8 | + navigator.clipboard.writeText(text).then(() => { |
| 9 | + setCopied(true); |
| 10 | + setTimeout(() => setCopied(false), 2000); |
| 11 | + }); |
| 12 | + }; |
| 13 | + |
| 14 | + const buttonStyle = { |
| 15 | + position: 'absolute', |
| 16 | + top: '0.5rem', |
| 17 | + right: '0.5rem', |
| 18 | + backgroundColor: '#e5e7eb', |
| 19 | + fontSize: '0.875rem', |
| 20 | + padding: '0.25rem 0.5rem', |
| 21 | + borderRadius: '0.25rem', |
| 22 | + cursor: 'pointer', |
| 23 | + transition: 'background-color 0.3s ease', |
| 24 | + }; |
| 25 | + |
| 26 | + const handleMouseOver = (e) => { |
| 27 | + e.target.style.backgroundColor = '#d1d5db'; |
| 28 | + }; |
| 29 | + |
| 30 | + const handleMouseOut = (e) => { |
| 31 | + e.target.style.backgroundColor = '#e5e7eb'; |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <button |
| 36 | + className="copy-button" |
| 37 | + style={buttonStyle} |
| 38 | + onClick={handleCopy} |
| 39 | + onMouseOver={handleMouseOver} |
| 40 | + onMouseOut={handleMouseOut} |
| 41 | + > |
| 42 | + {copied ? 'Copied!' : 'Copy'} |
| 43 | + </button> |
| 44 | + ); |
| 45 | +} |
| 46 | +CopyButton.propTypes = { |
| 47 | + text: PropTypes.string.isRequired, |
| 48 | +}; |
| 49 | + |
| 50 | +//integrating the CopyButton component into the CodeBlock component |
| 51 | + |
| 52 | +export default function CodeBlock({ children }) { |
| 53 | + const codeText = children?.props?.children?.toString() || ''; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div style={{ position: 'relative' }}> |
| 57 | + <CopyButton text={codeText} /> |
| 58 | + <pre>{children}</pre> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +CodeBlock.propTypes = { |
| 64 | + children: PropTypes.node.isRequired, |
| 65 | +}; |
0 commit comments