UNPKG

lightview

Version:

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

685 lines (634 loc) 33.1 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: 'Select' } ] }); 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">Select</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;"> Select allows the user to pick a value from a list of options. Supports labels, validation, helper text, and reactive state binding. </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;">Simple selects 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: 320, autoRun: true }); </script><code contenteditable="true">await import('/components/data-input/select.js'); const { tags, $ } = Lightview; const { div, Select } = tags; // 1. Basic select with string options const basic = Select({ placeholder: 'Pick a movie franchise', options: ['Star Wars', 'Harry Potter', 'Lord of the Rings', 'Marvel'] }); // 2. Select with label and helper const withLabel = Select({ label: 'Country', placeholder: 'Select your country', options: [ { value: 'us', label: 'United States' }, { value: 'uk', label: 'United Kingdom' }, { value: 'ca', label: 'Canada' }, { value: 'au', label: 'Australia' } ], helper: 'We use this for shipping calculations' }); // 3. Required select with color const required = Select({ label: 'Priority', placeholder: 'Select priority...', options: ['Low', 'Medium', 'High', 'Critical'], color: 'primary', required: true }); // Insert all examples $('#example').content( div({ style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;' }, basic, withLabel, required) );</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: 320 }); </script><code contenteditable="true">await import('/components/data-input/select.js'); const { $, tags } = Lightview; const { Select, div } = tags; const basic = { tag: Select, attributes: { placeholder: 'Pick a movie franchise', options: ['Star Wars', 'Harry Potter', 'Lord of the Rings', 'Marvel'] } }; const withLabel = { tag: Select, attributes: { label: 'Country', placeholder: 'Select your country', options: [ { value: 'us', label: 'United States' }, { value: 'uk', label: 'United Kingdom' }, { value: 'ca', label: 'Canada' }, { value: 'au', label: 'Australia' } ], helper: 'We use this for shipping calculations' } }; const required = { tag: Select, attributes: { label: 'Priority', placeholder: 'Select priority...', options: ['Low', 'Medium', 'High', 'Critical'], color: 'primary', required: true } }; $('#example').content({ tag: div, attributes: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;' }, children: [basic, withLabel, required] });</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: 320 }); </script><code contenteditable="true">await import('/components/data-input/select.js'); const { $ } = Lightview; const demo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;', children: [ { Select: { placeholder: 'Pick a movie franchise', options: ['Star Wars', 'Harry Potter', 'Lord of the Rings', 'Marvel'] } }, { Select: { label: 'Country', placeholder: 'Select your country', options: [ { value: 'us', label: 'United States' }, { value: 'uk', label: 'United Kingdom' }, { value: 'ca', label: 'Canada' }, { value: 'au', label: 'Australia' } ], helper: 'We use this for shipping calculations' } }, { Select: { label: 'Priority', placeholder: 'Select priority...', options: ['Low', 'Medium', 'High', 'Critical'], color: 'primary', required: 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: 320 }); </script><code contenteditable="true" class="language-html">&lt;!-- Import the component (registers lv-select) --&gt; &lt;script type="module" src="/components/data-input/select.js"&gt;&lt;/script&gt; &lt;div style="display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;"&gt; &lt;lv-select placeholder="Pick a movie franchise" options='["Star Wars", "Harry Potter", "Lord of the Rings", "Marvel"]'&gt; &lt;/lv-select&gt; &lt;lv-select label="Country" placeholder="Select your country" options='[{"value":"us","label":"United States"},{"value":"uk","label":"United Kingdom"}]' helper="We'll never share your data."&gt; &lt;/lv-select&gt; &lt;lv-select label="Priority" placeholder="Select priority..." options='["Low", "Medium", "High", "Critical"]' color="primary" required&gt; &lt;/lv-select&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 and live validation</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/data-input/select.js'); const { signal, tags, $ } = Lightview; const { div, p, span, Select } = tags; // Signal for two-way binding const selectedRole = signal(''); // Validation function const validateRole = (val) => { if (!val) return 'Please select a role'; return null; }; // Role descriptions const roleDescriptions = { admin: 'Full access to all features', editor: 'Can edit and publish content', viewer: 'Read-only access', guest: 'Limited access to public content' }; // Reactive select with validation const reactiveDemo = div({ style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;' }, Select({ label: 'User Role', placeholder: 'Assign a role...', value: selectedRole, options: [ { value: 'admin', label: 'Administrator' }, { value: 'editor', label: 'Editor' }, { value: 'viewer', label: 'Viewer' }, { value: 'guest', label: 'Guest' } ], validate: validateRole, required: true }), p({ style: 'font-size: 0.875rem;' }, () => { const role = selectedRole.value; if (!role) return span({ style: 'opacity: 0.7;' }, 'Select a role to see its description'); return span({ style: 'color: oklch(var(--p));' }, roleDescriptions[role]); } ) ); $('#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: 180 }); </script><code contenteditable="true">await import('/components/data-input/select.js'); const { signal, $, tags } = Lightview; const { Select, div, p, span } = tags; const selectedRole = signal(''); const validateRole = (val) => { if (!val) return 'Please select a role'; return null; }; const roleDescriptions = { admin: 'Full access to all features', editor: 'Can edit and publish content', viewer: 'Read-only access', guest: 'Limited access to public content' }; const reactiveDemo = { tag: div, attributes: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;' }, children: [ { tag: Select, attributes: { label: 'User Role', placeholder: 'Assign a role...', value: selectedRole, options: [ { value: 'admin', label: 'Administrator' }, { value: 'editor', label: 'Editor' }, { value: 'viewer', label: 'Viewer' }, { value: 'guest', label: 'Guest' } ], validate: validateRole, required: true } }, { tag: p, attributes: { style: 'font-size: 0.875rem;' }, children: [ () => { const role = selectedRole.value; if (!role) return { tag: span, attributes: { style: 'opacity: 0.7;' }, children: ['Select a role to see its description'] }; return { tag: span, attributes: { style: 'color: oklch(var(--p));' }, children: [roleDescriptions[role]] }; } ] } ] }; $('#example').content(reactiveDemo);</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/data-input/select.js'); const { signal, $ } = Lightview; const selectedRole = signal(''); const validateRole = (val) => { if (!val) return 'Please select a role'; return null; }; const roleDescriptions = { admin: 'Full access to all features', editor: 'Can edit and publish content', viewer: 'Read-only access', guest: 'Limited access to public content' }; const reactiveDemo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem; max-width: 28rem;', children: [ { Select: { label: 'User Role', placeholder: 'Assign a role...', value: selectedRole, options: [ { value: 'admin', label: 'Administrator' }, { value: 'editor', label: 'Editor' }, { value: 'viewer', label: 'Viewer' }, { value: 'guest', label: 'Guest' } ], validate: validateRole, required: true } }, { p: { style: 'font-size: 0.875rem;', children: [ () => { const role = selectedRole.value; if (!role) return { span: { style: 'opacity: 0.7;', children: ['Select a role to see its description'] } }; return { span: { style: 'color: oklch(var(--p));', children: [roleDescriptions[role]] } }; } ] } } ] } }; $('#example').content(reactiveDemo);</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>options</code></td> <td>array</td> <td>[]</td> <td>Array of strings or objects <code>{value, label, disabled}</code></td> </tr> <tr> <td><code>value</code></td> <td>* | signal</td> <td>-</td> <td>Selected value (reactive with signals)</td> </tr> <tr> <td><code>defaultValue</code></td> <td>*</td> <td>''</td> <td>Default value for uncontrolled mode</td> </tr> <tr> <td><code>placeholder</code></td> <td>string</td> <td>-</td> <td>Placeholder option (disabled first option)</td> </tr> <tr> <td><code>label</code></td> <td>string</td> <td>-</td> <td>Label text displayed above select</td> </tr> <tr> <td><code>helper</code></td> <td>string</td> <td>-</td> <td>Helper text displayed below the select</td> </tr> <tr> <td><code>error</code></td> <td>string | function</td> <td>-</td> <td>Error message or reactive error function</td> </tr> <tr> <td><code>validate</code></td> <td>function</td> <td>-</td> <td>Validation function: <code>(value) => errorMessage | null</code></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>ghost</code></td> <td>boolean</td> <td>false</td> <td>Ghost style (no background)</td> </tr> <tr> <td><code>disabled</code></td> <td>boolean</td> <td>false</td> <td>Disable the select</td> </tr> <tr> <td><code>required</code></td> <td>boolean</td> <td>false</td> <td>Mark as required field (shows asterisk)</td> </tr> <tr> <td><code>onChange</code></td> <td>function</td> <td>-</td> <td>Callback when value changes: <code>(value, 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;">Select Gallery</h2> <p class="text-sm" style="opacity: 0.7; margin-bottom: 1.5rem;"> Live examples using <code>&lt;lv-select&gt;</code> custom elements. </p> <script type="module" src="/components/data-input/select.js"></script> <!-- Sizes --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Sizes</h3> <div style="display: flex; flex-direction: column; gap: 1rem; max-width: 20rem; margin-bottom: 2rem;"> <lv-select size="xs" options='["Extra Small"]'></lv-select> <lv-select size="sm" options='["Small"]'></lv-select> <lv-select options='["Medium (default)"]'></lv-select> <lv-select size="lg" options='["Large"]'></lv-select> </div> <!-- Colors --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Colors</h3> <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(13rem, 1fr)); gap: 1rem; margin-bottom: 2rem;"> <lv-select color="primary" options='["Primary"]'></lv-select> <lv-select color="secondary" options='["Secondary"]'></lv-select> <lv-select color="accent" options='["Accent"]'></lv-select> <lv-select color="info" options='["Info"]'></lv-select> <lv-select color="success" options='["Success"]'></lv-select> <lv-select color="warning" options='["Warning"]'></lv-select> <lv-select color="error" options='["Error"]'></lv-select> <lv-select ghost="true" options='["Ghost Style"]'></lv-select> </div> <!-- Labels and Helpers --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Labels & Assistance</h3> <div style="display: flex; flex-direction: column; gap: 1.5rem; margin-bottom: 2rem; max-width: 28rem;"> <lv-select label="Country" placeholder="Select your country" options='["United States", "United Kingdom", "Canada", "Other"]' helper="Used for shipping calculations."> </lv-select> <lv-select label="Required Field" placeholder="Must choose one" options='["Choice A", "Choice B"]' required="true"> </lv-select> <lv-select label="Error State" options='["Option 1", "Option 2"]' error="Selection is required."> </lv-select> </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-select label="Disabled Select" options='["Locked value"]' disabled="true"></lv-select> </div> </div> </div> </div> </div>