forge
Explore
Log in

Don't you dare go Hollow

A long dropdown (8 options or more, such as the audit log's action filter) opens a search field over its options instead of the browser's list: typing filters them, ignoring case and the . _ - separators, arrows and Enter pick one, Escape closes

Browse files at this commit

adf74643c9670e302eaeddd07f6040d82c3a0a16Ahmet Kaan GümüşParent: c3349ae
  • feat
    • a long dropdown (8 options or more, such as the audit log's action filter) opens a search field over its options instead of the browser's list: typing filters them, ignoring case and the . _ - separators, arrows and Enter pick one, Escape closes
    • a printable key on the closed select opens it with that key typed
    • the select stays the source of truth, so the form, auto-submit and the no-JS path work as before
Show full message
✨ feat: a long dropdown (8 options or more, such as the audit log's action filter) opens a search field over its options instead of the browser's list: typing filters them, ignoring case and the . _ - separators, arrows and Enter pick one, Escape closes; a printable key on the closed select opens it with that key typed; the select stays the source of truth, so the form, auto-submit and the no-JS path work as before
crates/forge-web/assets/nav.jsmodified+321 -0
@@ -981,6 +981,327 @@
981 if (c.form.requestSubmit) { c.form.requestSubmit(); } else { c.form.submit(); }981 if (c.form.requestSubmit) { c.form.requestSubmit(); } else { c.form.submit(); }
982 });982 });
983 983
984+ // ---- searchable select ----------------------------------------------------------------------
985+ // A long single-choice select (the audit action filter and the like) opens a search field over
986+ // its options. The select stays the source of truth: the picker only sets selectedIndex and fires
987+ // input and change, so forms, the no-JS path and data-autosubmit behave as before. Document-level
988+ // delegation means a live swap needs no setup.
989+
990+ var SELECT_SEARCH_MIN = 8; // fewer options than this keep the browser's own picker
991+ var SELECT_MIN_WIDTH = 220; // the picker is never narrower than this, in px
992+ var SELECT_ROOM = 260; // less room below the select than this, and more above: the picker goes above
993+ var SELECT_SLOP = 10; // a touch that moves further than this is a scroll, not a tap
994+ var SELECT_LIST_ID = 'select-search-list';
995+
996+ var ssPicker = null; // the picker element, made on first use and reused
997+ var ssInput = null; // its search field
998+ var ssList = null; // its listbox
999+ var ssNone = null; // the no-match line
1000+ var ssSelect = null; // the select the picker is open for
1001+ var ssShowing = false; // whether the picker is open
1002+ var ssRows = []; // one entry per option shown: { li, text, norm, group }
1003+ var ssGroups = []; // one entry per optgroup: { li, shown }
1004+ var ssActiveLi = null; // the highlighted option
1005+ var ssTouch = null; // where the current touch started on a select
1006+
1007+ // The select an event landed on when it can take the search picker, or null.
1008+ function selectTarget(e) {
1009+ var el = e.target && e.target.closest ? e.target.closest('select') : null;
1010+ if (!el || el.multiple || el.size > 1 || el.disabled || el.options.length < SELECT_SEARCH_MIN) { return null; }
1011+ return el;
1012+ }
1013+
1014+ // The option element an event landed on, or null.
1015+ function ssOptionOf(node) {
1016+ return node && node.closest ? node.closest('[role="option"]') : null;
1017+ }
1018+
1019+ // Lower case with the separators removed, so `repo.delete` matches `repo delete` and `user_login`.
1020+ function ssNorm(s) {
1021+ return s.replace(/[_.\- ]+/g, '');
1022+ }
1023+
1024+ // Makes the picker once: a search field over a listbox, appended to the body.
1025+ function ssBuild() {
1026+ ssPicker = document.createElement('div');
1027+ ssPicker.className = 'select-search';
1028+ ssPicker.setAttribute('popover', 'manual');
1029+
1030+ ssInput = document.createElement('input');
1031+ ssInput.type = 'search';
1032+ ssInput.className = 'select-search-input';
1033+ ssInput.setAttribute('autocomplete', 'off');
1034+ ssInput.setAttribute('spellcheck', 'false');
1035+ ssInput.setAttribute('role', 'combobox');
1036+ ssInput.setAttribute('aria-controls', SELECT_LIST_ID);
1037+ ssInput.setAttribute('aria-autocomplete', 'list');
1038+ ssInput.setAttribute('aria-expanded', 'false');
1039+ ssInput.placeholder = document.body.getAttribute('data-select-filter') || '';
1040+
1041+ ssList = document.createElement('ul');
1042+ ssList.className = 'select-search-list';
1043+ ssList.id = SELECT_LIST_ID;
1044+ ssList.setAttribute('role', 'listbox');
1045+
1046+ ssNone = document.createElement('li');
1047+ ssNone.className = 'select-search-none';
1048+ ssNone.setAttribute('role', 'presentation');
1049+ ssNone.textContent = document.body.getAttribute('data-select-none') || '';
1050+ ssNone.hidden = true;
1051+
1052+ ssPicker.append(ssInput, ssList);
1053+ document.body.appendChild(ssPicker);
1054+
1055+ ssInput.addEventListener('input', ssFilter, false);
1056+ ssInput.addEventListener('keydown', ssKey, false);
1057+ ssList.addEventListener('click', function (e) {
1058+ var li = ssOptionOf(e.target);
1059+ if (li) { ssPick(li); }
1060+ }, false);
1061+ ssList.addEventListener('mouseover', function (e) {
1062+ var li = ssOptionOf(e.target);
1063+ if (li && li !== ssActiveLi && li.getAttribute('aria-disabled') !== 'true' && !li.hidden) { ssActivate(li, false); }
1064+ }, false);
1065+ }
1066+
1067+ // Fills the list from the select: an optgroup gets a heading, hidden options are left out.
1068+ function ssRender() {
1069+ ssList.textContent = '';
1070+ ssRows = [];
1071+ ssGroups = [];
1072+ ssActiveLi = null;
1073+
1074+ function addOption(opt, group) {
1075+ if (opt.hidden) { return; }
1076+ var li = document.createElement('li');
1077+ li.id = 'select-search-opt-' + opt.index;
1078+ li.setAttribute('role', 'option');
1079+ li.setAttribute('data-index', String(opt.index));
1080+ li.setAttribute('aria-selected', opt.index === ssSelect.selectedIndex ? 'true' : 'false');
1081+ if (opt.disabled) { li.setAttribute('aria-disabled', 'true'); }
1082+ var text = opt.textContent.trim();
1083+ li.textContent = text;
1084+ ssList.appendChild(li);
1085+ var lower = text.toLocaleLowerCase();
1086+ ssRows.push({ li: li, text: lower, norm: ssNorm(lower), group: group });
1087+ }
1088+
1089+ Array.prototype.forEach.call(ssSelect.children, function (node) {
1090+ if (node.tagName === 'OPTGROUP') {
1091+ var head = document.createElement('li');
1092+ head.className = 'select-search-group';
1093+ head.setAttribute('role', 'presentation');
1094+ head.textContent = node.label;
1095+ ssList.appendChild(head);
1096+ var group = { li: head, shown: 0 };
1097+ ssGroups.push(group);
1098+ Array.prototype.forEach.call(node.children, function (opt) {
1099+ if (opt.tagName === 'OPTION') { addOption(opt, group); }
1100+ });
1101+ } else if (node.tagName === 'OPTION') {
1102+ addOption(node, null);
1103+ }
1104+ });
1105+ ssList.appendChild(ssNone);
1106+ }
1107+
1108+ // Moves the highlight to `li`, scrolling it into view when the keys moved it.
1109+ function ssActivate(li, scroll) {
1110+ if (ssActiveLi) { ssActiveLi.classList.remove('is-active'); }
1111+ ssActiveLi = li;
1112+ if (!li) { ssInput.removeAttribute('aria-activedescendant'); return; }
1113+ li.classList.add('is-active');
1114+ ssInput.setAttribute('aria-activedescendant', li.id);
1115+ if (scroll) { li.scrollIntoView({ block: 'nearest' }); }
1116+ }
1117+
1118+ // Applies the search: a case-insensitive substring match, also matched without the separators.
1119+ // The first enabled option shown becomes the highlight.
1120+ function ssFilter() {
1121+ var q = ssInput.value.trim().toLocaleLowerCase();
1122+ var nq = ssNorm(q);
1123+ var first = null;
1124+ var any = false;
1125+ ssGroups.forEach(function (g) { g.shown = 0; });
1126+ ssRows.forEach(function (row) {
1127+ var show = !q || row.text.indexOf(q) >= 0 || (nq !== '' && row.norm.indexOf(nq) >= 0);
1128+ row.li.hidden = !show;
1129+ if (!show) { return; }
1130+ any = true;
1131+ if (row.group) { row.group.shown += 1; }
1132+ if (!first && row.li.getAttribute('aria-disabled') !== 'true') { first = row.li; }
1133+ });
1134+ ssGroups.forEach(function (g) { g.li.hidden = g.shown === 0; });
1135+ ssNone.hidden = any;
1136+ ssActivate(first, true);
1137+ }
1138+
1139+ // The enabled options on screen, in order: what the arrow keys move through.
1140+ function ssOffered() {
1141+ return ssRows.filter(function (row) {
1142+ return !row.li.hidden && row.li.getAttribute('aria-disabled') !== 'true';
1143+ }).map(function (row) { return row.li; });
1144+ }
1145+
1146+ function ssStep(dir) {
1147+ var list = ssOffered();
1148+ if (!list.length) { return; }
1149+ var at = list.indexOf(ssActiveLi);
1150+ var next = at < 0 ? (dir > 0 ? 0 : list.length - 1) : Math.max(0, Math.min(list.length - 1, at + dir));
1151+ ssActivate(list[next], true);
1152+ }
1153+
1154+ // Keys in the search field. Home and End stay with the text.
1155+ function ssKey(e) {
1156+ if (e.key === 'ArrowDown') { e.preventDefault(); ssStep(1); }
1157+ else if (e.key === 'ArrowUp') { e.preventDefault(); ssStep(-1); }
1158+ else if (e.key === 'Enter') { e.preventDefault(); if (ssActiveLi) { ssPick(ssActiveLi); } }
1159+ else if (e.key === 'Escape') {
1160+ e.preventDefault();
1161+ e.stopPropagation();
1162+ var back = ssSelect;
1163+ ssClose();
1164+ if (back) { back.focus({ preventScroll: true }); }
1165+ }
1166+ else if (e.key === 'Tab') { ssClose(); }
1167+ }
1168+
1169+ // Places the picker under the select, or above it when there is less room below.
1170+ function ssPosition() {
1171+ if (!ssSelect || !ssSelect.isConnected) { ssClose(); return; }
1172+ var r = ssSelect.getBoundingClientRect();
1173+ var below = window.innerHeight - r.bottom;
1174+ var width = Math.max(r.width, SELECT_MIN_WIDTH);
1175+ var left = Math.max(8, Math.min(r.left, window.innerWidth - width - 8));
1176+ ssPicker.style.left = left + 'px';
1177+ ssPicker.style.width = width + 'px';
1178+ if (below < SELECT_ROOM && r.top > below) {
1179+ ssPicker.style.top = 'auto';
1180+ ssPicker.style.bottom = (window.innerHeight - r.top + 4) + 'px';
1181+ } else {
1182+ ssPicker.style.bottom = 'auto';
1183+ ssPicker.style.top = (r.bottom + 4) + 'px';
1184+ }
1185+ }
1186+
1187+ // Opens the picker for `sel`, with `prefill` as the search (a typed character).
1188+ function ssOpen(sel, prefill) {
1189+ if (!ssPicker) { ssBuild(); }
1190+ if (ssShowing && ssSelect === sel) { ssInput.focus({ preventScroll: true }); return; }
1191+ ssSelect = sel;
1192+ ssRender();
1193+ ssInput.value = prefill || '';
1194+ ssFilter();
1195+ ssPosition();
1196+ if (!ssShowing) {
1197+ ssShowing = true;
1198+ if (ssPicker.showPopover) { ssPicker.showPopover(); } else { ssPicker.classList.add('is-open'); }
1199+ }
1200+ ssInput.setAttribute('aria-expanded', 'true');
1201+ ssInput.focus({ preventScroll: true });
1202+ var end = ssInput.value.length;
1203+ ssInput.setSelectionRange(end, end);
1204+ }
1205+
1206+ // Closes the picker and forgets its select. The select keeps its value and focus.
1207+ function ssClose() {
1208+ if (!ssPicker) { return; }
1209+ if (ssShowing) {
1210+ ssShowing = false;
1211+ if (ssPicker.hidePopover) { ssPicker.hidePopover(); } else { ssPicker.classList.remove('is-open'); }
1212+ }
1213+ ssList.textContent = '';
1214+ ssRows = [];
1215+ ssGroups = [];
1216+ ssActiveLi = null;
1217+ ssInput.value = '';
1218+ ssInput.setAttribute('aria-expanded', 'false');
1219+ ssInput.removeAttribute('aria-activedescendant');
1220+ ssSelect = null;
1221+ }
1222+
1223+ // Picks an option: the select takes it and fires input and change, as a native choice does.
1224+ function ssPick(li) {
1225+ if (li.getAttribute('aria-disabled') === 'true' || !ssSelect) { return; }
1226+ var sel = ssSelect;
1227+ var index = parseInt(li.getAttribute('data-index'), 10);
1228+ if (index !== sel.selectedIndex) {
1229+ sel.selectedIndex = index;
1230+ sel.dispatchEvent(new Event('input', { bubbles: true }));
1231+ sel.dispatchEvent(new Event('change', { bubbles: true }));
1232+ }
1233+ ssClose();
1234+ sel.focus({ preventScroll: true });
1235+ }
1236+
1237+ // A mouse press on a select opens the picker (the press is kept from the native one). A press on
1238+ // an option keeps focus in the field. A press anywhere else closes the picker.
1239+ document.addEventListener('mousedown', function (e) {
1240+ var node = e.target && e.target.nodeType === 1 ? e.target : null;
1241+ if (ssShowing && node && ssPicker.contains(node)) {
1242+ if (ssOptionOf(node)) { e.preventDefault(); }
1243+ return;
1244+ }
1245+ var sel = e.button === 0 ? selectTarget(e) : null;
1246+ if (sel) {
1247+ e.preventDefault();
1248+ sel.focus({ preventScroll: true });
1249+ // A second press on the select shuts the picker, as it does the native one.
1250+ if (ssShowing && ssSelect === sel) { ssClose(); } else { ssOpen(sel, ''); }
1251+ return;
1252+ }
1253+ if (ssShowing) { ssClose(); }
1254+ }, false);
1255+
1256+ // Keys on a focused select: Enter, Space, F4 and Alt with an arrow open the picker; a printable
1257+ // character opens it with that character as the search. Other keys keep the browser's own behaviour.
1258+ document.addEventListener('keydown', function (e) {
1259+ var sel = selectTarget(e);
1260+ if (!sel || e.ctrlKey || e.metaKey) { return; }
1261+ var key = e.key;
1262+ if (key === 'Enter' || key === ' ' || key === 'Spacebar' || key === 'F4' || ((key === 'ArrowDown' || key === 'ArrowUp') && e.altKey)) {
1263+ e.preventDefault();
1264+ ssOpen(sel, '');
1265+ } else if (typeof key === 'string' && key.length === 1 && !e.altKey) {
1266+ e.preventDefault();
1267+ ssOpen(sel, key);
1268+ }
1269+ }, false);
1270+
1271+ // Touch: iOS ignores a prevented mousedown, so a tap on a select (a touch that does not move) opens
1272+ // the picker from touchend, which also keeps the native picker from opening.
1273+ document.addEventListener('touchstart', function (e) {
1274+ var sel = selectTarget(e);
1275+ var t = sel && e.touches && e.touches.length === 1 ? e.touches[0] : null;
1276+ ssTouch = t ? { sel: sel, x: t.clientX, y: t.clientY } : null;
1277+ }, { passive: true });
1278+
1279+ document.addEventListener('touchend', function (e) {
1280+ var start = ssTouch;
1281+ ssTouch = null;
1282+ if (!start || !e.changedTouches || !e.changedTouches.length) { return; }
1283+ if (selectTarget(e) !== start.sel) { return; }
1284+ var t = e.changedTouches[0];
1285+ if (Math.abs(t.clientX - start.x) >= SELECT_SLOP || Math.abs(t.clientY - start.y) >= SELECT_SLOP) { return; }
1286+ e.preventDefault();
1287+ start.sel.focus({ preventScroll: true });
1288+ ssOpen(start.sel, '');
1289+ }, { passive: false });
1290+
1291+ // The picker follows its select when the page scrolls or resizes (a phone scrolls the field into
1292+ // view when its keyboard opens). A scroll inside the picker (its list) does not move it.
1293+ function ssFollow(e) {
1294+ if (!ssShowing) { return; }
1295+ var t = e && e.target;
1296+ if (t && t.nodeType === 1 && ssPicker.contains(t)) { return; }
1297+ ssPosition();
1298+ }
1299+ window.addEventListener('resize', ssFollow, false);
1300+ document.addEventListener('scroll', ssFollow, true);
1301+
1302+ // A re-rendered page (a live swap) closes the picker: its select may be gone.
1303+ document.addEventListener('forge:swapped', function () { ssClose(); });
1304+
984 // Appearance page: a colour or theme choice shows on <html> at once, before the form's request returns.1305 // Appearance page: a colour or theme choice shows on <html> at once, before the form's request returns.
985 // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light.1306 // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light.
986 var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' };1307 var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' };
crates/forge-web/src/i18n/link.rsmodified+9 -1
@@ -15,6 +15,10 @@ pub enum K {
15 DraftRestored,15 DraftRestored,
16 /// The button that drops a restored draft.16 /// The button that drops a restored draft.
17 DraftDiscard,17 DraftDiscard,
18+ /// The placeholder of the search field over a long select dropdown.
19+ SelectFilter,
20+ /// The line a select search shows when no option matches.
21+ SelectNone,
18 }22 }
19 23
20 /// The phrase for `key`.24 /// The phrase for `key`.
@@ -33,6 +37,10 @@ pub fn t(lang: Lang, key: K) -> &'static str {
33 (DraftRestored, Lang::Tr) => "Taslak geri yüklendi",37 (DraftRestored, Lang::Tr) => "Taslak geri yüklendi",
34 (DraftDiscard, Lang::En) => "Discard",38 (DraftDiscard, Lang::En) => "Discard",
35 (DraftDiscard, Lang::Tr) => "Vazgeç",39 (DraftDiscard, Lang::Tr) => "Vazgeç",
40+ (SelectFilter, Lang::En) => "Search\u{2026}",
41+ (SelectFilter, Lang::Tr) => "Ara\u{2026}",
42+ (SelectNone, Lang::En) => "No matches",
43+ (SelectNone, Lang::Tr) => "Eşleşme yok",
36 }44 }
37 }45 }
38 46
@@ -43,7 +51,7 @@ mod tests {
43 #[test]51 #[test]
44 fn every_string_has_no_em_dash() {52 fn every_string_has_no_em_dash() {
45 for lang in [Lang::En, Lang::Tr] {53 for lang in [Lang::En, Lang::Tr] {
46- for key in [K::Restarting, K::BackSubmit, K::Saved, K::Done, K::DraftRestored, K::DraftDiscard] {54+ for key in [K::Restarting, K::BackSubmit, K::Saved, K::Done, K::DraftRestored, K::DraftDiscard, K::SelectFilter, K::SelectNone] {
47 assert!(!t(lang, key).contains('\u{2014}'));55 assert!(!t(lang, key).contains('\u{2014}'));
48 }56 }
49 }57 }
crates/forge-web/src/views/mod.rsmodified+1 -1
@@ -664,7 +664,7 @@ pub async fn shell(
664 }664 }
665 <script src=(NAV_SCRIPT_HREF) defer="defer"></script>665 <script src=(NAV_SCRIPT_HREF) defer="defer"></script>
666 </head>666 </head>
667- <body data-link-restarting=(lk::t(lang, LinkKey::Restarting)) data-link-back=(lk::t(lang, LinkKey::BackSubmit)) data-done-saved=(lk::t(lang, LinkKey::Saved)) data-done-ok=(lk::t(lang, LinkKey::Done)) data-draft-restored=(lk::t(lang, LinkKey::DraftRestored)) data-draft-discard=(lk::t(lang, LinkKey::DraftDiscard))>667+ <body data-link-restarting=(lk::t(lang, LinkKey::Restarting)) data-link-back=(lk::t(lang, LinkKey::BackSubmit)) data-done-saved=(lk::t(lang, LinkKey::Saved)) data-done-ok=(lk::t(lang, LinkKey::Done)) data-draft-restored=(lk::t(lang, LinkKey::DraftRestored)) data-draft-discard=(lk::t(lang, LinkKey::DraftDiscard)) data-select-filter=(lk::t(lang, LinkKey::SelectFilter)) data-select-none=(lk::t(lang, LinkKey::SelectNone))>
668 <header class="topbar">668 <header class="topbar">
669 if signed_in {669 if signed_in {
670 <details class="nav-toggle">670 <details class="nav-toggle">
style/_select_search.scssadded+67 -0
@@ -0,0 +1,67 @@
1+// The search picker over a long single-choice select (the audit action filter and the like). It
2+// floats beside the select as a popover, styled as the browser's own select picker in main.scss.
3+// Hidden until open: an author display value would override the browser's rule that hides a closed
4+// popover, so the flex display is only set while it is shown (or the fallback class is on).
5+.select-search {
6+ position: fixed;
7+ margin: 0;
8+ inset: auto;
9+ box-sizing: border-box;
10+ display: none;
11+ flex-direction: column;
12+ gap: 4px;
13+ padding: 4px;
14+ background: var(--surface);
15+ border: 1px solid var(--border);
16+ border-radius: var(--radius-md);
17+ box-shadow: var(--shadow);
18+ color: var(--text);
19+}
20+
21+.select-search:popover-open,
22+.select-search.is-open {
23+ display: flex;
24+}
25+
26+// The field inherits the global input style; it only needs the full width of the picker.
27+.select-search-input {
28+ width: 100%;
29+}
30+
31+.select-search-list {
32+ list-style: none;
33+ margin: 0;
34+ padding: 0;
35+ max-height: 280px;
36+ overflow-y: auto;
37+}
38+
39+.select-search-list li {
40+ padding: 7px 10px;
41+ border-radius: var(--radius-sm);
42+ cursor: pointer;
43+}
44+
45+.select-search-list li.is-active {
46+ background: var(--surface-2);
47+}
48+
49+.select-search-list li[aria-selected="true"] {
50+ background: var(--accent-soft);
51+ color: var(--accent-ink);
52+ font-weight: 600;
53+}
54+
55+.select-search-list li[aria-disabled="true"] {
56+ color: var(--muted);
57+ cursor: default;
58+}
59+
60+// Group headers and the no-match line are not options: small muted text, not selectable.
61+.select-search-list .select-search-group,
62+.select-search-list .select-search-none {
63+ font-size: .8125rem;
64+ color: var(--muted);
65+ padding: 6px 10px;
66+ cursor: default;
67+}
style/main.scssmodified+1 -0
@@ -7849,6 +7849,7 @@ button.ci-job-last {
7849 @import "feedback";7849 @import "feedback";
7850 @import "drafts";7850 @import "drafts";
7851 @import "palette";7851 @import "palette";
7852+@import "select_search";
7852 7853
7853 // A monospace value with its Copy button on the right; long values wrap inside the box.7854 // A monospace value with its Copy button on the right; long values wrap inside the box.
7854 .copy-box {7855 .copy-box {