Kempo UI Icon Kempo UI

Kempo UI

Kempo UI Icon
Base Components
Utils

CodeEditor

Table of Contents

A Monaco-powered code editor component with language support, theme integration, optional toolbar controls, and form association.

Basic Example

A simple code editor with JavaScript syntax highlighting:

<k-code-editor language='javascript'></k-code-editor>

Built-in Controls

Use the controls attribute to show built-in toolbar controls:

<k-code-editor language='javascript' controls='full'></k-code-editor>

Custom Controls

Slot controls into the toolbar for a custom layout:

<k-code-editor language='html'>
<kc-format-code slot='toolbar-top-left'></kc-format-code>
<kc-copy-code slot='toolbar-top-left'></kc-copy-code>
<kc-editor-theme slot='toolbar-top-right'></kc-editor-theme>
<kc-fullscreen slot='toolbar-top-right'></kc-fullscreen>
</k-code-editor>

Setting Height

The default height is 400px. Override it with an inline style or CSS class on the element:

<k-code-editor style='height: 500px'></k-code-editor>

Form Integration

The code editor supports form association. Add a name attribute so the value is submitted with the form, and use required to enforce validation:

<form id='codeForm'>
<k-code-editor name='code' language='javascript' required class='b r'></k-code-editor>
<button type='submit' class='btn mt'>Submit</button>
</form>
<pre id='formOutput'></pre>
<script>
document.getElementById('codeForm').addEventListener('submit', e => {
e.preventDefault();
const data = new FormData(e.target);
document.getElementById('formOutput').textContent = data.get('code');
});
</script>

          
        

Disabled

The disabled attribute makes the editor non-interactive (Monaco is set to readOnly), mutes the toolbar, fades the host, and excludes the field from form submission.

<k-code-editor disabled value="..."></k-code-editor>

JavaScript API

Interact with the editor programmatically:

const editor = document.querySelector('k-code-editor');
// Get/set value
editor.setValue('console.log("hello")');
const code = editor.getValue();
// Change language
editor.setLanguage('html');
// Format code
editor.formatCode();
// Undo / Redo
editor.undo();
editor.redo();
// Copy to clipboard
editor.copyToClipboard();
// Find & Replace
editor.openFind();
// Word wrap / Minimap
editor.setWordWrap(true);
editor.setMinimap(true);
// Font size
editor.increaseFontSize();
editor.decreaseFontSize();
// Fold / Unfold
editor.foldAll();
editor.unfoldAll();
// Change editor theme
editor.setEditorTheme('dark');

JavaScript Reference

Constructor

The CodeEditor component is created via its custom element tag <k-code-editor>.

Properties / Attributes

PropertyAttributeTypeDefaultDescription
namenameString''Form field name
valuevalueString''Editor content
languagelanguageString'javascript'Monaco language mode (e.g. javascript, html, css, json, typescript)
controlscontrolsString''Set to 'full' for built-in toolbar; 'none' or empty for no built-in controls
monacoSrcmonaco-srcString''Custom Monaco CDN base URL
editorThemeeditor-themeString'auto'Theme override: 'auto' (matches site theme), 'light', or 'dark'
disableddisabledBooleanfalseEditor is non-interactive (Monaco set to readOnly), toolbar is muted, host fades, and the field is excluded from form submission
requiredrequiredBooleanfalseWhen true and the editor is empty, the element reports a valueMissing validity error

Global Configuration

Configure the Monaco CDN URL globally before importing the component:

window.kempo = {
monacoUrl: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.52.2/min'
};

Methods

MethodReturnsDescription
getValue()StringReturns the current editor content
setValue(str)thisSets the editor content
clear()thisClears the editor content
formatCode()thisFormats/beautifies the code using Monaco's built-in formatter
selectAll()thisSelects all content
getSelectedText()StringReturns the currently selected text
focus()thisFocuses the editor
setLanguage(lang)thisChanges the syntax language
setEditorTheme(theme)thisSets theme override: 'auto', 'light', or 'dark'
copyToClipboard()thisCopies the editor content to the clipboard
undo()thisUndoes the last edit
redo()thisRedoes the last undone edit
setWordWrap(enabled)thisEnables or disables word wrapping
setMinimap(enabled)thisShows or hides the minimap
openFind()thisOpens Monaco's built-in Find & Replace dialog
increaseFontSize()thisIncreases font size by 2px (max 40)
decreaseFontSize()thisDecreases font size by 2px (min 8)
foldAll()thisFolds all code regions
unfoldAll()thisUnfolds all code regions

Events

EventdetailDescription
ready{ value }Fired when Monaco is initialized and ready
change{ value }Fired when the content changes
input{ value }Fired on each keystroke/edit

Toolbar Slots

SlotDescription
toolbar-top-leftLeft section of the top toolbar
toolbar-top-centerCenter section of the top toolbar
toolbar-top-rightRight section of the top toolbar
toolbar-topFull-width top toolbar
toolbar-bottom-leftLeft section of the bottom toolbar
toolbar-bottom-centerCenter section of the bottom toolbar
toolbar-bottom-rightRight section of the bottom toolbar
toolbar-bottomFull-width bottom toolbar

Available Controls

ElementDescription
<kc-format-code>Formats the code using Monaco's built-in formatter
<kc-copy-code>Copies the editor content to the clipboard
<kc-undo>Undoes the last edit
<kc-redo>Redoes the last undone edit
<kc-word-wrap>Toggles word wrapping on/off
<kc-minimap>Toggles the minimap on/off
<kc-find-replace>Opens Monaco's Find & Replace dialog
<kc-font-size>Container for increase/decrease font size buttons
<kc-font-size-decrease>Decreases the editor font size
<kc-font-size-increase>Increases the editor font size
<kc-fold-all>Toggles fold/unfold all code regions
<kc-fullscreen>Toggles the editor in and out of fullscreen — visible in all modes
<kc-language>Select dropdown to change the syntax language
<kc-editor-theme>Select dropdown to switch between auto, light, and dark editor themes
<k-control-group>Groups controls visually with shared borders
<kc-spacer>Flexible spacer to push controls apart