Introducing a new way to review your PRs.

Read the announcement

frappe / frappe-ui

The all-in-one toolkit for modern frontends. High-quality components, app recipes and AI-ready docs.

View on GitHub

28 documents · Updated Nov 23, 2026

docs / code-editor-molecule

Code editor molecule

The code editor molecule is a stable CodeMirror-based surface for code-authoring interfaces. It provides the editor lifecycle, content binding, composable parts, capability extensions, language loading, and styling hooks. The consuming application owns field layout and product actions. It is available from frappe-ui/code-editor and complements the editor and list families in Editor and list molecules.

Public surface

Package boundary

The code editor is a separate package surface rather than a root export. Applications import the reviewed editor names from frappe-ui/code-editor, while language packages and other CodeMirror extensions remain application choices.

Public groupMain exportsPurpose
EngineuseCodeEditor, CodeEditorOptionsCreate and own a live editor view
ComponentsCodeEditor, CodeEditorContentCompose the editor with an application-owned layout
Capability bundleCodeKit, CodeKitOptionsStart with common editor behavior and tune selected members
Individual extensionscodeChrome, codeHighlight, codeKeymapAdd Frappe styling, syntax colors, and shared keyboard behavior
Language accessloadLanguage, LanguageKeyLoad a supported language when the language is known at run time
Integration typesCodeEditorExposed, CodeKitExtensionDescribe template-ref and kit contracts

The subpath does not export a labeled field or a product-specific editor. This keeps the stable molecule focused on editor behavior while applications assemble the final field or screen. See the component library overview for the relation between root components and specialized molecules.

References: package.json• L74-94 src/…/index.ts• L1-32

Composition boundary

The molecule separates editor behavior from application layout. CodeEditor owns the live view and content model without drawing a field box. CodeEditorContent supplies the box that contains the CodeMirror DOM. This lets an application place labels, descriptions, errors, actions, or previews around the editor.

Applications can use the molecule in two ways:

  • Use CodeEditor with CodeEditorContent in its default slot.
  • Use useCodeEditor with CodeEditorContent :editor="..." when the application owns the wrapper and view directly.

The following flow shows how content and chosen capabilities become an editor surface and application-owned field behavior.

flowchart LR
  A[Application content] --> B[CodeEditor or useCodeEditor]
  C[CodeKit and extensions] --> B
  B --> D[Live EditorView]
  D --> E[CodeEditorContent]
  E --> F[Application field layout]
  B --> G[Live model and commit events]
  E --> H[Overflow signal]
  G --> F
  H --> F

This boundary keeps labels, validation, expansion controls, and product actions in the consuming application. Related composition patterns appear in Input and selection controls, Overlays and navigation, and Layout and feedback components.

References: src/…/CodeEditor.vue• L35-82 src/…/CodeEditorContent.vue• L13-16 src/…/Field.vue• L11-12 src/…/Primitives.vue• L14-18

Editor lifecycle

Engine and component

useCodeEditor is the engine-facing API. It creates the live EditorView, binds optional text content, applies extensions, reports editor events, and destroys the view with its owner. It returns a ShallowRef<EditorView | null> so composable users can work with the native CodeMirror view.

CodeEditor is the component boundary around that engine. It exposes text through the unnamed v-model, accepts a required reactive extensions array, supports reactive editable state, and supports one-time autofocus. Its renderless template leaves the content position and surrounding UI to the application.

Lifecycle concernPublic contract
ContentOptional Ref<string> for useCodeEditor; unnamed v-model for CodeEditor
CapabilitiesRequired extensions array
Editing stateReactive editable, defaulting to true
Initial focusOptional one-shot autofocus
EventsUpdate, commit, focus, and blur callbacks or component emits
TeardownThe engine destroys the live view with its owner

The template-ref escape hatch exposes the live view itself. A parent can use it for direct actions such as focus or insertion while CodeMirror remains the API for editor-specific operations.

References: src/…/useCodeEditor.ts• L21-39 src/…/useCodeEditor.ts• L74-87 src/…/useCodeEditor.ts• L163-221 src/…/CodeEditor.vue• L8-25 src/…/CodeEditor.vue• L41-74 src/…/types.ts• L4-19

Content part and context

CodeEditorContent is the visual mount point for the editor. It renders the root box, appends the live CodeMirror DOM, accepts normal class fallthrough, and exposes state markers for styling and layout. It does not own the editor view, so the engine can outlive the part when a view is detached.

The editor context lets descendant parts use the live view without passing it through each slot. An explicit editor prop takes priority over the injected view, including an explicit null. This supports direct composition and conditional attachment.

The part reports overflow only when content crosses its height cap or returns below it. The application can use this signal for an expand control or another action. The part marks its root with data-slot="code-editor-content" and marks the overflowing state with data-overflowing="true".

References: src/…/CodeEditorContent.vue• L13-29 src/…/CodeEditorContent.vue• L95-125 src/…/CodeEditorContent.vue• L128-137 src/…/code-editor-context.ts• L11-45 src/…/CodeEditor.cy.ts• L312-340 src/…/CodeEditor.cy.ts• L342-394

Reactive content contract

The editor has a live channel and a commit channel. update:modelValue or the content ref changes on each document edit. change or onChange fires on blur only when the document changed since focus. Applications can use the live channel for state and the commit channel for work such as saving or formatting.

External content writes update the existing document instead of replacing the editor state. This preserves the caret, selection, scroll position, and active input composition when the changed span does not touch them. The engine does not echo an external write through the live update channel.

Reactive extension changes also update the existing view. Document text, selection, undo history, and fold state can survive a language or capability change. Applications should keep extension values stable and change them only when the selected capabilities change.

The browser behavior tests confirm that live updates happen on each keystroke, blur commits happen only after an edit, and a focus-and-blur cycle without an edit emits no commit.

References: src/…/useCodeEditor.ts• L95-135 src/…/useCodeEditor.ts• L157-209 src/…/Field.vue• L42-52 src/…/Languages.vue• L35-42 src/…/CodeEditor.cy.ts• L99-127

Capability model

Extension array

The extensions array is the capability boundary of the molecule. Languages, keymaps, completion sources, linters, themes, and other CodeMirror behavior enter through this array. The editor does not inspect a language prop or maintain feature flags, so an application can add behavior that the library does not define.

CodeKit is a normal starting point, but it is only a value in the array. Applications can use it, use another CodeMirror bundle, or assemble their own array. This keeps the component stable while the consuming product selects its behavior.

References: src/…/useCodeEditor.ts• L15-30 src/…/CodeEditor.vue• L10-21 src/…/Languages.vue• L35-42

CodeKit bundle

CodeKit is a configurable bundle of common code-authoring behavior. Its named members are the small set that applications can enable, configure, or remove. Other base behavior stays inside the fixed editor setup.

MemberDefault role
lineNumbersOff by default; adds the line-number gutter when enabled
foldGutterAdds code-fold controls
autocompletionAdds completion behavior
searchAdds the search panel and key bindings
placeholderAdds hint text for an empty document
chromeAdds the Frappe editor surface styling hook
highlightAdds Frappe syntax colors
keymapAdds Tab indentation and Escape-to-blur behavior

Use .configure() to create a new kit. Passing false removes a member, and chained configuration keeps earlier choices. The shared CodeKit value stays unchanged.

References: src/…/kit.ts• L49-73 src/…/kit.ts• L75-86 src/…/kit.ts• L155-198 src/…/extensions.ts• L102-132 src/…/CodeEditor.cy.ts• L166-230

Standalone extensions

The three Frappe extensions are public on their own. This supports a small or custom extension array when an application does not want the full kit. codeChrome controls the Frappe surface layer. codeHighlight controls syntax colors. codeKeymap controls shared keyboard behavior.

Other product behavior stays outside the molecule. Custom keymaps, server-backed completions, custom search panels, lint rules, and multi-file navigation can travel as ordinary CodeMirror extensions or surrounding application UI.

References: src/…/index.ts• L16-29 src/…/extensions.ts• L15-28 src/…/extensions.ts• L31-42 src/…/extensions.ts• L102-132

Language configuration

Language loading

loadLanguage(key) maps application-facing keys to CodeMirror language extensions. It loads the grammar on demand and returns an extension for the reactive extensions array. An unknown key returns null, which gives the editor plain-text behavior.

The supported keys are:

  • json
  • html
  • javascript
  • python
  • sql
  • markdown
  • css
  • scss
  • yaml
  • xml

Language packages are optional peer dependencies. The application installs the languages it uses and maps its field or document type to a language key. If a known package is missing, the loader reports the package name and an install hint.

References: src/…/languages.ts• L11-39 src/…/languages.ts• L67-99 src/…/Languages.vue• L20-42

Runtime language changes

Language selection can change after mount. The application loads the selected language into a shallow ref, combines it with a stable kit in a computed extension array, and lets the reactive editor configuration apply the result without a remount. This supports fields whose language comes from runtime metadata.

When selection can change quickly, the application must reject stale asynchronous results. Only the extension for the current selection should enter the array.

References: src/…/Languages.vue• L20-42 src/…/useCodeEditor.ts• L169-194

Styling and integration

Styling layers

The styling model has three layers. CodeMirror supplies the base layout. codeChrome adds the Frappe box and its CSS contract. codeHighlight supplies syntax colors. Omitting one Frappe extension removes that layer without changing the others.

The content part accepts both a CSS custom property and an application class for the height cap. The package layout rules use zero specificity so an application class can control max-height. When content crosses the cap, the part updates the overflow state and emits the transition.

Hook groupPurpose
--code-bg, --code-border, --code-radiusSurface and box shape
--code-focus-ringFocus indication
--code-font-size, --code-padding-x, --code-padding-yText scale and inset
--code-min-height, --code-max-heightContent size and overflow cap

There are no variant or size props on the stable molecule. An application maps its own variants and sizes to CSS variable sets or classes on the field wrapper. The Frappe layer also styles gutters, active lines, fold markers, lint gutters, and completion popups so they follow the editor surface.

References: src/…/extensions.ts• L9-28 src/…/extensions.ts• L31-42 src/…/style.css• L1-17 src/…/style.css• L24-41 src/…/style.css• L47-72 src/…/style.css• L85-121 src/…/CodeEditor.cy.ts• L129-164 src/…/CodeEditor.cy.ts• L396-417

Application field boundary

An application that needs a labeled code field should build a thin adapter on top of the molecule. The adapter owns labels, descriptions, errors, required markers, variant and size mapping, expansion controls, previews, and commit-time formatting. The stable molecule supplies editor behavior and the signals that the adapter needs.

For example, a field can listen to overflow to show an expand action and listen to change to format JSON after the user leaves the editor. This keeps product behavior outside the shared editor while allowing one field contract across screens.

The adapter should not replace editor state to update text. It should use the model for external content and normal CodeMirror dispatch operations for editor actions. The template ref exposes the view for focus or insertion when an imperative integration point is needed.

References: src/…/Field.vue• L28-52 src/…/Field.vue• L57-83 src/…/CodeEditor.vue• L62-74 src/…/types.ts• L4-19 src/…/CodeEditor.cy.ts• L232-265

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the frappe-ui/code-editor export map, src/…/index.ts, and documented public names aligned.
  • Keep CodeEditor, useCodeEditor, and CodeEditorContent aligned on view ownership. The engine must own creation and destruction while the part only mounts the view.
  • Keep the live model channel separate from the blur commit channel. A timing change can cause saves or formatting to run at the wrong time.
  • Keep extension identity stable when reactive reconfiguration must preserve undo history, selection, or fold state.
  • Keep CodeKit member defaults, standalone extensions, and keyboard behavior aligned. A kit change can remove a field capability or change focus behavior.
  • Keep language keys, optional peer packages, and the runtime language map aligned. A key can fail only after a user selects it.
  • Keep CodeEditorContent overflow events and data-overflowing state aligned with the height-cap contract.
  • Keep the public --code-* hooks and family stylesheet aligned when editor geometry or color behavior changes.
  • Keep application adapters responsible for labels, validation, expansion, and formatting instead of adding those concerns to the stable molecule.

Important failure modes

TriggerConsequenceGuard
Omitting extensions or passing an empty array when kit behavior is neededThe editor has no chosen language, Frappe chrome, keymap, or other optional capabilityPass CodeKit or a deliberate extension array
Creating fresh extension members during every renderReactive reconfiguration can replace capability state and lose extension-owned history or fold stateBuild kits and language extensions from stable values
Treating change as a keystroke eventSave or formatting work runs under the caret or on an uncommitted documentUse update:modelValue for live state and change for blur-time work
Passing a missing language package to loadLanguageThe selected language fails at load time and the editor has no grammarInstall the matching optional peer package and handle loader errors
Placing label or error expectations on CodeEditorThe screen has no label or validation surface because the component is renderlessAdd those elements in the application field adapter
Setting variant or size as component propsThe styling choice has no effect because those props are not part of the contractSet the documented --code-* variables or classes on the wrapper
Replacing live editor state instead of changing content through the model or a dispatchThe editor can lose bindings, focus state, or configured behaviorKeep the existing view and use the model or CodeMirror dispatch operations
Relying on CSS alone to detect a height-cap crossingAn expand control does not track content changesBind the affordance to CodeEditorContent's overflow event
Detaching a content part without releasing its observers or state markersA departed view can keep stale overflow, scroll, or resize stateLet the content part clear its markers and disconnect its observer when the view changes

Key files

FileWhy to read it
package.jsonDefines the frappe-ui/code-editor package boundary.
src/…/index.tsDefines the curated stable export surface and loads family styles.
src/…/useCodeEditor.tsDefines editor lifecycle, content binding, reactive extensions, and event timing.
src/…/CodeEditor.vueShows the renderless component boundary, model, context, and exposed view.
src/…/CodeEditorContent.vueShows the content mount, explicit-view override, overflow signal, and state markers.
src/…/code-editor-context.tsDefines the injected view contract used by composed parts.
src/…/kit.tsDefines the configurable capability bundle and fixed base.
src/…/extensions.tsDefines standalone Frappe styling and keyboard extensions.
src/…/languages.tsDefines supported language keys and on-demand loading.
src/…/style.cssDefines content layout, Frappe chrome, public CSS hooks, and editor state styling.
src/…/Field.vueDemonstrates the application-owned field boundary and commit-time formatting.
src/…/Languages.vueDemonstrates runtime language changes without remounting the editor.
src/…/Primitives.vueDemonstrates direct composition with useCodeEditor and CodeEditorContent.
src/…/CodeEditor.cy.tsVerifies the public lifecycle, keyboard, overflow, styling, and detach behavior.