Skip to main content

Code editor

CodeMirror editor for SQL, JSON and YAML on the code ground.

Guidance​

The code editor is CodeMirror 6 on the warm near-black code ground, in Light and Peat alike: Geist Mono at mono-editor, the mm-syn-* syntax colours, a line-number gutter and radius-md. It edits SQL, JSON (role documents) and YAML (pipeline configs), or plain text.

CodeMirror is loaded with a dynamic import in firstUpdated, so the element costs almost nothing until it is used. Until then, and during SSR, it renders a static highlighted copy of value with the same metrics, so nothing moves when the editor arrives. Set lazy="interaction" to wait for the first focus or pointer press before loading at all, for pages with many editors.

Tab indents. To leave the editor from the keyboard, press Escape and then Tab (CodeMirror's tab-focus mode), which the editor announces in its description. When a completion list is open, the first Escape closes it, so press Escape again before Tab. Code always runs left to right, even in a right-to-left page.

Diagnostics (diagnostics) draw a wavy crimson underline, a gutter mark and a message under the editor with the line and column, so an error never relies on colour alone.

The element is form-associated and submits value under name.

Properties​

PropertyAttributeTypeDefaultDescription
languagelanguageCodeEditorLanguage'text'sql, json, yaml or text.
valuevaluestring''The text.
labellabelstring''Accessible name of the editor ("Role document", "Pipeline config").
placeholderplaceholderstring''Shown when the editor is empty. Never a substitute for label.
readonlyreadonlybooleanfalseSelectable and copyable but not editable.
diagnosticsEditorDiagnostic[][]Inline diagnostics: [{ from, to, severity, message }] with document offsets.
resizeresize'vertical' | 'none''vertical'vertical (default) lets people drag the editor taller; none fixes the height.
lazylazy'load' | 'interaction''load'load (default) loads CodeMirror after first render; interaction waits for the first focus or press.
readybooleanfalseTrue once CodeMirror is mounted.
viewThe CodeMirror view, once loaded.
selectedTextstringThe selected text, or the empty string when nothing is selected or the editor has not loaded.
namenamestring''Name submitted with the form.
disableddisabledbooleanfalseDisables the control and excludes it from submission.
requiredrequiredbooleanfalseRequires a value before the form submits.
formHTMLFormElement | null
validityValidityState
validationMessagestring
willValidateboolean
isDisabledbooleanTrue when disabled directly or by a disabled ancestor fieldset.

Methods​

MethodDescription
load()Loads CodeMirror now. Resolves when the editor is interactive.
formResetCallback()Restore the initial value when the owning form resets.

Events​

EventDetailDescription
inputEvent
changeEvent
mm-inputCustomEvent<{ value: string }>The text changed.
mm-changeCustomEvent<{ value: string }>The text changed and the editor lost focus.
mm-readyCustomEvent<Record<string, never>>CodeMirror loaded and the editor is interactive.

Slots​

SlotDescription
badgeStatus shown under the editor, such as a read-only note. Defaults to "Read only" with a lock when readonly.

CSS parts​

PartDescription
frameThe code ground holding the editor.
fallbackThe static highlighted copy shown before CodeMirror loads.
footerThe row under the editor with diagnostics and the badge.
messagesThe list of diagnostic messages.

CSS custom properties​

PropertyDescription
--mm-code-editor-heightInitial height of the editor. Defaults to 240px; people can drag it taller when resize="vertical".

Examples​

Every state of mm-code-editor in Storybook, with keyboard and screen reader tests.