Code Editor
Editable, syntax-highlighted source field on CodeMirror 6, themed entirely through CSS variables.
Presets
Curated looks for c2-code-editor. Open any card in the studio to start from it. The frame comes from
--c2-code-editor--background, --border and --border-radius; the syntax palette from the twelve
--c2-code-editor__theme--token-* variables, which are the same names c2-code-viewer uses.
// The default palette, on the default surface.
export function debounce(fn, wait = 200) {
let timer
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => fn(...args), wait)
}
}
<c2-code-editor
language="javascript"
line-numbers
value="// The default palette, on the default surface.
export function debounce(fn, wait = 200) {
let timer
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => fn(...args), wait)
}
}"
></c2-code-editor>.card {
border-radius: 10px;
padding: 16px;
background: var(--surface);
box-shadow: 0 8px 24px rgb(24 24 27 / 8%);
}
<style>
.borderless {
--c2-code-editor--border: 1px solid transparent;
--c2-code-editor--background: color-mix(in srgb, var(--c2-theme--color-on-surface, #18181b) 3%, var(--c2-theme--color-surface, #ffffff));
--c2-code-editor--border-radius: 10px;
--c2-code-editor--min-height: 96px;
--c2-code-editor__active-line--background: transparent;
--c2-code-editor--color: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-keyword: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-string: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-comment: color-mix(in srgb, #636c76 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-constant: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-function: color-mix(in srgb, #8250df 86%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-type: color-mix(in srgb, #bc4c00 84%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-property: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-variable: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-tag: color-mix(in srgb, #1a7f37 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-punctuation: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-link: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-invalid: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
}
</style>
<c2-code-editor
class="borderless"
language="css"
value=".card {
border-radius: 10px;
padding: 16px;
background: var(--surface);
box-shadow: 0 8px 24px rgb(24 24 27 / 8%);
}"
></c2-code-editor>{
"name": "@c2n/code-editor",
"engine": "codemirror",
"dense": true,
"rows": 24
}
<style>
.dense {
--c2-code-editor--font-size: 11px;
--c2-code-editor--line-height: 1.4;
--c2-code-editor--min-height: 84px;
--c2-code-editor--padding-block: 6px;
--c2-code-editor--padding-inline: 8px;
--c2-code-editor--border-radius: 4px;
--c2-code-editor__gutter--min-width: 24px;
--c2-code-editor--color: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-keyword: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-string: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-comment: color-mix(in srgb, #636c76 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-constant: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-function: color-mix(in srgb, #8250df 86%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-type: color-mix(in srgb, #bc4c00 84%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-property: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-variable: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-tag: color-mix(in srgb, #1a7f37 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-punctuation: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-link: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-invalid: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
}
</style>
<c2-code-editor
class="dense"
language="json"
line-numbers
value='{
"name": "@c2n/code-editor",
"engine": "codemirror",
"dense": true,
"rows": 24
}'
></c2-code-editor><form> <c2-text-field name="title" label="Title"></c2-text-field> <c2-button type="submit">Save</c2-button> </form>
<style>
.accented {
--c2-code-editor--border: 1px solid var(--c2-theme--color-primary, #0265dc);
--c2-code-editor--border-radius: 8px;
--c2-code-editor--box-shadow: 0 0 0 4px rgba(2, 101, 220, 0.08);
--c2-code-editor__gutter--background: rgba(2, 101, 220, 0.04);
--c2-code-editor__gutter--border-right: 1px solid rgba(2, 101, 220, 0.16);
--c2-code-editor--color: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-keyword: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-string: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-comment: color-mix(in srgb, #636c76 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-constant: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-function: color-mix(in srgb, #8250df 86%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-type: color-mix(in srgb, #bc4c00 84%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-property: color-mix(in srgb, #0550ae 69%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-variable: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-tag: color-mix(in srgb, #1a7f37 85%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-punctuation: var(--c2-theme--color-on-surface, #24292e);
--c2-code-editor__theme--token-link: color-mix(in srgb, #0969da 83%, var(--c2-theme--color-on-surface, #18181b));
--c2-code-editor__theme--token-invalid: color-mix(in srgb, #cf222e 77%, var(--c2-theme--color-on-surface, #18181b));
}
</style>
<c2-code-editor
class="accented"
language="html"
line-numbers
value="<form>
<c2-text-field name="title" label="Title"></c2-text-field>
<c2-button type="submit">Save</c2-button>
</form>"
></c2-code-editor>