Skip to main content

CodeEditor

    The CodeEditor provides a text input field that is specifically designed for editing code. It further offers properties to configure e.g. syntax highlighting, spell checks or line wrapping. Once the editor is focused via the keyboard, the user must press Enter to start editing and Escape to quit editing and return to the keyboard navigation flow.

    Import​

    import { CodeEditor } from '@dynatrace/strato-components/editors';

    Demo​

    By default, the CodeEditor is uncontrolled and manages its own internal state. To pre-fill the editor with an initial value, use the defaultValue prop.

    Set language​

    To enable syntax highlighting, specify the desired language with the language property.

    Disable editing​

    To disable editing of the content, use the readOnly property.

    Enable line wrap​

    To enable line wrap as soon as the content overflows, use the lineWrap property.

    Enable actions​

    To display actions in the top-right corner of the editor, render CodeEditor.ActionsMenu as a child. An empty CodeEditor.ActionsMenu renders the built-in "Copy" button, which copies the current editor content to the clipboard. You can't remove or replace it. When you add further actions, such as CodeEditor.Download or custom Menu.* items, the button is replaced with a menu that contains all of them.

    Add download action​

    Add CodeEditor.Download to let users download the content of the editor as a file. Together with the built-in copy action, it turns the button into a menu that contains both actions.

    The file name defaults to code.<extension>. Set fileName to override it. If fileName carries no extension of its own, the extension is derived from the language prop, falling back to .txt when language is unset or has no direct file extension. Characters other than letters, digits, -, and _ are stripped from both the name and the extension.

    Set onDownload to react to the download. The callback fires once the file is handed off to the browser, not once the download finishes.

    Note

    Firefox and Safari may open the content in a new browser tab instead of downloading it, and ignore fileName. Use the browser's save option to store the file.

    Add custom actions​

    To display other actions besides the built-in copy action, pass children to CodeEditor.ActionsMenu. They render after "Copy" and, if present, "Download", and turn the button into a menu. Only Menu.* components are accepted. Unsupported children are ignored.

    Expand to full height​

    To expand the CodeEditor to the full available height of its parent, use the fullHeight property. If you want the CodeEditor to respect the min-height and max-height of the parent, use a flex container.

    Folding​

    The CodeEditor supports folding of code blocks such as functions, objects, and comments. Blocks are detected automatically based on the syntax of the selected language. Folding can be triggered via the gutter icons or keyboard shortcuts:

    • Fold the current code block: Ctrl+Shift+[ (Windows/Linux) / Cmd+Option+[ (macOS).
    • Unfold the current code block: Ctrl+Shift+] (Windows/Linux) / Cmd+Option+] (macOS).
    • Fold all: Ctrl+Alt+[ (Windows/Linux) / Control+Option+[ (macOS).
    • Unfold all: Ctrl+Alt+] (Windows/Linux) / Control+Option+] (macOS).

    Controlled​

    To control which blocks are folded from outside the CodeEditor, pass an array of character start positions to the folding prop.

    The onFoldingChange callback is called whenever the folding state changes — for example, when the user folds or unfolds a block via the gutter or keyboard shortcut. It receives the updated array of folded character positions and is typically paired with the folding prop to keep the external state in sync.

    Uncontrolled​

    For uncontrolled scenarios where the parent doesn't need to track fold positions, use defaultFolding to set the initial folds without managing state externally.

    Change size​

    The CodeEditor component offers a size prop with default and condensed options. The condensed option enables a more compact code display by optimizing space, while the prop defaults to default for a regular view.

    Validate JSON​

    When language="json" is set, the CodeEditor automatically runs a JSON linter. Syntax errors are marked in the editor, and a hover tooltip explains each issue. No additional configuration is needed.

    Show inline diagnostics​

    Use the diagnostics prop to attach per-range validation feedback from any source. Each entry specifies a character range or position, a message, and an optional severity ('error', 'warning', or 'info'). Passing diagnostics also disables the built-in JSON linter so you have full control over what is shown.

    Note

    Keep the diagnostics array referentially stable by storing it in state or wrapping it in useMemo. A new array reference on every render causes the editor to redispatch diagnostics unnecessarily.

    Validate with a server​

    Combine diagnostics with a debounced useEffect and AbortController to run server-side validation. The abort controller cancels any in-flight request when a new keystroke arrives before the previous response has been applied.

    Off-screen diagnostic indicators​

    Set showOffScreenDiagnosticIndicators to show floating pill buttons at the top and/or bottom edge of the editor when one or more error-severity diagnostics are scrolled out of view. Each pill displays the count of off-screen errors in its direction; clicking it scrolls the nearest such error into view.

    When the editor has enough height for both indicators, they appear simultaneously. When only one fits, the top indicator takes priority — the bottom one appears only if there are no errors above.

    showOffScreenDiagnosticIndicators has no effect when there are no error-severity diagnostics in the editor — whether provided via the diagnostics prop or produced by the built-in CodeMirror linter.

    Show lint gutter​

    Set showLintGutter to display a severity marker in the gutter column next to each line that has a diagnostic. showLintGutter has no effect when diagnostics is undefined or empty.

    Note

    Use lint gutters for larger editors with lengthy or complex queries to help users quickly spot errors and warnings. For small editors with simple content, such as basic form inputs, a lint gutter isn't needed.

    Use CodeEditor in a form​

    Use CodeEditor inside a FormField with the name and required props to connect it to form validation. FormFieldMessages displays validation errors beneath the editor. Blur the editor to trigger validation: leaving the field empty shows a valueMissing error. When language="json" is set, the built-in linter also surfaces syntax errors as part of the validation state. Learn about using the FormField.

    Use onValidityChange to subscribe to validation state changes. It fires on blur and whenever the linter settles, reporting the current status, trigger, and any errors. To trigger validation programmatically, call editorRef.current.validate(). It returns a ValidationState with trigger: 'manual' once the linter has finished.

    Control state​

    The CodeEditor also supports controlled behavior, enabling direct management of its state. To do so, pass a handler to onChange and assign the updated value via the value prop.

    Caution

    isControlled is deprecated and will be removed in a future major version when the true uncontrolled and controlled behaviors become the default. Adopting it now lets you opt in early and try it out.

    Set isControlled to make the CodeEditor strictly reflect its value prop. User input is not applied to the editor directly. Instead, every change calls onChange with the new value, and the editor only updates when value changes. This ensures the displayed content always matches the state you control.

    Still have questions?
    Find answers in the Dynatrace Community