UNPKG

lightview

Version:

A reactive UI library with features of Bau, Juris, and HTMX plus safe LLM UI generation

583 lines (540 loc) 28.8 kB
<!-- 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: 'Toggle' } ] }); 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">Toggle</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;"> Toggle is a checkbox styled as a switch. Used for binary choices or switching settings on/off. </p> <!-- Basic Example with Examplify --> <div class="card bg-base-200" style="margin-bottom: 2rem;"> <div class="card-body"> <h2 class="card-title">Basic Examples</h2> <p class="text-sm" style="opacity: 0.7; margin-bottom: 1rem;">Toggle switches with various configurations</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: 280, autoRun: true }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { tags, $ } = Lightview; const { div, Toggle } = tags; // 1. Basic toggle const basic = Toggle({ label: 'Wi-Fi', defaultChecked: true, color: 'primary' }); // 2. Toggle with description const withDesc = Toggle({ label: 'Dark Mode', description: 'Switch between light and dark themes', color: 'secondary' }); // 3. Toggle with label on right const labelRight = Toggle({ label: 'Notifications', labelPosition: 'right', color: 'accent' }); // 4. Disabled toggle const disabled = Toggle({ label: 'Maintenance Mode', defaultChecked: true, disabled: true }); // Insert all examples $('#example').content( div({ style: 'display: flex; flex-direction: column; gap: 1rem' }, basic, withDesc, labelRight, disabled) );</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: 280 }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { $, tags } = Lightview; const { Toggle } = tags; const demo = { tag: 'div', attributes: { style: 'display: flex; flex-direction: column; gap: 1rem' }, children: [ { tag: Toggle, attributes: { label: 'Wi-Fi', defaultChecked: true, color: 'primary' } }, { tag: Toggle, attributes: { label: 'Dark Mode', description: 'Switch between light and dark themes', color: 'secondary' } }, { tag: Toggle, attributes: { label: 'Notifications', labelPosition: 'right', color: 'accent' } }, { tag: Toggle, attributes: { label: 'Maintenance Mode', defaultChecked: true, disabled: true } } ] }; $('#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: 280 }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { $ } = Lightview; const demo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem', children: [ { Toggle: { label: 'Wi-Fi', defaultChecked: true, color: 'primary' } }, { Toggle: { label: 'Dark Mode', description: 'Switch between light and dark themes', color: 'secondary' } }, { Toggle: { label: 'Notifications', labelPosition: 'right', color: 'accent' } }, { Toggle: { label: 'Maintenance Mode', defaultChecked: true, disabled: true } } ] } }; $('#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: 280 }); </script><code contenteditable="true" class="language-html">&lt;!-- Import the component (registers lv-toggle) --&gt; &lt;script type="module" src="/components/data-input/toggle.js"&gt;&lt;/script&gt; &lt;div style="display: flex; flex-direction: column; gap: 1rem;"&gt; &lt;lv-toggle label="Wi-Fi" default-checked color="primary"&gt;&lt;/lv-toggle&gt; &lt;lv-toggle label="Dark Mode" description="Switch between light and dark themes" color="secondary"&gt;&lt;/lv-toggle&gt; &lt;lv-toggle label="Notifications" label-position="right" color="accent"&gt;&lt;/lv-toggle&gt; &lt;lv-toggle label="Maintenance Mode" default-checked disabled&gt;&lt;/lv-toggle&gt; &lt;/div&gt;</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 Example</h2> <p class="text-sm" style="opacity: 0.7; margin-bottom: 1rem;">Two-way binding with signals </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: 300 }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { signal, tags, $ } = Lightview; const { div, p, Toggle } = tags; // Signals for settings const wifi = signal(true); const bluetooth = signal(false); const airplane = signal(false); // Reactive toggles const reactiveDemo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 24rem' }, Toggle({ label: 'Wi-Fi', description: () => wifi.value ? 'Connected to HomeNetwork' : 'Disconnected', checked: wifi, color: 'primary' }), Toggle({ label: 'Bluetooth', description: () => bluetooth.value ? 'Discoverable' : 'Off', checked: bluetooth, color: 'info' }), Toggle({ label: 'Airplane Mode', description: 'Disables all wireless connections', checked: airplane, color: 'warning' }), div({ class: 'divider' }), p({ class: 'text-sm font-mono opacity-70' }, () => `WiFi: ${wifi.value}, BT: ${bluetooth.value}, Airplane: ${airplane.value}` ) ); $('#example').content(reactiveDemo);</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: 300 }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { signal, $, tags } = Lightview; const { Toggle } = tags; const wifi = signal(true); const bluetooth = signal(false); const airplane = signal(false); const demo = { tag: 'div', attributes: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 24rem' }, children: [ { tag: Toggle, attributes: { label: 'Wi-Fi', description: () => wifi.value ? 'Connected to HomeNetwork' : 'Disconnected', checked: wifi, color: 'primary' } }, { tag: Toggle, attributes: { label: 'Bluetooth', description: () => bluetooth.value ? 'Discoverable' : 'Off', checked: bluetooth, color: 'info' } }, { tag: Toggle, attributes: { label: 'Airplane Mode', description: 'Disables all wireless connections', checked: airplane, color: 'warning' } }, { tag: 'div', attributes: { class: 'divider' } }, { tag: 'p', attributes: { class: 'text-sm font-mono opacity-70' }, children: [() => `WiFi: ${wifi.value}, BT: ${bluetooth.value}, Airplane: ${airplane.value}`] } ] }; $('#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: 300 }); </script><code contenteditable="true">await import('/components/data-input/toggle.js'); const { signal, $ } = Lightview; const wifi = signal(true); const bluetooth = signal(false); const airplane = signal(false); const demo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 24rem', children: [ { Toggle: { label: 'Wi-Fi', description: () => wifi.value ? 'Connected to HomeNetwork' : 'Disconnected', checked: wifi, color: 'primary' } }, { Toggle: { label: 'Bluetooth', description: () => bluetooth.value ? 'Discoverable' : 'Off', checked: bluetooth, color: 'info' } }, { Toggle: { label: 'Airplane Mode', description: 'Disables all wireless connections', checked: airplane, color: 'warning' } }, { div: { class: 'divider' } }, { p: { class: 'text-sm font-mono opacity-70', children: [() => `WiFi: ${wifi.value}, BT: ${bluetooth.value}, Airplane: ${airplane.value}`] } } ] } }; $('#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>checked</code></td> <td>boolean | signal</td> <td>-</td> <td>Checked state (reactive with signals)</td> </tr> <tr> <td><code>defaultChecked</code></td> <td>boolean</td> <td>false</td> <td>Default checked state for uncontrolled mode</td> </tr> <tr> <td><code>label</code></td> <td>string</td> <td>-</td> <td>Label text displayed next to toggle</td> </tr> <tr> <td><code>labelPosition</code></td> <td>string</td> <td>'left'</td> <td>'left' | 'right' - Position of label relative to toggle</td> </tr> <tr> <td><code>description</code></td> <td>string | function</td> <td>-</td> <td>Description text below the label (can be reactive)</td> </tr> <tr> <td><code>color</code></td> <td>string</td> <td>-</td> <td>'primary' | 'secondary' | 'accent' | 'info' | 'success' | 'warning' | 'error' </td> </tr> <tr> <td><code>size</code></td> <td>string</td> <td>'md'</td> <td>'xs' | 'sm' | 'md' | 'lg'</td> </tr> <tr> <td><code>disabled</code></td> <td>boolean</td> <td>false</td> <td>Disable the toggle</td> </tr> <tr> <td><code>onChange</code></td> <td>function</td> <td>-</td> <td>Callback when value changes: <code>(checked, event) => void</code></td> </tr> <tr> <td><code>useShadow</code></td> <td>boolean</td> <td>*</td> <td>Render in Shadow DOM. *Follows global <code>initComponents()</code> setting</td> </tr> </tbody> </table> </div> <!-- Custom Element Gallery --> <h2 class="text-xl font-bold" style="margin-top: 2rem; margin-bottom: 1rem;">Toggle Gallery</h2> <p class="text-sm" style="opacity: 0.7; margin-bottom: 1.5rem;"> Live examples using <code>&lt;lv-toggle&gt;</code> custom elements. </p> <script type="module" src="/components/data-input/toggle.js"></script> <!-- Colors --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Colors</h3> <div style="display: flex; flex-wrap: wrap; gap: 1rem; margin-bottom: 2rem;"> <lv-toggle label="Default" checked="true"></lv-toggle> <lv-toggle label="Primary" color="primary" checked="true"></lv-toggle> <lv-toggle label="Secondary" color="secondary" checked="true"></lv-toggle> <lv-toggle label="Accent" color="accent" checked="true"></lv-toggle> <lv-toggle label="Success" color="success" checked="true"></lv-toggle> <lv-toggle label="Warning" color="warning" checked="true"></lv-toggle> <lv-toggle label="Info" color="info" checked="true"></lv-toggle> <lv-toggle label="Error" color="error" checked="true"></lv-toggle> </div> <!-- Sizes --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Sizes</h3> <div style="display: flex; align-items: center; flex-wrap: wrap; gap: 1.5rem; margin-bottom: 2rem;"> <lv-toggle size="xs" label="Extra Small" checked="true"></lv-toggle> <lv-toggle size="sm" label="Small" checked="true"></lv-toggle> <lv-toggle size="md" label="Medium" checked="true"></lv-toggle> <lv-toggle size="lg" label="Large" checked="true"></lv-toggle> </div> <!-- Labels and Descriptions --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Labels & Descriptions</h3> <div style="display: flex; flex-direction: column; gap: 1.5rem; margin-bottom: 2rem; max-width: 28rem;"> <lv-toggle label="Notifications" description="Enable push notifications for new messages." color="primary" checked="true"> </lv-toggle> <lv-toggle label="Public Profile" label-position="right" description="Allow others to see your active status." color="secondary"> </lv-toggle> </div> <!-- States --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">States</h3> <div style="display: flex; flex-direction: column; gap: 1rem; margin-bottom: 2rem; max-width: 28rem;"> <lv-toggle label="Disabled Off" disabled="true"></lv-toggle> <lv-toggle label="Disabled On" checked="true" disabled="true"></lv-toggle> </div> </div> </div> </div> </div>