Skip to content

Commit 64a3349

Browse files
authored
Merge pull request #170 from patelvivekdev/master
Add: Copy Code Feature in the Documentation
2 parents 39ed540 + baa6055 commit 64a3349

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

src/assets/clipboard.svg

+1
Loading

src/components/common/Markdown.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { MDXRemote } from 'next-mdx-remote/rsc';
2+
import { useMDXComponents } from 'mdx-components';
23

34
export default function Markdown({ children }) {
5+
const mdxComponents = useMDXComponents({});
46
// @ts-expect-error Server Component
5-
return <MDXRemote source={children as any} />;
7+
return <MDXRemote source={children as any} components={{ ...mdxComponents }} />;
68
}

src/components/common/Pre.module.css

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.container {
2+
position: relative;
3+
}
4+
5+
6+
.button {
7+
border:0px;
8+
color: var(--base900);
9+
background: var(--base100);
10+
position: absolute;
11+
right: 0.5rem;
12+
top: 0.5rem;
13+
height: 2rem;
14+
width: 2rem;
15+
cursor: pointer;
16+
font-size: max(14px, min(2vw, 16px));
17+
}
18+
19+
.clipboard {
20+
color: var(--primary400)
21+
}

src/components/common/Pre.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use client';
2+
import { useState, useRef } from 'react';
3+
import Clipboard from 'assets/clipboard.svg';
4+
import styles from './Pre.module.css';
5+
6+
const Pre = (props: any) => {
7+
const textInput = useRef<any>(null);
8+
const [hovered, setHovered] = useState(false);
9+
const [copied, setCopied] = useState(false);
10+
11+
const onEnter = () => {
12+
setHovered(true);
13+
};
14+
const onExit = () => {
15+
setHovered(false);
16+
setCopied(false);
17+
};
18+
const onCopy = () => {
19+
setCopied(true);
20+
navigator.clipboard.writeText(textInput?.current?.textContent);
21+
setTimeout(() => {
22+
setCopied(false);
23+
}, 2500);
24+
};
25+
26+
return (
27+
<div ref={textInput} onMouseEnter={onEnter} onMouseLeave={onExit} className={styles.container}>
28+
{hovered && (
29+
<button aria-label="Copy code" className={styles.button} onClick={onCopy}>
30+
{copied ? <Clipboard className={styles.clipboard} /> : <Clipboard />}
31+
</button>
32+
)}
33+
<pre>{props.children}</pre>
34+
</div>
35+
);
36+
};
37+
38+
export default Pre;

src/mdx-components.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { MDXComponents } from 'mdx/types';
22
import Link, { LinkProps } from 'next/link';
3+
import Pre from 'components/common/Pre';
34

45
// This file allows you to provide custom React components
56
// to be used in MDX files. You can import and use any
@@ -12,6 +13,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
1213
// Allows customizing built-in components, e.g. to add styling.
1314
// h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>,
1415
a: props => <Link {...(props as LinkProps)} />,
16+
pre: Pre,
1517
...components,
1618
};
1719
}

0 commit comments

Comments
 (0)