Skip to content

Code Editor Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Create a plain editable code area from slotted code. The slotted content sets the initial code, and expand grows the editor to fit its lines.

function greet(name, lastVisit) {
  return "Hello, " + name + "! Welcome back. Your last visit was on " + lastVisit.toLocaleDateString() + ".";
}
Layout
Buttons
State
<div class="code-editor-basic">
<sv-code-editor expand>
<pre>
function greet(name, lastVisit) {
return "Hello, " + name + "! Welcome back. Your last visit was on " + lastVisit.toLocaleDateString() + ".";
}</pre>
</sv-code-editor>
</div>
<style>
.code-editor-basic {
display: grid;
sv-code-editor {
inline-size: min(100%, 32rem);
margin-inline: auto;
}
}
</style>

Syntax Highlighting

Add syntax highlighting with a library like highlight.js. highlighter takes the code and returns highlighted HTML, and codeStylesheet takes the selector of the <link> or <style> that holds the theme.

const message = "Hello World!";

for (const word of message.split(" ")) {
  console.log(word);
}
<!-- Light theme -->
<link class="hljs-light" href="highlight.js/styles/atom-one-light.min.css" rel="stylesheet" />
<!-- Dark theme -->
<link class="hljs-dark" href="highlight.js/styles/atom-one-dark.min.css" rel="stylesheet" />
<sv-code-editor class="highlighted-editor" expand>
<pre>
const message = "Hello World!";
for (const word of message.split(" ")) {
console.log(word);
}</pre>
</sv-code-editor>
<script type="application/x-typescript" module>
import hljs from "highlight.js/lib/core";
import typescript from "highlight.js/lib/languages/typescript";
hljs.registerLanguage("typescript", typescript);
const editor = document.querySelector<CodeEditor>(".highlighted-editor")!;
editor.highlighter = code =>
`<code class="hljs">${hljs.highlight(code, { language: "typescript" }).value}</code>`;
// The docs site color scheme, a page of your own would pick one theme
const preferred = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const stored = document.documentElement.style.colorScheme;
const colorScheme = stored === "dark" || stored === "light" ? stored : preferred;
editor.codeStylesheet = `.hljs-${colorScheme}`;
document.addEventListener("color-scheme-changed", event => {
const theme = (event as CustomEvent<"light" | "dark">).detail as "light" | "dark";
editor.codeStylesheet = `.hljs-${theme}`;
});
</script>
<style>
.highlighted-editor {
max-inline-size: 32rem;
margin-inline: auto;
}
</style>

One Line

Use the editor as a single line input. one-line makes Enter do nothing and strips pasted newlines.

Filter
<div class="one-line-field">
<span id="one-line-filter-label" class="one-line-label">Filter</span>
<sv-code-editor
class="one-line-input"
aria-labelledby="one-line-filter-label"
expand
one-line
value="status == 'open' && assignee != null"
></sv-code-editor>
</div>
<style>
.one-line-field {
display: grid;
gap: 0.4em;
max-inline-size: 32rem;
margin-inline: auto;
.one-line-label {
font-size: 0.85em;
color: var(--sv-gray-3);
}
.one-line-input {
--sv-code-editor-font-sz: 0.95rem;
--sv-code-editor-pad: 0.6rem;
}
}
</style>

Add a title bar or status line inside the editor border. The header and footer slots sit above and below the code.

config.json
{
  "port": 3000,
  "open": true
}
JSON
<sv-code-editor class="framed-editor" aria-labelledby="framed-editor-name" expand line-numbers>
<span id="framed-editor-name" class="editor-header" slot="header">config.json</span>
<pre>
{
"port": 3000,
"open": true
}</pre>
<span class="editor-footer" slot="footer">JSON</span>
</sv-code-editor>
<style>
.framed-editor {
max-inline-size: 32rem;
margin-inline: auto;
.editor-header,
.editor-footer {
padding: 0.35em 0.8em;
font-size: 0.8em;
color: var(--sv-gray-2);
background-color: var(--sv-gray-5);
}
.editor-header {
border-start-start-radius: var(--sv-bdr-rad-md);
border-start-end-radius: var(--sv-bdr-rad-md);
}
.editor-footer {
text-align: end;
border-end-start-radius: var(--sv-bdr-rad-md);
border-end-end-radius: var(--sv-bdr-rad-md);
}
}
</style>

Styling

Restyle the editor frame, text metrics, and copy button, in CSS only. The --sv-code-editor-* variables set the font size, line height, and padding, ::part(wrapper) and ::part(copy-button) restyle the frame and the button, and :state(--focused) styles focus.

export function total(items) {
  return items.length;
}
<sv-code-editor class="styled-editor" copy-button expand line-numbers>
<pre>
export function total(items) {
return items.length;
}</pre>
</sv-code-editor>
<style>
.styled-editor {
--sv-code-editor-font-sz: 0.85rem;
--sv-code-editor-ln-height: 1.9;
--sv-code-editor-ln-num-width: 2.5rem;
--sv-code-editor-pad: 1rem;
max-inline-size: 32rem;
margin-inline: auto;
&::part(wrapper) {
border-color: var(--sv-accent);
border-radius: var(--sv-bdr-rad-lg);
}
&:state(--focused)::part(wrapper) {
box-shadow: 0 0 0 2px var(--sv-accent);
}
&::part(copy-button) {
border-radius: 999px;
}
}
</style>