Skip to content

Code Editor

Last updated 24 Nov 2026

NbCodeEditor is a field for editing code: SVG, JSON, HTML, CSS, Markdown and anything else Monaco, the editor inside VS Code, can highlight. It has the label, helper and error of every other field, takes its colours from the theme, and grows with its content.

Setup ​

Monaco is an optional peer dependency, so install it in the app that uses the editor:

sh
pnpm add monaco-editor

Monaco does part of its work in web workers, and the app supplies them. With Vite, import each worker with ?worker and hand them to Monaco once, before the first editor mounts (in main.ts):

ts
import EditorWorker from 'monaco-editor/editor/editor.worker.js?worker'
// Only for the languages that have a language service:
import JsonWorker from 'monaco-editor/language/json/json.worker.js?worker'

self.MonacoEnvironment = {
  getWorker: (_id: string, label: string) =>
    label === 'json' ? new JsonWorker() : new EditorWorker(),
}

Write the .js extension: Vite does not resolve these worker paths without it. The CSS, HTML and TypeScript workers are at monaco-editor/language/{css,html,typescript}/{css,html,ts}.worker.js. Without a MonacoEnvironment, Monaco falls back to running that work on the main thread, and says so in the console.

Monaco is loaded on the editor's first mount and lands in its own chunk, so a page without an editor never downloads it. An offline app (a service worker precache) should leave that chunk out of the precache unless the editor has to work offline: it is several megabytes.

Basic usage ​

vue
<template>
  <NbCodeEditor
    v-model="settings"
    language="json"
    label="Settings"
    helper="Saved when you leave the page."
  />
</template>

Validation ​

error and warning work as on every field: the message shows under the box and the box takes the state's colour.

vue
<NbCodeEditor
  v-model="text"
  language="json"
  label="Settings"
  :error="valid ? '' : 'This is not valid JSON.'"
/>

Size and layout ​

vue
<!-- A fixed four-line box, without the gutter. -->
<NbCodeEditor
  v-model="notes"
  language="markdown"
  :rows="4"
  :max-rows="4"
  :line-numbers="false"
/>

Monaco options and the instance ​

options is merged over the editor's own construction options for anything the props do not cover. ready hands over the Monaco editor once it is on screen, and the component exposes focus(), format() (the language's formatter, where Monaco has one) and editor.

vue
<template>
  <NbCodeEditor
    ref="code"
    v-model="css"
    language="css"
    :options="{ tabSize: 4, folding: false }"
    @ready="(editor) => editor.revealLine(1)"
  />
  <NbButton variant="secondary" @click="code?.format()">Format</NbButton>
</template>

Props ​

PropTypeDefaultDescription
modelValuestring''The code. Use v-model.
languagestring'plaintext'Monaco language id: xml (for SVG), json, html, css, markdown...
rowsnumber8Lines shown when the content is shorter.
max-rowsnumber24Lines it grows to before it scrolls.
line-numbersbooleantrueShows the line-number gutter.
wrapbooleantrueWraps long lines instead of scrolling sideways.
optionsobjectnoneMonaco construction options, merged over the editor's own.
labelstring''The field's label.
helperstring''Helper text under the field.
errorstring''Error message. Marks the field invalid.
warningstring''Warning message.
placeholderstring''Shown while the editor is empty.
readonlybooleanfalseThe code can be read and selected, not changed.
disabledbooleanfalseRead-only and dimmed.
requiredbooleanfalseMarks the label and the input as required.

Events ​

EventPayloadDescription
update:modelValuestringThe code changed.
readyeditorMonaco is loaded and the editor is on screen.