Examples

Markdown

Language support comes from a CodeMirror package you install, here @codemirror/lang-markdown.

Preview
## Release 2.4

- Faster invoice export
- **Fixed:** duplicate payment reminders

See [the changelog](https://example.com/changelog).
Example.tsx
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.

Preview

Applied to every outgoing webhook.

{
  "retries": 3,
  "timeoutSeconds": 30,
  "regions": ["eu-west-1", "ap-south-1"]
}
Tab indents. Press Escape, then Tab, to leave the editor.
Example.tsx
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

CodeEditor props
PropTypeDefault
labelrequired
Names the editable area.
ReactNode—
value / defaultValue / onValueChange
string—
extensions
Language support and other CodeMirror extensions, e.g. [markdown()]. Keep the array stable (useMemo).
Extension[]—
tabIndents
Tab inserts indentation; Escape then Tab leaves. Off by default so Tab moves focus.
booleanfalse
lineNumbers
booleantrue
wrap
Wrap long lines.
booleanfalse
placeholder
string—
description · error · invalid · required
Field states, wired to the editable area.
—
disabled · readOnly
booleanfalse
name
Submits the text in a hidden input.
string—
minHeight · maxHeight
number | string160 · –
messages
Translate 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.

AttributeValues
[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.
Keyboard interactions
KeyAction
TabLeave the field (default) · indent (tabIndents)
Escape, then TabLeave the field when tabIndents is on
Ctrl/⌘ + Z · Shift + ZUndo · redo
Ctrl/⌘ + FSearch
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.