Let a reader try an example, inspect its source and come back to their work. Each file has its own copy button. Type a title below, switch to Source, then return to Example to see your edit.
Example viewer example
Example.jsx
import { ExampleViewer } from "./ui/example-viewer";
import { Input } from "./ui/input";
import "./example.css";
const files = [{
filename: "Manuscript.jsx",
code: `import { Input } from "./ui/input";
export default function Manuscript() {
return <Input label="Working title" value="Field notes" />;
}`,
}];
export default function Example() {
return (
<section class="sample">
<h2>Copy desk</h2>
<ExampleViewer title="Manuscript title" files={files}>
<Input label="Working title" value="Field notes" />
</ExampleViewer>
</section>
);
}
example.css
@media screen and (prefers-reduced-motion: no-preference) {
#example button:is(.v-primary, .v-secondary):enabled {
transition: transform 120ms ease-out;
}
#example button:is(.v-primary, .v-secondary):enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button:is(.v-primary, .v-secondary):enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
--example-seed: var(--blokd-primary);
--example-secondary-text: var(--blokd-on-surface-subtle);
}
body {
--blokd-primary: color-mix(in srgb, var(--example-seed) 84%, var(--blokd-on-surface));
--blokd-on-surface-subtle: color-mix(in srgb, var(--example-secondary-text) 84%, var(--blokd-on-surface));
--blokd-input-border: color-mix(in srgb, var(--blokd-on-surface) 55%, var(--blokd-surface));
margin: 0;
padding: clamp(0.8rem, 4vw, 1.5rem);
color: var(--blokd-on-surface);
background: var(--blokd-surface-muted);
font: 1rem/1.5 var(--blokd-font-sans);
}
#example {
display: grid;
gap: 0.8rem;
min-width: 0;
align-content: start;
}
#example > button,
.sample > button {
justify-self: start;
}
.sample,
.tile {
background: var(--blokd-surface);
border: 1px solid var(--blokd-border);
border-radius: var(--blokd-radius-lg);
}
.sample {
display: grid;
gap: 0.8rem;
padding: clamp(0.85rem, 3vw, 1.35rem);
box-shadow: var(--blokd-shadow-xs);
min-width: 0;
}
.tile {
padding: 1rem;
min-width: 0;
}
h2,
h3,
p,
dl,
ul {
margin-block: 0;
}
h2,
h3 {
letter-spacing: -0.025em;
line-height: 1.2;
}
h2 {
font-size: 1.25rem;
}
h3 {
font-size: 1rem;
}
.sample > header:not(.split),
.identity > div,
.receipt {
display: grid;
gap: 0.25rem;
}
.sample > header p,
.tile p,
small {
color: var(--blokd-on-surface-muted);
}
small {
font-size: 0.82rem;
}
.split,
.actions,
.identity {
display: flex;
align-items: center;
gap: 0.65rem;
flex-wrap: wrap;
}
.split {
justify-content: space-between;
padding-block: 0.4rem;
border-bottom: 1px solid var(--blokd-border);
}
.split:last-child {
border-bottom: 0;
}
.metric {
font-size: 2rem;
letter-spacing: -0.06em;
}
.facts {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.45rem 1rem;
}
.facts dt {
color: var(--blokd-on-surface-muted);
}
.facts dd {
margin: 0;
font-weight: 600;
}
.type-sample {
font:
2rem/1.2 Georgia,
serif;
}
.listing {
padding-inline-start: 1.25rem;
}
.ink-strip {
height: 1.2rem;
background: var(--blokd-primary);
border-radius: var(--blokd-radius-sm);
}
.ink-proof {
display: grid;
gap: 0.15rem;
border-left: 0.6rem solid;
padding-left: 0.8rem;
}
.ink-proof strong {
font-size: 1.25rem;
letter-spacing: 0.06em;
}
.glyph {
display: inline-grid;
place-items: center;
width: 2rem;
height: 2rem;
border-radius: var(--blokd-radius-sm);
color: var(--blokd-on-primary-subtle);
background: var(--blokd-primary-subtle);
}
.cover-box {
width: min(100%, 18rem);
}
.cover {
display: flex;
min-height: 7.5rem;
height: 100%;
padding: 1rem 1.25rem;
flex-direction: column;
justify-content: space-between;
gap: 1.6rem;
color: #fff;
background: repeating-linear-gradient(150deg, transparent 0 8px, #fff2 8px 9px), #204a48;
}
.cover strong {
font:
italic 2.45rem/1 Georgia,
serif;
}
.cover span {
font: 0.78rem var(--blokd-font-mono);
}
.cover.grove {
background-color: #3d4b27;
}
.cover.dune {
background-color: #7c4428;
}
button:not([class]),
.entry {
font: inherit;
font-size: 0.875rem;
padding: 0.45rem 0.8rem;
border: 1px solid var(--blokd-border-strong);
border-radius: var(--blokd-radius-control);
background: var(--blokd-surface);
color: var(--blokd-on-surface);
}
button:not([class]) {
cursor: pointer;
font-weight: 600;
}
button:not([class]):hover:enabled {
background: var(--blokd-primary-subtle);
}
button:disabled {
cursor: default;
}
:focus-visible {
outline: 2px solid var(--blokd-primary);
outline-offset: 3px;
}
output:not(:empty) {
display: block;
color: var(--blokd-on-primary-subtle);
font-weight: 600;
font-size: 0.9rem;
}
#example, .sample { grid-template-columns: minmax(0, 1fr); }
.sample > header.split { gap: .75rem; flex-wrap: nowrap; }
.journal-preview { width: min(100%, 26rem); margin-inline: auto; }
.journal-preview header { padding-block: 0 .75rem; }
.journal-preview article { display: grid; gap: .45rem; padding-block: .45rem; }
.journal-preview article p { color: var(--blokd-on-surface-muted); }
Site-owned component source and scoped styles
src/ui/example-viewer
import './theme.css';
import { scope, type Ctx, type View } from '@fierst/blokd';
import { CodeBlock } from './copy-button';
import { SegmentedControl } from './segmented-control';
const viewerScope = scope`
:scope { min-width:0; border:1px solid var(--blokd-border); border-radius:var(--blokd-radius-lg); color:var(--blokd-on-surface); background:var(--blokd-surface); }
.toolbar { display:flex; align-items:center; justify-content:space-between; gap:.75rem; flex-wrap:wrap; padding:.75rem 1rem; border-bottom:1px solid var(--blokd-border); }
.panel { min-width:0; padding:1rem; }
.panel > * + * { margin-top:.75rem; }
`;
const modes = [
{ value: 'example', label: 'Example' },
{ value: 'source', label: 'Source' },
];
export function* ExampleViewer(
props: {
title: string;
children: View;
files: readonly { filename: string; code: string }[];
},
ctx: Ctx,
) {
let mode = 'example';
for (props of ctx) {
yield (
<section class={viewerScope} aria-label={props.title}>
<div class="toolbar">
<strong>{props.title}</strong>
<SegmentedControl
label={props.title + ' view'}
options={modes}
value={mode}
onChange={(value) => {
mode = value;
ctx.refresh();
}}
/>
</div>
<div class="panel" hidden={mode !== 'example'}>
{props.children}
</div>
<div class="panel" hidden={mode !== 'source'}>
{props.files.map((file) => (
<CodeBlock key={file.filename} filename={file.filename} code={file.code} />
))}
</div>
</section>
);
}
}
Install
sh
npx @fierst/blokd-ui@1.1.0 add example-viewerRegistry dependencies: theme, segmented-control, copy-button.
Exports from ./ui/example-viewer: ExampleViewer.
Download the registry source for example-viewer 1.1.0.
API contracts
ts
export function* ExampleViewer(
props: {
title: string;
children: View;
files: readonly { filename: string; code: string }[];
},
ctx: Ctx,
)