---
title: "Code Editor"
description: "Lets people edit code in a page or a form, with syntax colors, line numbers, folding, find and replace, autocomplete, and marks on errors."
url: https://gelatinui.com/components/code-editor
markdown_url: https://gelatinui.com/components/code-editor.md
access: pro
install: "npx shadcn@latest add @gelatin-ui-pro/code-editor"
version: "2026-11-09"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Code Editor

> Lets people edit code in a page or a form, with syntax colors, line numbers, folding, find and replace, autocomplete, and marks on errors.

Page: https://gelatinui.com/components/code-editor

## When to use it

Best for: Edit code in a page or a form: one file with highlight, folding, find, and lint marks.

- Use it when people must edit code or config text in your app.
- Use `readOnly` for a file that people can select, copy, and search but not change.
- Give it a `name` to send the text with a form. A `Field` around it gives the label and the error.
- Pass a CodeMirror `lint` source to show problems as wavy lines and gutter marks.
- Put it in a `CodeBlock` to get the file header, the copy button, and the Wrap toggle.

## When not to use it

- To only show code with no edits, use a [Code Block](https://gelatinui.com/components/code-block.md).
- For a JSON value that people open and close by key, use a [JSON Viewer](https://gelatinui.com/components/json-viewer.md).
- For plain text with no code features, use a [Textarea](https://gelatinui.com/components/textarea.md).

## Notes for agents

- CodeMirror loads with a dynamic `import()` on mount. Until then the same lines show with TanStack Highlight, with no layout shift.
- `language` takes a name or an alias (`tsx`, `json`, `sh`). The language loads on demand.
- Keep `lint` and `extensions` stable (a module constant or a memo). A new value reconfigures the editor.
- Tab indents. Escape, then Tab, leaves the editor. Set `indentWithTab={false}` to make Tab move the focus.
- `onValueChange` runs on user edits only, not on a `value` change.
- Add `CodeEditorStatus` as a child for the cursor place, the language, and the indent.

## Anatomy

```text
CodeEditor
└─ CodeEditorStatus (0..n)
```

## API reference

### CodeEditor

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `autoFocus` | `boolean` | false | Focus the text box when the editor is ready. |
| `palette` | `"color" \| "ink" \| "theme"` |  |  |
| `value` | `string` |  | The text (controlled). |
| `defaultValue` | `string` |  | The text at first (uncontrolled). |
| `onValueChange` | `function` |  | Runs on each edit by the user, with the whole text. Not on a `value` change. |
| `language` | `string` |  | A language name or alias of `#/lib/code-editor-languages-model` (`tsx`, `json`, `sh`). |
| `lineNumbers` | `boolean` | true | Line numbers in the gutter. Default `true`. |
| `folding` | `boolean` | true | Fold blocks from the gutter, and Cmd/Ctrl+Alt+[ ]. Default `true`. |
| `wrap` | `boolean` |  | Wrap long lines. Default: the Wrap toggle of the Code Block, else `false` (lines scroll). |
| `readOnly` | `boolean` | false | The text can be read, selected, copied, and searched, not changed. |
| `disabled` | `boolean` | false | Not focusable and not editable; 50% opacity. |
| `placeholder` | `string` |  | The text of an empty editor. |
| `tabSize` | `number` | 2 | Spaces per indent level, and the width of a tab. Default 2. |
| `indentWithTab` | `boolean` | true | Tab indents and Shift+Tab outdents. Default `true`. Escape, then Tab, leaves the editor (no keyboard trap, WCAG 2.1.2); the description of the text box says so, and the status row shows it. |
| `autocomplete` | `boolean` | true | The completions of the language as you type, and on Ctrl+Space. Default `true`. |
| `search` | `boolean` | true | The find and replace bar on Cmd/Ctrl+F. Default `true`. |
| `lint` | `LintSource` |  | A CodeMirror lint source: its problems show as wavy lines, gutter marks, and tooltips. Keep it stable (a module function or a memo): a new function restarts the linter. |
| `extensions` | `readonly Extension[]` |  | More CodeMirror extensions, after the editor's own. Keep the array stable (a module constant or a memo): a new array reconfigures them. |
| `name` | `string` |  | The form field name: a hidden input carries the text. |
| `form` | `string` |  | The form of the hidden input, by id. |
| `required` | `boolean` | false |  |
| `invalid` | `boolean` | false | The text is wrong: the destructive border and ring, and `aria-invalid`. |
| `labels` | `Partial<CodeEditorLabels>` |  | The words of the editor (`CodeEditorLabels` in `#/lib/code-editor-state`). Keep a custom `unfold` function stable: a new one rebuilds the fold gutter. |
| `children` | `ReactNode` |  | Parts after the code, inside the frame: `CodeEditorStatus`. |

### CodeEditorStatus

No props of its own: `className`, `children`, and every other prop go to the element it renders.

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| CodeEditor | `code-editor` | `data-disabled`, `data-focused`, `data-framed`, `data-invalid` | `--background`, `--code-background`, `--code-editor-action`, `--code-editor-action-hover`, `--code-editor-match`, `--code-editor-match-current`, `--code-editor-surface`, `--foreground`, `--input`, `--warning` |  |
| CodeEditorStatus | `code-editor-status` |  |  |  |

## Usage

Give the editor a `language` and a name for screen readers. The editor loads CodeMirror when it mounts.

```tsx title="config-editor.tsx"
import { CodeEditor, CodeEditorStatus } from "@/components/ui/pro/code-editor";

const CONFIG = `{
  "style": "new-york",
  "tailwind": { "css": "src/styles.css" }
}`;

export function ConfigEditor() {
  return (
    <CodeEditor aria-label="components.json" language="json" defaultValue={CONFIG}>
      <CodeEditorStatus />
    </CodeEditor>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui-pro/code-editor
```

It adds 34 registry items: 31 files, 6147 lines, and the npm packages `@base-ui/react`, `@codemirror/autocomplete`, `@codemirror/commands`, `@codemirror/lang-css`, `@codemirror/lang-html`, `@codemirror/lang-javascript`, `@codemirror/lang-json`, `@codemirror/lang-markdown`, `@codemirror/language`, `@codemirror/legacy-modes`, `@codemirror/lint`, `@codemirror/search`, `@codemirror/state`, `@codemirror/view`, `@lezer/highlight`, `@phosphor-icons/react`, `@tanstack/highlight`, `@xstate/react`, `clsx`, `tailwind-merge`, `tailwind-variants`, `xstate`.

## Built on

- [Button](https://gelatinui.com/components/button.md)
- [Code](https://gelatinui.com/components/code.md)
- [Input Group](https://gelatinui.com/components/input-group.md)
- [Kbd](https://gelatinui.com/components/kbd.md)

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Code Block](https://gelatinui.com/components/code-block.md)
- [Code](https://gelatinui.com/components/code.md)
- [JSON Viewer](https://gelatinui.com/components/json-viewer.md)
- [Textarea](https://gelatinui.com/components/textarea.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Tab, Shift + Tab | Indents or outdents the line. With `indentWithTab={false}`, Tab moves the focus. |
| Escape, then Tab | Leaves the editor. |
| ⌘F, Ctrl+F | Opens the find and replace bar. |
| Enter, Shift + Enter | In the find field: goes to the next or the previous match. |
| Escape | In the find bar: closes it and puts focus back in the code. |
| Ctrl+Space | Opens the completions of the language. |
| ⌘⌥[, Ctrl+Alt+[ | Folds the block at the cursor. Use ] to unfold it. |
| ⌘Z, Ctrl+Z | Undoes the last edit. Add Shift to redo. |

## Accessibility

The text box is the CodeMirror content: a `textbox` with `aria-multiline`, named by `aria-label` or `aria-labelledby`. `invalid`, `required`, and `readOnly` set the ARIA states.

Tab indents. Escape, then Tab, leaves the editor, so the keyboard is never trapped. The text box description says so.

The find bar fields have names, and Match case, Whole word, and Regexp are toggle buttons (`aria-pressed`).

If CodeMirror cannot load, a plain `<textarea>` with the same name and description takes its place.

## Motion

| Part | Motion |
| --- | --- |
| Find bar | popup · 150ms · ease-panel (0.2, 0.4, 0.2, 1) · it scales from 96% and fades in |
