lightview
Version:
A reactive UI library with features of Bau, Juris, and HTMX plus safe LLM UI generation
600 lines (548 loc) • 27.8 kB
HTML
<!-- SEO-friendly SPA Shim -->
<script src="/lightview-router.js"></script>
<script>
if (globalThis.LightviewRouter) {
LightviewRouter.base('/index.html');
}
</script>
<!-- Load the page-specific stylesheet -->
<link rel="stylesheet" href="./index.css">
<!-- Gallery Structure -->
<div class="gallery-page">
<div class="gallery-layout">
<!-- Sidebar Overlay -->
<div id="sidebar-overlay" class="sidebar-overlay"></div>
<!-- Sidebar -->
<div id="gallery-sidebar" class="gallery-sidebar" style="visibility: hidden" src="./component-nav.html"></div>
<!-- Main Content -->
<div id="gallery-main" class="gallery-main">
<!-- Header Container -->
<div
style="position: sticky; top: 0; z-index: 30; background: var(--gallery-surface); border-bottom: 1px solid var(--gallery-border); backdrop-filter: blur(8px);">
<!-- Breadcrumbs Row -->
<div style="padding: 0.75rem 1.5rem 0;">
<script>
(() => {
const { Breadcrumbs } = Lightview.tags;
const breadcrumbs = Breadcrumbs({
id: 'page-breadcrumbs',
items: [
{ label: 'Components', href: '/docs/components' },
{ label: 'Swap' }
]
});
document.currentScript.replaceWith(breadcrumbs.domEl);
})();
</script>
</div>
<!-- Title Row -->
<div class="gallery-header"
style="border-bottom: none; height: auto; padding-top: 0.5rem; padding-bottom: 0.75rem;">
<button id="toggle-btn" class="toggle-btn" aria-label="Toggle Sidebar">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="toggle-icon"
style="stroke: currentColor; stroke-width: 2;">
<path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" />
<path stroke-linecap="round" stroke-linejoin="round" d="M11 19l-7-7 7-7" />
</svg>
</button>
<h1 class="gallery-title">Swap</h1>
</div>
</div>
<!-- Content -->
<div class="gallery-content">
<div class="section-content" style="max-width: 1000px;">
<p class="text-lg" style="opacity: 0.7; margin-bottom: 1.5rem;">
Swap toggles the visibility of two elements with a smooth transition.
Perfect for toggle buttons, theme switchers, and animated icons.
</p>
<!-- Swap Example with Examplify -->
<div class="card bg-base-200" style="margin-bottom: 2rem;">
<div class="card-body">
<h2 class="card-title">Usage Example</h2>
<p class="text-sm" style="opacity: 0.7; margin-bottom: 1rem;">Swap with transition effects
and state binding.</p>
<!-- Tabs -->
<script>
globalThis.switchSyntaxTab = (tabId) => {
const tabs = ['tagged', 'vdom', 'object', 'html'];
tabs.forEach(t => {
const tabEl = document.getElementById(`tab-btn-${t}`);
const contentEl = document.getElementById(`syntax-${t}`);
if (t === tabId) {
tabEl.classList.add('syntax-tab-active');
contentEl.style.display = 'block';
} else {
tabEl.classList.remove('syntax-tab-active');
contentEl.style.display = 'none';
}
});
};
</script>
<div role="tablist" class="syntax-tabs" style="margin-bottom: 1rem;">
<button id="tab-btn-tagged" role="tab" class="syntax-tab syntax-tab-active"
onclick="switchSyntaxTab('tagged')">Tagged</button>
<button id="tab-btn-vdom" role="tab" class="syntax-tab"
onclick="switchSyntaxTab('vdom')">vDOM</button>
<button id="tab-btn-object" role="tab" class="syntax-tab"
onclick="switchSyntaxTab('object')">Object DOM</button>
<button id="tab-btn-html" role="tab" class="syntax-tab"
onclick="switchSyntaxTab('html')">HTML</button>
</div>
<!-- Tagged Syntax -->
<div id="syntax-tagged">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180,
autoRun: true
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, tags, $ } = Lightview;
const { div, p, Swap, Button } = tags;
const isActive = signal(false);
// Controlled swap
const swap = Swap({
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
Swap.On({}, '🥳'),
Swap.Off({}, '😭')
);
// External control
const demo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
swap,
p({ class: 'text-sm' },
() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔'
),
Button({
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
}, 'Toggle from button')
);
$('#example').content(demo);</code></pre>
</div>
<!-- vDOM Syntax -->
<div id="syntax-vdom" style="display: none;">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, $, tags } = Lightview;
const { Swap, Button, div, p } = tags;
const isActive = signal(false);
const demo = {
tag: div,
attributes: { style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
children: [
{
tag: Swap,
attributes: {
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
children: [
{ tag: Swap.On, children: ['🥳'] },
{ tag: Swap.Off, children: ['😭'] }
]
},
{
tag: p,
attributes: { class: 'text-sm' },
children: [() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔']
},
{
tag: Button,
attributes: {
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
},
children: ['Toggle from button']
}
]
};
$('#example').content(demo);</code></pre>
</div>
<!-- Object DOM Syntax -->
<div id="syntax-object" style="display: none;">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, $ } = Lightview;
const isActive = signal(false);
const demo = {
div: {
style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center',
children: [
{
Swap: {
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; },
children: [
{ 'Swap.On': { children: ['🥳'] } },
{ 'Swap.Off': { children: ['😭'] } }
]
}
},
{
p: {
class: 'text-sm',
children: [() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔']
}
},
{
Button: {
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; },
children: ['Toggle from button']
}
}
]
}
};
$('#example').content(demo);</code></pre>
</div>
<!-- HTML Syntax -->
<div id="syntax-html" style="display: none;">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
language: 'html',
minHeight: 180
});
</script><code contenteditable="true" class="language-html"><!-- Import components -->
<script type="module" src="/components/actions/swap.js"></script>
<script type="module" src="/components/actions/button.js"></script>
<div id="demo-root" style="display: flex; flex-direction: column; gap: 1rem; align-items: center">
<lv-swap id="my-swap" effect="flip" class="text-6xl">
<on>🥳</on>
<off>😭</off>
</lv-swap>
<p id="status-text" class="text-sm">Waiting...</p>
<lv-button id="toggle-btn" color="primary" size="sm">Toggle State</lv-button>
</div>
<script type="module">
const { signal, effect, $ } = Lightview;
const isActive = signal(false);
// Link swap state
const swap = document.getElementById('my-swap');
swap.addEventListener('change', (e) => { isActive.value = e.target.checked; });
// External toggle
document.getElementById('toggle-btn').onclick = () => {
isActive.value = !isActive.value;
};
// Reactive sync
effect(() => {
swap.setAttribute('active', isActive.value);
document.getElementById('status-text').textContent =
isActive.value ? 'Party time! 🎉' : 'So sad... 💔';
});
</script></code></pre>
</div>
</div>
</div>
<!-- Reactive Example with Examplify -->
<div class="card bg-base-200" style="margin-bottom: 2rem;">
<div class="card-body">
<h2 class="card-title">Reactive Toggle</h2>
<p class="text-sm" style="opacity: 0.7; margin-bottom: 1rem;">Controlled swap with signal
binding</p>
<!-- Tabs -->
<script>
globalThis.switchReactiveSyntaxTab = (tabId) => {
const tabs = ['tagged', 'vdom', 'object'];
tabs.forEach(t => {
const tabEl = document.getElementById(`reactive-tab-btn-${t}`);
const contentEl = document.getElementById(`reactive-syntax-${t}`);
if (t === tabId) {
tabEl.classList.add('syntax-tab-active');
contentEl.style.display = 'block';
} else {
tabEl.classList.remove('syntax-tab-active');
contentEl.style.display = 'none';
}
});
};
</script>
<div role="tablist" class="syntax-tabs" style="margin-bottom: 1rem;">
<button id="reactive-tab-btn-tagged" role="tab" class="syntax-tab syntax-tab-active"
onclick="switchReactiveSyntaxTab('tagged')">Tagged</button>
<button id="reactive-tab-btn-vdom" role="tab" class="syntax-tab"
onclick="switchReactiveSyntaxTab('vdom')">vDOM</button>
<button id="reactive-tab-btn-object" role="tab" class="syntax-tab"
onclick="switchReactiveSyntaxTab('object')">Object DOM</button>
</div>
<!-- Tagged Syntax -->
<div id="reactive-syntax-tagged">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, tags, $ } = Lightview;
const { div, p, Swap, Button } = tags;
const isActive = signal(false);
// Controlled swap
const swap = Swap({
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
Swap.On({}, '🥳'),
Swap.Off({}, '😭')
);
// External control
const demo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
swap,
p({ class: 'text-sm' },
() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔'
),
Button({
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
}, 'Toggle from button')
);
$('#example').content(demo);</code></pre>
</div>
<!-- vDOM Syntax -->
<div id="reactive-syntax-vdom" style="display: none;">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, $, tags } = Lightview;
const { Swap, Button, div, p } = tags;
const isActive = signal(false);
const demo = {
tag: div,
attributes: { style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center' },
children: [
{
tag: Swap,
attributes: {
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; }
},
children: [
{ tag: Swap.On, children: ['🥳'] },
{ tag: Swap.Off, children: ['😭'] }
]
},
{
tag: p,
attributes: { class: 'text-sm' },
children: [() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔']
},
{
tag: Button,
attributes: {
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; }
},
children: ['Toggle from button']
}
]
};
$('#example').content(demo);</code></pre>
</div>
<!-- Object DOM Syntax -->
<div id="reactive-syntax-object" style="display: none;">
<pre><script>
examplify(document.currentScript.nextElementSibling, {
at: document.currentScript.parentElement,
scripts: ['/lightview.js', '/lightview-x.js'],
styles: ['https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.min.css'],
type: 'module',
minHeight: 180
});
</script><code contenteditable="true">await import('/components/actions/swap.js');
await import('/components/actions/button.js');
const { signal, $ } = Lightview;
const isActive = signal(false);
const demo = {
div: {
style: 'display: flex; flex-direction: column; gap: 1rem; align-items: center',
children: [
{
Swap: {
effect: 'flip',
class: 'text-6xl',
active: () => isActive.value,
onChange: (checked) => { isActive.value = checked; },
children: [
{ 'Swap.On': { children: ['🥳'] } },
{ 'Swap.Off': { children: ['😭'] } }
]
}
},
{
p: {
class: 'text-sm',
children: [() => isActive.value ? 'Party time! 🎉' : 'So sad... 💔']
}
},
{
Button: {
color: 'primary',
size: 'sm',
onclick: () => { isActive.value = !isActive.value; },
children: ['Toggle from button']
}
}
]
}
};
$('#example').content(demo);</code></pre>
</div>
</div>
</div>
<!-- Props Table -->
<h2 class="text-xl font-bold" style="margin-bottom: 1rem;">Props</h2>
<div style="overflow-x: auto; margin-bottom: 2rem;">
<table class="table table-zebra">
<thead>
<tr>
<th>Prop</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>active</code></td>
<td>boolean | function</td>
<td>false</td>
<td>Control swap state (reactive)</td>
</tr>
<tr>
<td><code>effect</code></td>
<td>string</td>
<td>-</td>
<td>'rotate' | 'flip'</td>
</tr>
<tr>
<td><code>onChange</code></td>
<td>function</td>
<td>-</td>
<td>Callback when state changes: <code>(checked) => void</code></td>
</tr>
</tbody>
</table>
</div>
<!-- Sub-components -->
<h2 class="text-xl font-bold" style="margin-bottom: 1rem;">Sub-components</h2>
<div style="overflow-x: auto; margin-bottom: 2rem;">
<table class="table table-zebra">
<thead>
<tr>
<th>Component</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Swap.On</code></td>
<td>Content visible when active/checked</td>
</tr>
<tr>
<td><code>Swap.Off</code></td>
<td>Content visible when inactive/unchecked</td>
</tr>
</tbody>
</table>
</div>
<!-- Swap Gallery -->
<h2 class="text-xl font-bold" style="margin-bottom: 1rem;">Swap Gallery</h2>
<p class="text-sm" style="opacity: 0.7; margin-bottom: 1.5rem;">
Live examples using <code><lv-swap></code> custom elements.
</p>
<script type="module" src="/components/actions/swap.js"></script>
<h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Effects</h3>
<div class="example-flex" style="margin-bottom: 2rem;">
<div class="text-center">
<p class="text-sm opacity-70 mb-2">Rotate</p>
<lv-swap effect="rotate" class="text-4xl">
<on>😈</on>
<off>😇</off>
</lv-swap>
</div>
<div class="text-center">
<p class="text-sm opacity-70 mb-2">Flip</p>
<lv-swap effect="flip" class="text-4xl">
<on>🥳</on>
<off>😭</off>
</lv-swap>
</div>
</div>
<h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Interactive Icons</h3>
<div style="display: flex; gap: 1rem; align-items: center; margin-bottom: 2rem;">
<lv-swap effect="rotate" class="btn btn-circle">
<on>
<svg class="fill-current" xmlns="http://www.w3.org/2000/svg" width="32" height="32"
viewBox="0 0 512 512">
<polygon
points="400 145.49 366.51 112 256 222.51 145.49 112 112 145.49 222.51 256 112 366.51 145.49 400 256 289.49 366.51 400 400 366.51 289.49 256 400 145.49" />
</svg>
</on>
<off>
<svg class="fill-current" xmlns="http://www.w3.org/2000/svg" width="32" height="32"
viewBox="0 0 512 512">
<path d="M64,384H448V341.33H64Zm0-106.67H448V234.67H64ZM64,128v42.67H448V128Z" />
</svg>
</off>
</lv-swap>
<lv-swap class="text-4xl">
<on>❤️</on>
<off>🤍</off>
</lv-swap>
</div>
</div>
</div>
</div>
</div>
</div>