Web Component
The same component is also available as a native custom element, wrapped with Vue's defineCustomElement (Vue 3 only). See Vue and Web Components in the Vue documentation for how Vue custom elements behave.
There are two entry points:
Automatic registration
Importing vue-codemirror6/wc-auto registers <code-mirror>:
Registering your own tag name
Import vue-codemirror6/wc and call register() with the tag name you want:
You can also define it yourself with the default export:
A constructor can only be defined once per custom element registry. Register a single tag name, and do not combine vue-codemirror6/wc with vue-codemirror6/wc-auto on the same page; defining a second tag name throws NotSupportedError.
vue-codemirror6/wc does not add anything to HTMLElementTagNameMap, so declare the type of your tag yourself:
Using from a CDN (jsDelivr)
Both entries can be used without a bundler by loading the ESM files from jsDelivr through an import map (version 1.8.0 or later):
CodeMirror stops working when two copies of @codemirror/state (or other @codemirror/* packages) are loaded, and the element stops working when two copies of Vue are loaded. Map every package to a single URL as above, and do not use jsDelivr's /+esm URLs: they pin dependency versions independently for each package, which easily leads to duplicates.
- The last two entries are for
@codemirror/lang-javascript. Add the language packages you use, together with their@lezer/*parser packages, the same way. - To use your own tag name, map
"vue-codemirror6/wc": "https://cdn.jsdelivr.net/npm/vue-codemirror6@1/dist/wc.es.js"instead and callregister(). - The
*.umd.jsbuilds (vue-codemirror6/umd,vue-codemirror6/wc/umd,vue-codemirror6/wc-auto/umd) are for CommonJS / AMD loaders and cannot be loaded with a plain<script>tag.
Notes
- When using the element inside Vue templates, mark it as a custom element with
compilerOptions.isCustomElement(e.g.tag => tag === 'code-mirror'). - The editor is rendered inside an open shadow root.
- The examples below use
<code-mirror>; replace it with your own tag name when usingvue-codemirror6/wc.
Setting props
- Primitive props (
string,boolean,number) can be set as attributes or as DOM properties. - Object, array, and function props (
lang,linter,extensions,theme,keymap, …) can only be set as DOM properties.
Set properties before the element is connected to apply them on the first render:
Events
Vue emits are dispatched as native CustomEvents. The emitted arguments are available as an array in event.detail.
Instance API
The instance API is exposed directly on the element: