OsirionContentDemo
OsirionContentDemo renders a live example: a Razor component you registered, and, when the demo has source code, a
Code tab next to the preview. OsirionHtmlRenderer uses it for every fenced demo block in
Markdown content whose text is a registered key, which is how the examples on this site are embedded. A page can also
use it directly.
Package Osirion.Blazor.Core, namespace Osirion.Blazor.Components. Source on GitHub.
dotnet add package Osirion.Blazor<PackageReference Include="Osirion.Blazor" />@using Osirion.Blazor.Components
<OsirionTabs AriaLabel="Install command">
<OsirionTab Title=".NET CLI">
<pre><code>dotnet add package Osirion.Blazor</code></pre>
</OsirionTab>
<OsirionTab Title="PackageReference">
<pre><code><PackageReference Include="Osirion.Blazor" /></code></pre>
</OsirionTab>
</OsirionTabs>
@using Osirion.Blazor.Components
@* Renders the demo registered as "install-tabs", with its Preview and Code tabs *@
<OsirionContentDemo Key="install-tabs" />The example above is itself a demo, so it shows the Preview and Code tabs of the demo registered as install-tabs.
Usage
Register the demos once. The Code tab shows the source you pass, usually the demo's own .razor file embedded in the
assembly:
using Osirion.Blazor.Core.Extensions;
using Osirion.Blazor.Core.Services;
builder.Services.AddOsirionContentDemos(demos => demos
.Add<InstallTabsDemo>("install-tabs",
title: "Install command tabs",
source: ContentDemoRegistry.ReadEmbeddedSource(
typeof(InstallTabsDemo).Assembly,
"MySite.Components.Demos.InstallTabsDemo.razor")));
<ItemGroup>
<EmbeddedResource Include="Components\Demos\*.razor" />
</ItemGroup>
In Markdown, write a fenced block with the language demo and the key as its text:
```demo
install-tabs
```
On a Razor page, name the key:
@using Osirion.Blazor.Components
<OsirionContentDemo Key="install-tabs" />
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
Demo |
ContentDemo? |
null |
The demo to render, with its component, parameters, title and source. Takes precedence over Key. |
Key |
string? |
null |
The key of a demo registered with AddOsirionContentDemos, used when Demo is not set. The component renders nothing when neither resolves to a demo. |
EnableCopyButton |
bool |
true |
Whether the source code on the Code tab has a language header and a Copy button. The component loads the copy script itself, once per page, so the button works with or without OsirionHtmlRenderer. |
PreviewText |
string |
"Preview" |
The label of the preview tab. Shown only when the demo has source code. |
CodeText |
string |
"Code" |
The label of the Code tab. Shown only when the demo has source code. |
Theme |
ThemeMode |
ThemeMode.System |
The component's color mode. Light or Dark writes it to data-theme (data-bs-theme under Bootstrap) on the root element; System writes nothing. |
Class |
string? |
null |
Extra CSS classes for the component's root element. |
Style |
string? |
null |
An inline style for the component's root element. |
ContentDemo has Key, ComponentType, and the optional Parameters (passed to the component; entries whose value
is null are left out), Title and Source. Unmatched attributes and Class go on the root <div>; Style is
accepted but the component does not write it.
Behavior
- Keys are 1 to 60 lowercase letters, digits or hyphens, and registering a key twice throws
ArgumentException. - Only blocks at the top level of the content become demos. A
demoblock inside a list or quote, an unknown key, orOsirionHtmlRendererwithEnableDemos="false"leaves it a code block, so a typo is visible instead of silently empty. On GitHub, the same Markdown shows the block as a code block with the key, which keeps files readable there. - A demo with no
Sourcerenders only its preview, without tabs. The component renders nothing when neitherDemonor a registeredKeyresolves. - The source is HTML-encoded before it is shown; it is never run. Demos are components you register, so content can only choose among them. The marker that holds a demo's place in content is added after sanitizing and carries a random value per render, so content cannot forge one.
- The tabs are an OsirionTabs with a group name of their own for each instance, so several demos
on a page do not share radio buttons. The tab group is named by the demo's
Titlefollowed by "example". - Demos render under static SSR like any component. A demo that needs interactivity sets its own render mode.
- The Copy button on the source has the same markup as the HTML renderer's code blocks.
OsirionContentDemoloads the copy script (_content/Osirion.Blazor.Core/js/osirion-copy-button.es5.min.js) itself whenEnableCopyButtonis on and the demo has source, and it runs once per page load, so the button works used directly on a page and insideOsirionHtmlRenderer. The script is loaded through BlazorJSComponents, whichAddOsirionregisters; withoutAddOsirion, callbuilder.Services.AddJSComponents()yourself.
Styling
| Class | Element |
|---|---|
osirion-content-demo |
The root element |
osirion-content-demo-preview |
The box around the live component |
The preview box uses --osirion-spacing-6, --osirion-border-color, --osirion-radius-md and
--osirion-background-primary, each with a fallback value. The tabs and the source use the styles of
OsirionTabs and OsirionHtmlRenderer.