WebDev Bites: CodeViewer Component Interface

resize-able code viewer

CodeViewer — Public Interface

Element placed on page: <code-viewer width="40ch" style="float:right">
  <div>Hello World</div>
</code-viewer>

Slots

Attributes

Visuals (component & code colors)

Sizing & Typography

Highlighting (Prism Integration)

Convenience Transforms

Behaviors & Interaction

Parts (for theming)

Custom Properties (CSS Variables)

Events

No public events. Internal handlers respond to slotchange and click events but no custom events are emitted.

Usage Examples

1) Plain (non-Prism) Viewer

<code-viewer width="50ch" height="18rem" trim normalize-indent 
  font-family="JetBrains Mono" font-size="0.95rem">
  HTML Example
  <template slot="code">
    <div class="note">This appears as literal text, not rendered HTML.</div>
  </template>
</code-viewer>

2) Prism Highlighted Viewer

<link rel="stylesheet" href="prism.css">
<script src="prism.js" defer></script>

<code-viewer highlight="prism" language="javascript" width="52ch" 
  overflow-x="auto" code-padding="0.5rem 0.75rem" trim normalize-indent>
  JS Snippet
  <pre slot="code"><code>
// Your code here...
function hello(name) {
  console.log(`Hello, ${name}`);
}
  </code></pre>
</code-viewer>

3) Interactive Width Stepping

Notes & Limits