File tree 5 files changed +65
-1
lines changed
5 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { MDXRemote } from 'next-mdx-remote/rsc' ;
2
+ import { useMDXComponents } from 'mdx-components' ;
2
3
3
4
export default function Markdown ( { children } ) {
5
+ const mdxComponents = useMDXComponents ( { } ) ;
4
6
// @ts -expect-error Server Component
5
- return < MDXRemote source = { children as any } /> ;
7
+ return < MDXRemote source = { children as any } components = { { ... mdxComponents } } /> ;
6
8
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 1
1
import type { MDXComponents } from 'mdx/types' ;
2
2
import Link , { LinkProps } from 'next/link' ;
3
+ import Pre from 'components/common/Pre' ;
3
4
4
5
// This file allows you to provide custom React components
5
6
// to be used in MDX files. You can import and use any
@@ -12,6 +13,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
12
13
// Allows customizing built-in components, e.g. to add styling.
13
14
// h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>,
14
15
a : props => < Link { ...( props as LinkProps ) } /> ,
16
+ pre : Pre ,
15
17
...components ,
16
18
} ;
17
19
}
You can’t perform that action at this time.
0 commit comments