UNPKG

lightview

Version:

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

660 lines (612 loc) 32.4 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: 'Input' } ] }); 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">Input</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;"> Text input allows users to enter and edit text. Supports labels, validation, helper text, and reactive state binding. </p> <!-- Basic Examples --> <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 inputs 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/input.js'); const { tags, $ } = Lightview; const { div, Input } = tags; // 1. Basic input const basic = Input({ placeholder: 'Basic input' }); // 2. Input with label and helper const withLabel = Input({ label: 'Email Address', type: 'email', placeholder: 'you@example.com', helper: 'We will never share your email.' }); // 3. Input with color const withColor = Input({ label: 'Primary Color', placeholder: 'Styled input', color: 'primary' }); // 4. Required input const requiredInput = Input({ label: 'Username', placeholder: 'Enter username', required: true }); // Insert all examples $('#example').content( div({ style: 'display: flex; flex-direction: column; gap: 1rem;' }, basic, withLabel, withColor, requiredInput) );</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/input.js'); const { $, tags } = Lightview; const { Input, div } = tags; const basic = { tag: Input, attributes: { placeholder: 'Basic input' } }; const withLabel = { tag: Input, attributes: { label: 'Email Address', type: 'email', placeholder: 'you@example.com', helper: 'We will never share your email.' } }; const withColor = { tag: Input, attributes: { label: 'Primary Color', placeholder: 'Styled input', color: 'primary' } }; const requiredInput = { tag: Input, attributes: { label: 'Username', placeholder: 'Enter username', required: true } }; $('#example').content({ tag: div, attributes: { style: 'display: flex; flex-direction: column; gap: 1rem;' }, children: [basic, withLabel, withColor, requiredInput] });</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/input.js'); const { $ } = Lightview; const demo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem;', children: [ { Input: { placeholder: 'Basic input' } }, { Input: { label: 'Email Address', type: 'email', placeholder: 'you@example.com', helper: 'We will never share your email.' } }, { Input: { label: 'Primary Color', placeholder: 'Styled input', color: 'primary' } }, { Input: { label: 'Username', placeholder: 'Enter username', 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-input) --&gt; &lt;script type="module" src="/components/data-input/input.js"&gt;&lt;/script&gt; &lt;div style="display: flex; flex-direction: column; gap: 1rem;"&gt; &lt;lv-input placeholder="Basic input"&gt;&lt;/lv-input&gt; &lt;lv-input label="Email Address" type="email" placeholder="you@example.com" helper="We'll never share your email."&gt;&lt;/lv-input&gt; &lt;lv-input label="Primary Color" placeholder="Styled input" color="primary"&gt;&lt;/lv-input&gt; &lt;lv-input label="Username" placeholder="Enter username" required&gt;&lt;/lv-input&gt; &lt;/div&gt;</code></pre> </div> </div> </div> <!-- Reactive Example --> <div style="background-color: oklch(var(--b2)); border-radius: var(--rounded-box, 1rem); margin-bottom: 2rem;"> <div style="padding: 1.5rem;"> <h2 style="font-size: 1.5rem; font-weight: 600; line-height: 2rem;">Reactive Example</h2> <p style="font-size: 0.875rem; 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: 220 }); </script><code contenteditable="true">await import('/components/data-input/input.js'); const { signal, tags, $ } = Lightview; const { div, p, span, Input } = tags; // Signal for two-way binding const username = signal(''); // Validation function const validateUsername = (val) => { if (!val) return 'Username is required'; if (val.length < 3) return 'Username must be at least 3 characters'; if (!/^[a-zA-Z0-9_]+$/.test(val)) return 'Only letters, numbers, and underscores allowed'; return null; }; // Reactive input with validation const reactiveDemo = div({ style: 'display: flex; flex-direction: column; gap: 1rem;' }, Input({ label: 'Username', placeholder: 'Enter your username...', value: username, validate: validateUsername, required: true }), p({ style: 'font-size: 0.875rem;' }, span({ style: 'opacity: 0.7;' }, 'Current value: '), span({ style: 'font-family: monospace; color: oklch(var(--p));' }, () => username.value || '(empty)') ), p({ style: 'font-size: 0.875rem;' }, () => { const error = validateUsername(username.value); return error ? span({ style: 'color: oklch(var(--er));' }, '✗ ' + error) : span({ style: 'color: oklch(var(--su));' }, '✓ Username is valid!'); } ) ); $('#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: 220 }); </script><code contenteditable="true">await import('/components/data-input/input.js'); const { signal, $, tags } = Lightview; const { Input, div, p, span } = tags; const username = signal(''); const validateUsername = (val) => { if (!val) return 'Username is required'; if (val.length < 3) return 'Username must be at least 3 characters'; if (!/^[a-zA-Z0-9_]+$/.test(val)) return 'Only letters, numbers, and underscores allowed'; return null; }; const reactiveDemo = { tag: div, attributes: { style: 'display: flex; flex-direction: column; gap: 1rem;' }, children: [ { tag: Input, attributes: { label: 'Username', placeholder: 'Enter your username...', value: username, validate: validateUsername, required: true } }, { tag: p, attributes: { style: 'font-size: 0.875rem;' }, children: [ { tag: span, attributes: { style: 'opacity: 0.7;' }, children: ['Current value: '] }, { tag: span, attributes: { style: 'font-family: monospace; color: oklch(var(--p));' }, children: [() => username.value || '(empty)'] } ] }, { tag: p, attributes: { style: 'font-size: 0.875rem;' }, children: [ () => { const error = validateUsername(username.value); return error ? { tag: span, attributes: { style: 'color: oklch(var(--er));' }, children: ['✗ ' + error] } : { tag: span, attributes: { style: 'color: oklch(var(--su));' }, children: ['✓ Username is valid!'] }; } ] } ] }; $('#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: 220 }); </script><code contenteditable="true">await import('/components/data-input/input.js'); const { signal, tags, $ } = Lightview; const { Input, div, p, span } = tags; const username = signal(''); const validateUsername = (val) => { if (!val) return 'Username is required'; if (val.length < 3) return 'Username must be at least 3 characters'; if (!/^[a-zA-Z0-9_]+$/.test(val)) return 'Only letters, numbers, and underscores allowed'; return null; }; const reactiveDemo = { div: { style: 'display: flex; flex-direction: column; gap: 1rem;', children: [ { Input: { label: 'Username', placeholder: 'Enter your username...', value: username, validate: validateUsername, required: true } }, { p: { style: 'font-size: 0.875rem;', children: [ { span: { style: 'opacity: 0.7;', children: ['Current value: '] } }, { span: { style: 'font-family: monospace; color: oklch(var(--p));', children: [() => username.value || '(empty)'] } } ] } }, { p: { style: 'font-size: 0.875rem;', children: [ () => { const error = validateUsername(username.value); return error ? { span: { style: 'color: oklch(var(--er));', children: ['✗ ' + error] } } : { span: { style: 'color: oklch(var(--su));', children: ['✓ Username is valid!'] } }; } ] } } ] } }; $('#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>type</code></td> <td>string</td> <td>'text'</td> <td>Input type (text, password, email, number, etc.)</td> </tr> <tr> <td><code>value</code></td> <td>string | signal</td> <td>-</td> <td>Input value (reactive with signals)</td> </tr> <tr> <td><code>defaultValue</code></td> <td>string</td> <td>''</td> <td>Default value for uncontrolled mode</td> </tr> <tr> <td><code>placeholder</code></td> <td>string</td> <td>-</td> <td>Placeholder text</td> </tr> <tr> <td><code>label</code></td> <td>string</td> <td>-</td> <td>Label text displayed above input</td> </tr> <tr> <td><code>helper</code></td> <td>string</td> <td>-</td> <td>Helper text displayed below the input</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 input</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>onBlur</code></td> <td>function</td> <td>-</td> <td>Callback when input loses focus</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;">Input Gallery</h2> <p class="text-sm" style="opacity: 0.7; margin-bottom: 1.5rem;"> Live examples using <code>&lt;lv-input&gt;</code> custom elements. </p> <script type="module" src="/components/data-input/input.js"></script> <!-- Sizes --> <h3 class="text-lg font-semibold" style="margin-bottom: 0.75rem;">Sizes</h3> <div style="display: flex; align-items: flex-end; flex-wrap: wrap; gap: 1rem; margin-bottom: 2rem;"> <lv-input size="xs" placeholder="Extra small"></lv-input> <lv-input size="sm" placeholder="Small"></lv-input> <lv-input placeholder="Medium (default)"></lv-input> <lv-input size="lg" placeholder="Large"></lv-input> </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-input color="primary" placeholder="Primary"></lv-input> <lv-input color="secondary" placeholder="Secondary"></lv-input> <lv-input color="accent" placeholder="Accent"></lv-input> <lv-input color="info" placeholder="Info"></lv-input> <lv-input color="success" placeholder="Success"></lv-input> <lv-input color="warning" placeholder="Warning"></lv-input> <lv-input color="error" placeholder="Error"></lv-input> <lv-input ghost="true" placeholder="Ghost Style"></lv-input> </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-input label="Email Address" type="email" placeholder="you@example.com" helper="We'll never share your email address."> </lv-input> <lv-input label="Password" type="password" placeholder="Enter your password" required="true"> </lv-input> <lv-input label="Error State" placeholder="Invalid input" error="This field has an error message."> </lv-input> </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-input label="Disabled Input" value="Cannot edit this" disabled="true"></lv-input> <lv-input label="Read Only (via value)" value="Static text" disabled="true"></lv-input> </div> </div> </div> </div> </div>