Skip to content

Commit 69e998b

Browse files
committed
Make editor height to match number of lines
1 parent 18535e8 commit 69e998b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

example/components/chakra-markdown.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { Box, Text, Link as ChakraLink, Heading, useColorModeValue, useColorMode } from '@chakra-ui/react';
33
import MonacoEditor from '@monaco-editor/react';
44
import ReactMarkdown from 'react-markdown';
55

66
const ChakraMarkdown = React.memo<any>(({ content }) => {
77
const { colorMode } = useColorMode();
88
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light';
9-
9+
const [editorHeight, setEditorHeight] = useState('200px');
1010
const renderers = {
1111
h1: ({ children }) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
1212
h2: ({ children }) => <Heading as="h2" size="lg" mb={2}>{children}</Heading>,
@@ -18,28 +18,34 @@ const ChakraMarkdown = React.memo<any>(({ content }) => {
1818
),
1919
code: ({ inline, children, className }) => {
2020
const language = className ? className.replace('language-', '') : 'plaintext';
21+
const code = String(children);
2122
if (inline) {
22-
return <Text as="code" bg={useColorModeValue("gray.100", "gray.700")} p="1" borderRadius="md">{children}</Text>;
23+
return <Text as="code" bg={useColorModeValue("gray.100", "gray.700")} p="1" borderRadius="md">{code}</Text>;
2324
}
25+
useEffect(() => {
26+
const lines = code.split('\n').length;
27+
const lineHeight = 20; // Approximate height of each line in pixels
28+
setEditorHeight(`${lines * lineHeight}px`);
29+
}, [children]);
2430
return (
2531
<Box border="1px solid" borderColor={useColorModeValue("gray.200", "gray.600")} borderRadius="md" overflow="hidden" my={2}>
2632
<MonacoEditor
27-
height="200px"
33+
height={editorHeight}
2834
language={language}
2935
theme={monacoTheme}
30-
value={String(children).trim()}
36+
value={code}
3137
options={{
3238
readOnly: true,
3339
minimap: { enabled: false },
3440
scrollBeyondLastLine: false,
3541
lineNumbers: "off",
42+
scrollbar: { vertical: 'hidden' },
3643
}}
3744
/>
3845
</Box>
3946
);
4047
},
4148
};
42-
4349
return <ReactMarkdown components={renderers} children={content} />;
4450
});
4551

0 commit comments

Comments
 (0)