Code Viewer
PROA code documentation component that shows the rendered result alongside its source.
Usage guide
#A code documentation component showing a rendered result and source. It is not a code editor.
Preview and code
Put the UI in sy-code-render and code in sy-code-script to show both.
Display code only
Pass the source as escaped text. Code-only views remain expanded.
- Highlighting and shared options
- Configure display options on the parent and specify highlighted lines with
highlightLines, such as2,4-6. - Expansion state
defaultExpandedsets the initial state;expandedis the current state controlled by the app.
Examples
#Feature examples
#Choose a feature from 18 topics. Explore property settings, method calls, event results, and slot layouts through live examples and code.
No matching features.
API reference
#Hover over a name or focus it with the keyboard to see the code. On touch screens, tap the name.
<sy-code-viewer>
Properties
| Attribute / Property | Type | Default | Description |
|---|---|---|---|
string | 'plaintext' | Language used for source highlighting. | |
boolean | false | Show line-number gutter. | |
boolean | true | Show the copy-to-clipboard button at top-right of the source panel. | |
boolean | false | Wrap long lines of code. | |
string | '' | Max-height of the script (code panel) section. Accepts any CSS length (`"320px"`, `"40vh"`, …). The live UI section and the collapse trigger are unaffected — only the code panel scrolls past this value. Renamed from the previous `maxHeight` to make the scope (script-only, not viewer-total) explicit at the call site. | |
string | '' | Comma list of line numbers / ranges to emphasize. e.g. "3,5-7,12". 1-based. | |
boolean | undefined | Control the current source-area expansion state externally. | |
boolean | true | Expand the source area initially. | |
boolean | true | When false, the trigger is hidden and the script section is forced expanded. | |
string | 'SOURCE' | Text in the toggle bar. |
Events
| Event | Detail | Description |
|---|---|---|
CustomEvent<{ expanded: boolean; source: 'user' | 'prop' }> | Fires whenever the viewer's expand state changes — by user click on the trigger or by the host updating the `expanded` prop. `source` distinguishes "user click" from "programmatic" for analytics consumers. | |
CustomEvent<{ language: string }> | Fires when the `language` prop changes. | |
CustomEvent<{ raw: string; lines: number[] }> | Fires when the `highlightLines` prop changes. Payload includes parsed line numbers. | |
CustomEvent<{ hasRender: boolean; hasScript: boolean }> | Fires once after the viewer's first render is complete. |
Slots
| Slot | Description |
|---|---|
| Component body or child items. |
<sy-code-render>
Properties
| Attribute / Property | Type | Default | Description |
|---|---|---|---|
string | '' | Authored markup snapshot — captured at connectedCallback time before descendants hydrate. Read by paired `<sy-code-script>` to display the user's authored markup instead of the post-hydration runtime DOM. Mutable so the component can refresh it after user-driven mutations. |
Events
| Event | Detail | Description |
|---|---|---|
CustomEvent<{ html: string }> | Fires whenever the snapshot is refreshed (initial connection or a user-driven mutation that reaches the host's own child list). Paired `<sy-code-script>` listens to this event to re-render the code panel. |
<sy-code-script>
Events
| Event | Detail | Description |
|---|---|---|
CustomEvent<{ code: string; language: string }> | Fires after the rendered copy button writes the code to the clipboard successfully. (Past-tense name avoids the JS property collision with the `copy` method that other web standards reserve.) | |
CustomEvent<{ code: string; lineCount: number; language: string }> | Fires whenever the resolved code changes. | |
CustomEvent<{ line: number }> | Fires when a line number is clicked (when `showLineNumbers` on viewer). |