Dropdowns learn to listen — type, and the list answers
Browse files at this commitd80fd9e2890f1f6f8efae1f26da5da70997bd2caParent: ff05544- feat
- dropdowns learn to listen — type, and the list answers
2 changed files +92 -6
- crates/izlek-web/src/dropdown.rs +66 -6
- style/main.scss +26 -0
crates/izlek-web/src/dropdown.rsmodified+66 -6
| @@ -50,9 +50,26 @@ pub async fn dropdown_script(cx: &Cx) -> Result { | |||
| 50 | panel.style.top = top + 'px';\ | 50 | panel.style.top = top + 'px';\ |
| 51 | panel.style.minWidth = r.width + 'px';\ | 51 | panel.style.minWidth = r.width + 'px';\ |
| 52 | }\ | 52 | }\ |
| 53 | + function visibleRows(panel) { return Array.prototype.slice.call(panel.querySelectorAll('.dd-option:not(.dd-option-hidden)')); }\ | ||
| 53 | function activate(panel, row) {\ | 54 | function activate(panel, row) {\ |
| 54 | panel.querySelectorAll('.dd-option-active').forEach(function (r) { r.classList.remove('dd-option-active'); });\ | 55 | panel.querySelectorAll('.dd-option-active').forEach(function (r) { r.classList.remove('dd-option-active'); });\ |
| 55 | - if (row) { row.classList.add('dd-option-active'); row.focus(); }\ | 56 | + if (!row) { return; }\ |
| 57 | + row.classList.add('dd-option-active');\ | ||
| 58 | + if (panel.__ddSearch) { row.scrollIntoView({ block: 'nearest' }); } else { row.focus(); }\ | ||
| 59 | + }\ | ||
| 60 | + function isTypeKey(e) { return e.key && e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey; }\ | ||
| 61 | + function typeIntoSearch(search, e) {\ | ||
| 62 | + search.focus();\ | ||
| 63 | + search.value += e.key;\ | ||
| 64 | + search.dispatchEvent(new Event('input', { bubbles: true }));\ | ||
| 65 | + }\ | ||
| 66 | + function filterRows(panel, query) {\ | ||
| 67 | + var q = query.toLowerCase();\ | ||
| 68 | + panel.querySelectorAll('.dd-option').forEach(function (r) {\ | ||
| 69 | + r.classList.toggle('dd-option-hidden', q !== '' && r.textContent.toLowerCase().indexOf(q) === -1);\ | ||
| 70 | + });\ | ||
| 71 | + var vis = visibleRows(panel);\ | ||
| 72 | + activate(panel, panel.querySelector('.dd-option-selected:not(.dd-option-hidden)') || vis[0]);\ | ||
| 56 | }\ | 73 | }\ |
| 57 | function pick(select, trigger, panel, row) {\ | 74 | function pick(select, trigger, panel, row) {\ |
| 58 | select.value = row.dataset.value;\ | 75 | select.value = row.dataset.value;\ |
| @@ -68,10 +85,16 @@ pub async fn dropdown_script(cx: &Cx) -> Result { | |||
| 68 | }\ | 85 | }\ |
| 69 | function openPanel(select, trigger, panel) {\ | 86 | function openPanel(select, trigger, panel) {\ |
| 70 | closeAll();\ | 87 | closeAll();\ |
| 88 | + var search = panel.__ddSearch;\ | ||
| 89 | + if (search) {\ | ||
| 90 | + search.value = '';\ | ||
| 91 | + panel.querySelectorAll('.dd-option').forEach(function (r) { r.classList.remove('dd-option-hidden'); });\ | ||
| 92 | + }\ | ||
| 71 | panel.classList.add('dd-open');\ | 93 | panel.classList.add('dd-open');\ |
| 72 | place(panel, trigger);\ | 94 | place(panel, trigger);\ |
| 73 | trigger.setAttribute('aria-expanded', 'true');\ | 95 | trigger.setAttribute('aria-expanded', 'true');\ |
| 74 | activate(panel, panel.querySelector('.dd-option-selected') || panel.querySelector('.dd-option'));\ | 96 | activate(panel, panel.querySelector('.dd-option-selected') || panel.querySelector('.dd-option'));\ |
| 97 | + if (search) { search.focus(); }\ | ||
| 75 | }\ | 98 | }\ |
| 76 | function enhance(select) {\ | 99 | function enhance(select) {\ |
| 77 | if (select.dataset.ddDone) { return; }\ | 100 | if (select.dataset.ddDone) { return; }\ |
| @@ -89,7 +112,16 @@ pub async fn dropdown_script(cx: &Cx) -> Result { | |||
| 89 | panel.className = 'dd-panel';\ | 112 | panel.className = 'dd-panel';\ |
| 90 | panel.setAttribute('role', 'listbox');\ | 113 | panel.setAttribute('role', 'listbox');\ |
| 91 | panel.__ddTrigger = trigger;\ | 114 | panel.__ddTrigger = trigger;\ |
| 92 | - opts(select).forEach(function (opt) {\ | 115 | + var allOpts = opts(select);\ |
| 116 | + var search = null;\ | ||
| 117 | + if (allOpts.length > 7) {\ | ||
| 118 | + search = document.createElement('input');\ | ||
| 119 | + search.type = 'text';\ | ||
| 120 | + search.className = 'dd-search';\ | ||
| 121 | + panel.appendChild(search);\ | ||
| 122 | + panel.__ddSearch = search;\ | ||
| 123 | + }\ | ||
| 124 | + allOpts.forEach(function (opt) {\ | ||
| 93 | var row = document.createElement('button');\ | 125 | var row = document.createElement('button');\ |
| 94 | row.type = 'button';\ | 126 | row.type = 'button';\ |
| 95 | row.className = 'dd-option' + (opt.selected ? ' dd-option-selected' : '');\ | 127 | row.className = 'dd-option' + (opt.selected ? ' dd-option-selected' : '');\ |
| @@ -108,16 +140,40 @@ pub async fn dropdown_script(cx: &Cx) -> Result { | |||
| 108 | if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ | 140 | if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ |
| 109 | e.preventDefault();\ | 141 | e.preventDefault();\ |
| 110 | if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); return; }\ | 142 | if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); return; }\ |
| 111 | - var rows = Array.prototype.slice.call(panel.querySelectorAll('.dd-option'));\ | 143 | + var vis = visibleRows(panel);\ |
| 112 | - var idx = rows.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ | 144 | + var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ |
| 113 | - if (idx >= 0 && idx < rows.length) { activate(panel, rows[idx]); }\ | 145 | + if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ |
| 114 | } else if (e.key === 'Enter' && panel.classList.contains('dd-open')) {\ | 146 | } else if (e.key === 'Enter' && panel.classList.contains('dd-open')) {\ |
| 115 | e.preventDefault();\ | 147 | e.preventDefault();\ |
| 116 | var active = panel.querySelector('.dd-option-active');\ | 148 | var active = panel.querySelector('.dd-option-active');\ |
| 117 | if (active) { pick(select, trigger, panel, active); }\ | 149 | if (active) { pick(select, trigger, panel, active); }\ |
| 150 | + } else if (search && isTypeKey(e)) {\ | ||
| 151 | + e.preventDefault();\ | ||
| 152 | + if (!panel.classList.contains('dd-open')) { openPanel(select, trigger, panel); }\ | ||
| 153 | + typeIntoSearch(search, e);\ | ||
| 154 | + }\ | ||
| 155 | + });\ | ||
| 156 | + if (search) {\ | ||
| 157 | + search.addEventListener('input', function () { filterRows(panel, search.value); });\ | ||
| 158 | + }\ | ||
| 159 | + panel.addEventListener('keydown', function (e) {\ | ||
| 160 | + if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {\ | ||
| 161 | + e.preventDefault();\ | ||
| 162 | + var vis = visibleRows(panel);\ | ||
| 163 | + var idx = vis.indexOf(panel.querySelector('.dd-option-active')) + (e.key === 'ArrowDown' ? 1 : -1);\ | ||
| 164 | + if (idx >= 0 && idx < vis.length) { activate(panel, vis[idx]); }\ | ||
| 165 | + } else if (e.key === 'Enter') {\ | ||
| 166 | + e.preventDefault();\ | ||
| 167 | + var vis = visibleRows(panel);\ | ||
| 168 | + var active = panel.querySelector('.dd-option-active') || (vis.length === 1 ? vis[0] : null);\ | ||
| 169 | + if (active) { pick(select, trigger, panel, active); }\ | ||
| 170 | + } else if (search && e.target !== search && isTypeKey(e)) {\ | ||
| 171 | + e.preventDefault();\ | ||
| 172 | + typeIntoSearch(search, e);\ | ||
| 118 | }\ | 173 | }\ |
| 119 | });\ | 174 | });\ |
| 120 | panel.addEventListener('click', function (e) {\ | 175 | panel.addEventListener('click', function (e) {\ |
| 176 | + e.stopPropagation();\ | ||
| 121 | var row = e.target.closest('.dd-option');\ | 177 | var row = e.target.closest('.dd-option');\ |
| 122 | if (row) { pick(select, trigger, panel, row); }\ | 178 | if (row) { pick(select, trigger, panel, row); }\ |
| 123 | });\ | 179 | });\ |
| @@ -131,7 +187,11 @@ pub async fn dropdown_script(cx: &Cx) -> Result { | |||
| 131 | return true;\ | 187 | return true;\ |
| 132 | });\ | 188 | });\ |
| 133 | document.addEventListener('click', closeAll);\ | 189 | document.addEventListener('click', closeAll);\ |
| 134 | - window.addEventListener('scroll', closeAll, true);\ | 190 | + window.addEventListener('scroll', function (e) {\ |
| 191 | + var t = e.target;\ | ||
| 192 | + if (t && t.nodeType === 1 && t.classList.contains('dd-panel')) { return; }\ | ||
| 193 | + closeAll();\ | ||
| 194 | + }, true);\ | ||
| 135 | function enhanceAll() { document.querySelectorAll('select.status-select, select.field-input').forEach(enhance); }\ | 195 | function enhanceAll() { document.querySelectorAll('select.status-select, select.field-input').forEach(enhance); }\ |
| 136 | enhanceAll();\ | 196 | enhanceAll();\ |
| 137 | document.addEventListener('izlek:wire', enhanceAll);\ | 197 | document.addEventListener('izlek:wire', enhanceAll);\ |
style/main.scssmodified+26 -0
| @@ -2463,6 +2463,25 @@ input[type="radio"] { | |||
| 2463 | 2463 | ||
| 2464 | .dd-option-selected { background: var(--accent-tint); color: var(--accent-ink); } | 2464 | .dd-option-selected { background: var(--accent-tint); color: var(--accent-ink); } |
| 2465 | 2465 | ||
| 2466 | +.dd-option-hidden { display: none; } | ||
| 2467 | + | ||
| 2468 | +// Type-to-filter, injected as the panel's first child on any select with | ||
| 2469 | +// more than 7 options (dropdown.rs `enhance`) — small selects (Newest/ | ||
| 2470 | +// Oldest, yes/no) stay clean. Its own focus-visible override sits after | ||
| 2471 | +// the shared ring rule below (same (0,2,0) trick as `.modal:focus-visible`). | ||
| 2472 | +.dd-search { | ||
| 2473 | + display: block; | ||
| 2474 | + width: 100%; | ||
| 2475 | + margin-bottom: 6px; | ||
| 2476 | + padding: 8px 10px; | ||
| 2477 | + border: none; | ||
| 2478 | + border-bottom: 1px solid var(--border); | ||
| 2479 | + background: none; | ||
| 2480 | + font: inherit; | ||
| 2481 | + font-size: 13px; | ||
| 2482 | + color: var(--ink); | ||
| 2483 | +} | ||
| 2484 | + | ||
| 2466 | // A row that states a fact instead of pretending to be an editable field. An | 2485 | // A row that states a fact instead of pretending to be an editable field. An |
| 2467 | // empty box that silently does nothing is worse than a sentence. | 2486 | // empty box that silently does nothing is worse than a sentence. |
| 2468 | .field-stated { | 2487 | .field-stated { |
| @@ -2524,6 +2543,13 @@ input[type="radio"] { | |||
| 2524 | // the specificity tie against the shared rule above. | 2543 | // the specificity tie against the shared rule above. |
| 2525 | .modal:focus-visible { outline: none; } | 2544 | .modal:focus-visible { outline: none; } |
| 2526 | 2545 | ||
| 2546 | +// A boxed outline inside a panel that's already bordered reads as a double | ||
| 2547 | +// frame; the hairline brightening to the accent color is enough. | ||
| 2548 | +.dd-search:focus-visible { | ||
| 2549 | + outline: none; | ||
| 2550 | + border-bottom-color: var(--accent); | ||
| 2551 | +} | ||
| 2552 | + | ||
| 2527 | // -- the member list -------------------------------------------------------- | 2553 | // -- the member list -------------------------------------------------------- |
| 2528 | 2554 | ||
| 2529 | .member-table { | 2555 | .member-table { |