Forms
CodeEditor
A labelled field for code, configuration and Markdown source, on CodeMirror 6. Use it when people edit text with syntax; for formatted text use RichTextEditor.
import '@reactloom/code-editor/styles.css';
import { CodeEditor } from '@reactloom/code-editor';Examples
Markdown
Language support comes from a CodeMirror package you install, here @codemirror/lang-markdown.
## Release 2.4 - Faster invoice export - **Fixed:** duplicate payment reminders See [the changelog](https://example.com/changelog).
import { markdown } from '@codemirror/lang-markdown';
import { CodeEditor } from '@reactloom/code-editor';
import { useMemo } from 'react';
const notes = `## Release 2.4
- Faster invoice export
- **Fixed:** duplicate payment reminders
See [the changelog](https://example.com/changelog).
`;
export default function Example() {
// Keep extensions stable between renders.
const extensions = useMemo(() => [markdown()], []);
return (
<div style={{ width: '100%', maxWidth: 640 }}>
<CodeEditor label="Release notes (Markdown)" defaultValue={notes} extensions={extensions} wrap />
</div>
);
}JSON with validation and Tab indentation
The error comes from JSON.parse as you type. Tab indents; press Escape, then Tab, to leave the field.
Applied to every outgoing webhook.
{
"retries": 3,
"timeoutSeconds": 30,
"regions": ["eu-west-1", "ap-south-1"]
}
import { CodeEditor } from '@reactloom/code-editor';
import { useState } from 'react';
const initial = `{
"retries": 3,
"timeoutSeconds": 30,
"regions": ["eu-west-1", "ap-south-1"]
}
`;
export default function Example() {
const [value, setValue] = useState(initial);
let error: string | undefined;
try {
JSON.parse(value);
} catch (e) {
error = (e as Error).message;
}
return (
<div style={{ width: '100%', maxWidth: 640 }}>
<CodeEditor
label="Webhook retry policy (JSON)"
description="Applied to every outgoing webhook."
value={value}
onValueChange={setValue}
error={error}
tabIndents
name="policy"
/>
</div>
);
}API reference
| Prop | Type | Default |
|---|---|---|
labelrequiredNames the editable area. | ReactNode | — |
value / defaultValue / onValueChange | string | — |
extensionsLanguage support and other CodeMirror extensions, e.g. [markdown()]. Keep the array stable (useMemo). | Extension[] | — |
tabIndentsTab inserts indentation; Escape then Tab leaves. Off by default so Tab moves focus. | boolean | false |
lineNumbers | boolean | true |
wrapWrap long lines. | boolean | false |
placeholder | string | — |
description · error · invalid · requiredField states, wired to the editable area. | | — |
disabled · readOnly | boolean | false |
nameSubmits the text in a hidden input. | string | — |
minHeight · maxHeight | number | string | 160 · – |
messagesTranslate the Tab hint. | Partial<CodeEditorMessages> | — |
Customization
Gutters, selection, the active line and the cursor use ReactLoom tokens, so themes apply. Syntax colours come from CodeMirror’s default highlight style; pass your own syntaxHighlighting(…) in extensions to change them.
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-disabled] | present when on |
[data-invalid] | present when on |
[data-readonly] | present when on |
Theme tokens it reads
--pl-color-bg-subtle --pl-color-bg-surface --pl-color-border-focus --pl-color-border-input --pl-color-danger-border --pl-color-fg-disabled --pl-focus-ring-color --pl-focus-ring-width --pl-font-size-sm --pl-radius-md
Accessibility
- The editable area is a multiline textbox named by the label and described by the description and error; required, invalid, read-only and disabled states are exposed.
- Tab is not captured by default, so keyboard users can always leave the field. With tabIndents, Tab indents and Escape followed by Tab leaves; a hint describing this is part of the field’s description.
- Before JavaScript loads, the server renders the text read-only in the same box.
- Search (Ctrl/⌘ + F) opens CodeMirror’s labelled search panel.
| Key | Action |
|---|---|
| Tab | Leave the field (default) · indent (tabIndents) |
| Escape, then Tab | Leave the field when tabIndents is on |
| Ctrl/⌘ + Z · Shift + Z | Undo · redo |
| Ctrl/⌘ + F | Search |
| Ctrl/⌘ + ] · [ | Indent · outdent the line |
Usage guidelines
Do
- Validate as people type and show the first problem with its line.
- Use useMemo for extensions so the editor is not reconfigured on every render.
Don’t
- Don’t use it for prose that people format — use RichTextEditor.
- Don’t turn on tabIndents for short fields where Tab should move to the next field.