<light-preview>
LightPreview is a very lightweight code previewer designed to load your code sample
inside of an iFrame for encapsulated viewing. <light-preview> uses PrismJS as it’s
Examples
Nesting a light-preview inside of a light-preview
Loading inside a shadow DOM
By default, previews are loaded inside an <iframe> for true “encapsulation”. This may not always be desirable.
For not as strict encapsulation you can use preview-mode="shadow-dom"
This button lives in a "Shadow DOM" and not inside an iframe!!
Adding Additional buttons
Escaping nested script tags
Highlight Lines
If you check the source code in the above example, you’ll notice a </script>.
Rendering with the preview already open
Add the open boolean attribute to render the preview already open.
Additional Notes
For more on why script tags are used, check out Why script tags for further reading.
API Reference
Imports
<!-- Auto registers as <light-preview> -->
<script type="module" src="https://cdn.jsdelivr.net/npm/light-pen/exports/components/light-preview/light-preview-register.js"></script>
<script type="module">
// Auto registers as <light-preview>
import "https://cdn.jsdelivr.net/npm/light-pen/exports/components/light-preview/light-preview-register.js"
// Manual Register
import LightPreview from "https://cdn.jsdelivr.net/npm/light-pen/exports/components/light-preview/light-preview.js"
LightPreview.define()
// => Registers as <light-preview>
</script>
// Auto registers as <light-preview>
import "light-pen/exports/components/light-preview/light-preview-register.js"
// Manual Register
import LightPreview "light-pen/exports/components/light-preview/light-preview.js"
LightPreview.define()
// => Registers as <light-preview>
Slots
| Name | Description |
|---|---|
resize-icon
|
The icon to display in the resizer button. Override this to provide your own icon. |
summary
|
What to display in source code expander |
source-code-toggle-icon
|
Slot in your own icon to override the default caret. |
above-actions
|
Additional actions placed above the source code toggle |
below-actions
|
Additional actions placed above the source code toggle |
actions
|
Slot in buttons / links to allow for additional actions in the bottom bar. These are placed inline. |
preview-html
|
If you want to run code that is slightly different from the source code you want to display, slot it into “preview-html” |
code
|
Used to display both source code and to power your preview in the iframe. If you slot in “preview-html”, then it will only be used to show / highlight your source code. |
before-expanded-code
|
A slot to add actions such as buttons. This will sit above the source-code-toggle, and when code is expanded, will be above the expanded code. |
after-expanded-code
|
A slot to add actions such as buttons. This will sit above the source-code-toggle, and when code is expanded, will be below the expanded code. |
after-actions
|
A slot add actions below the source-code-toggle button. |
Attributes
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
[Attribute + Property]
src
|
The |
|
string
|
""
|
[Attribute]
sandbox-settings
[Property] sandboxSettings
|
The sandbox settings to provide to the <iframe> |
|
string
|
"allow-downloads allow-forms allow-modals allow-pointer-lock allow-popups allow-presentation allow-scripts"
|
[Attribute + Property]
summary
|
The text to provide in the toggle button
|
|
string
|
"Show source code"
|
[Attribute]
preview-mode
[Property] previewMode
|
Set to true to not use an <iframe> for previewing |
|
"iframe" | "shadow-dom"
|
"iframe"
|
[Attribute + Property]
resizing
|
When the resizer is being dragged, this will be true. |
|
boolean
|
false
|
[Attribute]
preview-html
[Property] previewHtml
|
If |
|
string
|
""
|
[Attribute + Property]
open
|
Whether or not the source code is being shown |
|
boolean
|
false
|
[Attribute]
resize-position
[Property] resizePosition
|
The current position of the resizer. 100 means all the way to right. 0 means all the way to left. |
|
number
|
100
|
[Attribute]
script-scope
[Property] scriptScope
|
When using |
|
"document" | "shadow-dom"
|
"document"
|
[Attribute + Property]
language
|
The language to highlight for. |
|
string
|
"html"
|
[Attribute]
preserve-whitespace
[Property] preserveWhitespace
|
Preserve leading and trailing whitespace. |
|
boolean
|
false
|
[Attribute]
disable-highlight
[Property] disableHighlight
|
If disabled, its on you to provide |
|
boolean
|
false
|
[Attribute]
highlight-lines
[Property] highlightLines
|
A string of possible lines to highlight. Example: “{1-9, 16, 18}” |
|
string
|
""
|
[Attribute]
inserted-lines
[Property] insertedLines
|
A string of lines that are inserted for diffs. Example: “{1-9, 16, 18}” |
|
string
|
""
|
[Attribute]
deleted-lines
[Property] deletedLines
|
A string of lines that are deleted for diffs. Example: “{1-9, 16, 18}” |
|
string
|
""
|
[Attribute]
disable-line-numbers
[Property] disableLineNumbers
|
whether or not to disable line numbers |
|
boolean
|
false
|
[Attribute]
line-number-start
[Property] lineNumberStart
|
Where to start counting indexes in the gutter. Note, this is purely for display purposes. |
|
number
|
1
|
[Attribute + Property]
code
|
We will take the code, wrap it in |
|
string
|
""
|
[Attribute + Property]
wrap
|
If |
|
"soft" | "hard"
|
"soft"
|
[Attribute]
unescape-behavior
[Property] unescapeBehavior
|
Whether or not to transform |
|
"all" | "last" | "none"
|
"last"
|
Functions
| Name | Description | Parameters |
|---|---|---|
highlight()
|
-
|
code
|
addShadowRootToPreview()
|
-
|
root: HTMLElement | Element | ShadowRoot
|
Parts
| Name | Description |
|---|---|
base
|
The base wrapping element |
preview
|
The wrapper around the iframe / div preview showing your code being run in the browser. |
start-panel
|
The iframe or preview-div |
preview-div
|
used for inline previews |
iframe
|
used for iframe based previews (default) |
panel-resizer
|
The button that resizes the panel |
end-panel
|
The white space when dragging the resizer |
source-details
|
The details element that shows the source code |
code-wrapper
|
The div that wraps the <pre> + |
pre
|
The <pre> element wrapping the source code |
code
|
The |
source-code-toggle
|
The button to show the source code |
actions
|
The buttons at the bottom of the code previewer |
before-expanded-code
|
A wrapper around the “before-expanded-code” slot |
after-expanded-code
|
A wrapper around the “after-expanded-code” slot |
after-actions
|
A wrapper around the “after-actions” slot |
source-code-toggle-icon
|
The caret icon in the source code toggle |