Skip to content

feat: add copy-button for codeBlocks #7539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/components/CodeBlock/CodeBlock.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useState } from 'react';
import PropTypes from 'prop-types';

function CopyButton({ text }) {
const [copied, setCopied] = useState(false);

const handleCopy = () => {
navigator.clipboard.writeText(text).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
});
};

const buttonStyle = {
position: 'absolute',
top: '0.5rem',
right: '0.5rem',
backgroundColor: '#e5e7eb',
fontSize: '0.875rem',
padding: '0.25rem 0.5rem',
borderRadius: '0.25rem',
cursor: 'pointer',
transition: 'background-color 0.3s ease',
};

const handleMouseOver = (e) => {
e.target.style.backgroundColor = '#d1d5db';
};

const handleMouseOut = (e) => {
e.target.style.backgroundColor = '#e5e7eb';
};

return (
<button
className="copy-button"
style={buttonStyle}
onClick={handleCopy}
onMouseOver={handleMouseOver}
onMouseOut={handleMouseOut}
>
{copied ? 'Copied!' : 'Copy'}
</button>
);
}
CopyButton.propTypes = {
text: PropTypes.string.isRequired,
};

//integrating the CopyButton component into the CodeBlock component

export default function CodeBlock({ children }) {
const codeText = children?.props?.children?.toString() || '';

return (
<div style={{ position: 'relative' }}>
<CopyButton text={codeText} />
<pre>{children}</pre>
</div>
);
}

CodeBlock.propTypes = {
children: PropTypes.node.isRequired,
};
9 changes: 8 additions & 1 deletion src/components/Page/Page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { useLocation } from 'react-router-dom';
import { MDXProvider } from '@mdx-js/react';

// Import Components
import PageLinks from '../PageLinks/PageLinks';
import Markdown from '../Markdown/Markdown';
import Contributors from '../Contributors/Contributors';
import { PlaceholderString } from '../Placeholder/Placeholder';
import AdjacentPages from './AdjacentPages';
import CodeBlock from '../CodeBlock/CodeBlock';

// Load Styling
import './Page.scss';
import Link from '../Link/Link';

const components = { pre: CodeBlock };

export default function Page(props) {
const {
title,
Expand Down Expand Up @@ -113,7 +118,9 @@ export default function Page(props) {
</div>
) : null}

<div id="md-content">{contentRender}</div>
<MDXProvider components={components}>
<div id="md-content">{contentRender}</div>
</MDXProvider>

{loadRelated && (
<div className="print:hidden">
Expand Down