Code Editor
Editable, syntax-highlighted source field on CodeMirror 6, themed entirely through CSS variables.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
value | string | — | The source code. Assigning it replaces the document without losing scroll position or undo history. |
language | string | — | Language id: one of BUILT_IN_LANGUAGES, anything languageLoader resolves, or empty for no highlighting. |
name | string | — | Form field name. |
label | string | — | Label above the editor, when the label slot is empty. |
aria-label | string | null | null | Accessible name when nothing visible labels the editor. Without either, the editor is named "Code editor" in the page's language. |
placeholder | string | — | Text shown while the document is empty. |
readonly | boolean | false | The document cannot be edited, but is still selectable and focusable. |
disabled | boolean | false | Blocks interaction entirely and dims the editor. |
required | boolean | false | The form is invalid while the value is empty. |
line-numbers | boolean | false | Show the line-number gutter. |
wrap | boolean | false | Wrap long lines instead of scrolling horizontally. |
autocomplete | boolean | false | Offer completions while typing (identifiers, and whatever the grammar contributes). |
tab-size | number | 2 | Width of a tab stop, in characters. |
error | boolean | false | Renders the error styling; error-text replaces the supporting text. |
error-text | string | — | Message shown in place of the supporting text while error is set. |
help | string | — | Help text under the editor, when the supporting-text slot is empty. |
Slots
| Name | Description |
|---|---|
label | Label shown above the editor. Falls back to the label attribute. |
supporting-text | Help text under the editor. Replaced by error-text while error is set. |
Events
| Name | Type | Description |
|---|---|---|
ready | CustomEvent<{ engine: 'codemirror' | 'basic' }> | The engine settled, so tests and hosts can wait for it. Does not bubble. |
input | Event | The document changed. Fires on every edit. |
change | Event | The value was committed: the editor lost focus after an edit, as a native control does. |
CSS parts
Shadow DOM styling hooks used with ::part(name). Prefer CSS custom properties when they cover the change.
| Name | Description |
|---|---|
container | The box around the label, editor and supporting text. |
label | Component-owned text region containing the label slot above the editor. |
editor | The element CodeMirror renders into, or the fallback textarea. |
supporting-text | Help and error row containing the supporting-text slot or generated fallback. |
CSS custom properties
Grouped by semantic target and state from --c2-<component>__<target>__<state>--<property>. Click a group to collapse it; click a name to copy it.
| Name | Type | Default | Description |
|---|---|---|---|
--c2-code-editor--background | color | #ffffff | |
--c2-code-editor--color | color | #24292e | Foreground of text no token class matched. |
--c2-code-editor--border | border | 1px solid #bcbcc6 | |
--c2-code-editor--border-radius | border-radius | 6px | |
--c2-code-editor--box-shadow | box-shadow | — | |
--c2-code-editor--font-family | font-family | ui-monospace, SFMono-Regular, Menlo, Consolas, monospace | |
--c2-code-editor--font-size | font-size | 13px | |
--c2-code-editor--line-height | line-height | 1.6 | |
--c2-code-editor--min-height | pixel | 120px | |
--c2-code-editor--max-height | pixel | 420px | The editor scrolls past this height. A height set on the element itself reaches the editor (the label and supporting text keep theirs); set this to none to fill it. |
--c2-code-editor--padding-block | padding | 10px | |
--c2-code-editor--padding-inline | padding | 12px | |
--c2-code-editor--gap | pixel | 6px | Space between the label, the editor and the supporting text. |
--c2-code-editor__focus--border | border | 1px solid rgb(2, 101, 220) | |
--c2-code-editor__focus--outline | outline | 2px solid rgba(2, 101, 220, 0.4) | |
--c2-code-editor__disabled--opacity | opacity | 0.38 | |
--c2-code-editor__gutter--background | color | transparent | |
--c2-code-editor__gutter--color | color | #71717a | |
--c2-code-editor__gutter--border-right | border | 1px solid #e4e4e7 | |
--c2-code-editor__gutter--min-width | pixel | 32px | |
--c2-code-editor__gutter__active--color | color | #18181b | |
--c2-code-editor__active-line--background | color | rgba(2, 101, 220, 0.04) | |
--c2-code-editor__selection--background | color | rgba(2, 101, 220, 0.18) | |
--c2-code-editor__cursor--color | color | #18181b | |
--c2-code-editor__matching-bracket--background | color | rgba(2, 101, 220, 0.16) | |
--c2-code-editor__placeholder--color | color | #71717a | |
--c2-code-editor__theme--token-keyword | color | #cf222e | |
--c2-code-editor__theme--token-string | color | #032f62 | |
--c2-code-editor__theme--token-comment | color | #636c76 | |
--c2-code-editor__theme--token-constant | color | #0550ae | |
--c2-code-editor__theme--token-function | color | #6f42c1 | |
--c2-code-editor__theme--token-type | color | #953800 | |
--c2-code-editor__theme--token-property | color | #0550ae | |
--c2-code-editor__theme--token-variable | color | #24292e | |
--c2-code-editor__theme--token-tag | color | #116329 | |
--c2-code-editor__theme--token-punctuation | color | #24292e | |
--c2-code-editor__theme--token-link | color | #032f62 | |
--c2-code-editor__theme--token-invalid | color | #cf222e | |
--c2-code-editor__label--color | color | #18181b | |
--c2-code-editor__label--font-size | font-size | 12px | |
--c2-code-editor__label--font-weight | font-weight | 500 | |
--c2-code-editor__supporting-text--color | color | #71717a | |
--c2-code-editor__supporting-text--font-size | font-size | 12px | |
--c2-code-editor__error--color | color | #dc2626 | |
--c2-code-editor__error--border | border | 1px solid #dc2626 | |
Text and languages
The text the component shows, in English. It shows another language when the app adds one with addTranslations, under code-editor, and the nearest lang asks for it; any message not translated stays in English. {0}, {1}… stand for the values a message carries. Languages guide.
| Key | English | Values |
|---|---|---|
codeEditor | Code editor | — |
valueMissing | Please enter some code. | — |
find | Find | — |
replace | Replace | — |
next | next | — |
previous | previous | — |
all | all | — |
matchCase | match case | — |
regexp | regexp | — |
byWord | by word | — |
replaceOne | replace | — |
replaceAll | replace all | — |
close | close | — |
currentMatch | current match | — |
onLine | on line | — |
replacedMatches | replaced $ matches | — |
replacedMatchOnLine | replaced match on line $ | — |
goToLine | Go to line | — |
go | go | — |
foldLine | Fold line | — |
unfoldLine | Unfold line | — |
foldedCode | folded code | — |
unfold | unfold | — |
foldedLines | Folded lines | — |
unfoldedLines | Unfolded lines | — |
to | to | — |
controlCharacter | Control character | — |
completions | Completions | — |
selectionDeleted | Selection deleted | — |