Implementing CodeMirror 6 in react (next.js). Everything works but the initial load seems to render the gutter numbers incorrectly.
As soon as I “click” into the textarea the gutter line heights get adjusted.
Been scouring the documentation and tried to trigger updates, but there doesn’t seem to be anything to available to mess with the initial load height.
Hook for calling codemirror
const useCodeMirror = <T extends Element>(
props: Props
): [React.MutableRefObject<T | null>, EditorView?] => {
const refContainer = useRef<T>(null)
const [editorView, setEditorView] = useState<EditorView>()
const { onChange } = props
useEffect(() => {
if (!refContainer.current) return
const fixedHeightEditor = EditorView.theme({
".cm-gutter, .cm-content": {height: "500px"},
".cm-scroller": {overflow: "auto"},
"&.cm-editor": {border: "1px solid #ddd", borderRadius: "4px"},
})
const startState = EditorState.create({
doc: props.initialDoc,
extensions: [
basicSetup,
keymap.of(defaultKeymap),
markdown({
base: markdownLanguage,
codeLanguages: languages,
addKeymap: true
}),
gutter({renderEmptyElements: true}),
fixedHeightEditor,
EditorView.lineWrapping,
EditorView.updateListener.of(update => {
if (update.heightChanged) {
console.log('heightChanged')
}
if (update.changes) {
onChange && onChange(update.state)
}
}),
]
})
const view = new EditorView({
state: startState,
parent: refContainer.current,
})
setEditorView(view)
return () => {
view.destroy()
}
}, [refContainer])
return [refContainer, editorView]
}
Component snippet to call the hook
const [refContainer, editorView] = useCodeMirror<HTMLDivElement>({
initialDoc: values['content'],
onChange: handleChange,
})
return (
<Box mb="15px" w={{ base: '300px', md: '600px', lg: '800px' }}>
<Heading w="100%" textAlign={'center'} fontWeight="normal">
{contentField.label}
</Heading>
<Box w="full" gap={4}>
<Box ref={refContainer}>
</Box>
</Box>
</Box>
)