react-codemirror icon indicating copy to clipboard operation
react-codemirror copied to clipboard

Problem with autoresize and min height

Open medihack opened this issue 1 year ago • 2 comments
trafficstars

I want to use react-codemirror in an editor that auto-resizes, but I still want to use a minimum height. Unfortunately, react-codemirror does set height: '100% !important' on the .cm-scroller element. That way, I always end up with something like this when only adding a minHeight and maxHeight:

<CodeMirror minHeight="200px" maxHeight="75vh" theme="dark" value={optionsSource} />

Screenshot 2024-01-12 004855

I would like to have the horizontal scrollbar at the bottom of the editor, but still want it to grow until a given height.

Is there a good workaround for having an auto-resizing editor that also has a minimum height? Or can I turn off the height: '100% !important' somehow?

medihack avatar Jan 11 '24 23:01 medihack

Here is some workaround I found to overwrite the height of cm-scroller set by react-codemirror:

  const theme = EditorView.theme({
    "& div.cm-scroller": {
      minHeight: `${minHeight} !important`,
    },
  })

  return (
    <CodeMirror
      minHeight={minHeight}
      maxHeight="75vh"
      theme={colorScheme === "dark" ? "dark" : "light"}
      extensions={[theme]}
      value={optionsSource}
    />
  )

It's a bit ugly, but now the horizontal scrollbar is on the bottom of the editor pane. But I still wonder why the height: '100% !important' was explicitly set by react-codemirror in the first place. The cm-scroller already has a 100% height (without important) set by CodeMirror itself.

medihack avatar Jan 13 '24 20:01 medihack

Not sure if this is a solution.

<CodeMirror
  height="100%"
  style={{ height: "100%" }}
/>

jaywcjlove avatar Mar 06 '24 09:03 jaywcjlove