Skip to content

Commit d284ac1

Browse files
committed
feat: create lazy loading wrapper for CodeBlock
1 parent 78ba4a7 commit d284ac1

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/components/CodeBlockLazy.jsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { lazy, Suspense } from 'react';
2+
3+
// Lazy load with error boundary
4+
const CodeBlock = lazy(() =>
5+
import('./CodeBlock').catch(() => {
6+
console.error('Failed to load CodeBlock component');
7+
// Return a fallback component on error
8+
return { default: ({ children, className = '' }) => (
9+
<code className={`block bg-gray-100 dark:bg-gray-800 p-3 rounded-lg overflow-x-auto my-4 text-gray-800 dark:text-gray-200 text-sm font-mono whitespace-pre-wrap break-words ${className}`}>
10+
{children}
11+
</code>
12+
)};
13+
})
14+
);
15+
16+
const CodeBlockFallback = ({ children, className = '' }) => (
17+
<code className={`block bg-gray-100 dark:bg-gray-800 p-3 rounded-lg overflow-x-auto my-4 text-gray-800 dark:text-gray-200 text-sm font-mono whitespace-pre-wrap break-words ${className}`}>
18+
{children}
19+
</code>
20+
);
21+
22+
export const LazyCodeBlock = (props) => (
23+
<Suspense fallback={<CodeBlockFallback {...props} />}>
24+
<CodeBlock {...props} />
25+
</Suspense>
26+
);
27+
28+
export const MarkdownCodeBlock = ({ node, inline, className, children, ...props }) => {
29+
const match = /language-(\w+)/.exec(className || '');
30+
const language = match ? match[1] : '';
31+
32+
const isInline = inline !== undefined ? inline : !match;
33+
34+
if (isInline) {
35+
return (
36+
<code className="px-1.5 py-0.5 rounded bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-white font-mono text-sm font-normal">
37+
{children}
38+
</code>
39+
);
40+
}
41+
42+
return (
43+
<LazyCodeBlock
44+
language={language}
45+
inline={false}
46+
className={className}
47+
>
48+
{children}
49+
</LazyCodeBlock>
50+
);
51+
};
52+
53+
export default LazyCodeBlock;

0 commit comments

Comments
 (0)