1- import React , { ReactNode } from 'react'
1+ import { ReactNode , memo , useMemo } from 'react'
22import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
33import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism'
44
@@ -10,7 +10,30 @@ interface CodeExampleProps {
1010 children : ReactNode
1111}
1212
13- export default function CodeExample ( { title, description, code, language = 'tsx' , children } : CodeExampleProps ) {
13+ function CodeExample ( { title, description, code, language = 'tsx' , children } : CodeExampleProps ) {
14+ // Memoize the code block to prevent re-rendering when only children change
15+ const codeBlock = useMemo ( ( ) => (
16+ < div className = "mb-4 rounded-lg overflow-hidden" >
17+ < SyntaxHighlighter
18+ language = { language }
19+ style = { oneDark }
20+ customStyle = { {
21+ margin : 0 ,
22+ borderRadius : '0.5rem' ,
23+ fontSize : '0.875rem' ,
24+ backgroundColor : '#0f172a' , // slate-900
25+ } }
26+ codeTagProps = { {
27+ style : {
28+ fontFamily : 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace' ,
29+ }
30+ } }
31+ >
32+ { code }
33+ </ SyntaxHighlighter >
34+ </ div >
35+ ) , [ code , language ] )
36+
1437 return (
1538 < div className = "mb-12" >
1639 { title && (
@@ -25,25 +48,7 @@ export default function CodeExample({ title, description, code, language = 'tsx'
2548 ) }
2649
2750 { /* Code Block */ }
28- < div className = "mb-4 rounded-lg overflow-hidden" >
29- < SyntaxHighlighter
30- language = { language }
31- style = { oneDark }
32- customStyle = { {
33- margin : 0 ,
34- borderRadius : '0.5rem' ,
35- fontSize : '0.875rem' ,
36- backgroundColor : '#0f172a' , // slate-900
37- } }
38- codeTagProps = { {
39- style : {
40- fontFamily : 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace' ,
41- }
42- } }
43- >
44- { code }
45- </ SyntaxHighlighter >
46- </ div >
51+ { codeBlock }
4752
4853 { /* Live Example */ }
4954 < div className = "not-prose border border-gray-200 dark:border-gray-800 rounded-lg p-6 bg-white dark:bg-gray-900" >
@@ -53,3 +58,5 @@ export default function CodeExample({ title, description, code, language = 'tsx'
5358 )
5459}
5560
61+ export default memo ( CodeExample )
62+
0 commit comments