From d80fd9e2890f1f6f8efae1f26da5da70997bd2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 29 Aug 2026 07:19:50 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(web):=20dropdowns=20learn=20to?= =?UTF-8?q?=20listen=20=E2=80=94=20type,=20and=20the=20list=20answers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/izlek-web/src/dropdown.rs | 72 +++++++++++++++++++++++++++++--- style/main.scss | 26 ++++++++++++ 2 files changed, 92 insertions(+), 6 deletions(-) diff --git a/crates/izlek-web/src/dropdown.rs b/crates/izlek-web/src/dropdown.rs index e67f3d7..a7bd46e 100644 --- a/crates/izlek-web/src/dropdown.rs +++ b/crates/izlek-web/src/dropdown.rs @@ -50,9 +50,26 @@ pub async fn dropdown_script(cx: &Cx) -> Result { panel.style.top = top + 'px';\ panel.style.minWidth = r.width + 'px';\ }\ + function visibleRows(panel) { return Array.prototype.slice.call(panel.querySelectorAll('.dd-option:not(.dd-option-hidden)')); }\ function activate(panel, row) {\ panel.querySelectorAll('.dd-option-active').forEach(function (r) { r.classList.remove('dd-option-active'); });\ - if (row) { row.classList.add('dd-option-active'); row.focus(); }\ + if (!row) { return; }\ + row.classList.add('dd-option-active');\ + if (panel.__ddSearch) { row.scrollIntoView({ block: 'nearest' }); } else { row.focus(); }\ + }\ + function isTypeKey(e) { return e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey; }\ + function typeIntoSearch(search, e) {\ + search.focus();\ + search.value += e.key;\ + search.dispatchEvent(new Event('input', { bubbles: true }));\ + }\ + function filterRows(panel, query) {\ + var q = query.toLowerCase();\ + panel.querySelectorAll('.dd-option').forEach(function (r) {\ + r.classList.toggle('dd-option-hidden', q !== '' && r.textContent.toLowerCase().indexOf(q) === -1);\ + });\ + var vis = visibleRows(panel);\ + activate(panel, panel.querySelector('.dd-option-selected:not(.dd-option-hidden)') || vis[0]);\ }\ function pick(select, trigger, panel, row) {\ select.value = row.dataset.value;\ @@ -68,10 +85,16 @@ pub async fn dropdown_script(cx: &Cx) -> Result { }\ function openPanel(select, trigger, panel) {\ closeAll();\ + var search = panel.__ddSearch;\ + if (search) {\ + search.value = '';\ + panel.querySelectorAll('.dd-option').forEach(function (r) { r.classList.remove('dd-option-hidden'); });\ + }\ panel.classList.add('dd-open');\ place(panel, trigger);\ trigger.setAttribute('aria-expanded', 'true');\ activate(panel, panel.querySelector('.dd-option-selected') || panel.querySelector('.dd-option'));\ + if (search) { search.focus(); }\ }\ function enhance(select) {\ if (select.dataset.ddDone) { return; }\ @@ -89,7 +112,16 @@ pub async fn dropdown_script(cx: &Cx) -> Result { panel.className = 'dd-panel';\ panel.setAttribute('role', 'listbox');\ panel.__ddTrigger = trigger;\ - opts(select).forEach(function (opt) {\ + var allOpts = opts(select);\ + var search = null;\ + if (allOpts.length > 7) {\ + search = document.createElement('input');\ + search.type = 'text';\ + search.className = 'dd-search';\ + panel.appendChild(search);\ + panel.__ddSearch = search;\ + }\ + allOpts.forEach(function (opt) {\ var row = document.createElement('button');\ row.type = 'button';\ row.className = 'dd-option' + (opt.selected ? ' dd-option-selected' : '');\ @@ -108,16 +140,40 @@ pub async fn dropdown_script(cx: &Cx) -> Result { if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ e.preventDefault();\ if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); return; }\ - var rows = Array.prototype.slice.call(panel.querySelectorAll('.dd-option'));\ - var idx = rows.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ - if (idx >= 0 && idx < rows.length) { activate(panel, rows[idx]); }\ + var vis = visibleRows(panel);\ + var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ + if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ } else if (e.key === 'Enter' && panel.classList.contains('dd-open')) {\ e.preventDefault();\ var active = panel.querySelector('.dd-option-active');\ if (active) { pick(select, trigger, panel, active); }\ + } else if (search && isTypeKey(e)) {\ + e.preventDefault();\ + if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); }\ + typeIntoSearch(search, e);\ + }\ + });\ + if (search) {\ + search.addEventListener('input', function () { filterRows(panel, search.value); });\ + }\ + panel.addEventListener('keydown', function (e) {\ + if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ + e.preventDefault();\ + var vis = visibleRows(panel);\ + var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ + if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ + } else if (e.key === 'Enter') {\ + e.preventDefault();\ + var vis = visibleRows(panel);\ + var active = panel.querySelector('.dd-option-active') || (vis.length === 1 ? vis[0] : null);\ + if (active) { pick(select, trigger, panel, active); }\ + } else if (search && e.target !== search && isTypeKey(e)) {\ + e.preventDefault();\ + typeIntoSearch(search, e);\ }\ });\ panel.addEventListener('click', function (e) {\ + e.stopPropagation();\ var row = e.target.closest('.dd-option');\ if (row) { pick(select, trigger, panel, row); }\ });\ @@ -131,7 +187,11 @@ pub async fn dropdown_script(cx: &Cx) -> Result { return true;\ });\ document.addEventListener('click', closeAll);\ - window.addEventListener('scroll', closeAll, true);\ + window.addEventListener('scroll', function (e) {\ + var t = e.target;\ + if (t && t.nodeType === 1 && t.classList.contains('dd-panel')) { return; }\ + closeAll();\ + }, true);\ function enhanceAll() { document.querySelectorAll('select.status-select, select.field-input').forEach(enhance); }\ enhanceAll();\ document.addEventListener('izlek:wire', enhanceAll);\ diff --git a/style/main.scss b/style/main.scss index 21322d9..1248e24 100644 --- a/style/main.scss +++ b/style/main.scss @@ -2463,6 +2463,25 @@ input[type="radio"] { .dd-option-selected { background: var(--accent-tint); color: var(--accent-ink); } +.dd-option-hidden { display: none; } + +// Type-to-filter, injected as the panel's first child on any select with +// more than 7 options (dropdown.rs `enhance`) — small selects (Newest/ +// Oldest, yes/no) stay clean. Its own focus-visible override sits after +// the shared ring rule below (same (0,2,0) trick as `.modal:focus-visible`). +.dd-search { + display: block; + width: 100%; + margin-bottom: 6px; + padding: 8px 10px; + border: none; + border-bottom: 1px solid var(--border); + background: none; + font: inherit; + font-size: 13px; + color: var(--ink); +} + // A row that states a fact instead of pretending to be an editable field. An // empty box that silently does nothing is worse than a sentence. .field-stated { @@ -2524,6 +2543,13 @@ input[type="radio"] { // the specificity tie against the shared rule above. .modal:focus-visible { outline: none; } +// A boxed outline inside a panel that's already bordered reads as a double +// frame; the hairline brightening to the accent color is enough. +.dd-search:focus-visible { + outline: none; + border-bottom-color: var(--accent); +} + // -- the member list -------------------------------------------------------- .member-table {