Skip to content

Commit 99d95b2

Browse files
[Repo Assist] Improve search UX: auto-focus, consistent close, Ctrl+K shortcut (#1084)
* Improve search UX: auto-focus, consistent close, Ctrl+K shortcut - openSearch() focuses the search input immediately after showModal() - closeSearch() resets the input value and clears results on every close path (backdrop click, Escape, or future programmatic close), making behaviour consistent regardless of how the dialog was dismissed - Keyboard shortcut: Ctrl+K / Cmd+K (in addition to '/') now opens search, matching the convention used by GitHub, MDN, and many modern doc sites - The '/' shortcut is now suppressed when the user is typing in an INPUT or TEXTAREA to avoid accidentally opening search mid-sentence - Escape handling moved from keyup to keydown so ev.preventDefault() reliably stops the browser's built-in dialog-close before our custom close logic runs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a1df296 commit 99d95b2

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
### Added
6+
* Search dialog now auto-focuses the search input when opened, clears on close, and can be triggered with `Ctrl+K` / `Cmd+K` in addition to `/`.
67
* Add `dotnet fsdocs convert` command to convert a single `.md`, `.fsx`, or `.ipynb` file to HTML (or another output format) without building a full documentation site. [#811](https://github.com/fsprojects/FSharp.Formatting/issues/811)
78
* `fsdocs convert` now accepts the input file as a positional argument (e.g. `fsdocs convert notebook.ipynb -o notebook.html`). [#1019](https://github.com/fsprojects/FSharp.Formatting/pull/1019)
89
* `fsdocs convert` infers the output format from the output file extension when `--outputformat` is not specified (e.g. `-o out.md` implies `--outputformat markdown`). [#1019](https://github.com/fsprojects/FSharp.Formatting/pull/1019)

docs/content/fsdocs-search.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,23 @@ if (root && searchBtn) {
5151
const resultsElement = document.querySelector("dialog ul");
5252
const searchBox = document.querySelector("dialog input[type=search]");
5353

54-
searchBtn.addEventListener("click", () => {
54+
function openSearch() {
5555
searchDialog.showModal();
56-
})
56+
searchBox.focus();
57+
}
58+
59+
function closeSearch() {
60+
searchBox.value = '';
61+
empty.textContent = "Type something to start searching.";
62+
clearResults();
63+
searchDialog.close();
64+
}
65+
66+
searchBtn.addEventListener("click", openSearch)
5767

5868
searchDialog.addEventListener("click", ev => {
5969
if (ev.target.tagName === "DIALOG") {
60-
searchBox.value = '';
61-
searchDialog.close()
70+
closeSearch();
6271
}
6372
})
6473

@@ -112,13 +121,21 @@ if (root && searchBtn) {
112121
}
113122
});
114123

115-
window.addEventListener('keyup', ev => {
124+
window.addEventListener('keydown', ev => {
116125
if (ev.key === 'Escape' && searchDialog.open) {
117-
searchDialog.close();
126+
ev.preventDefault();
127+
closeSearch();
118128
}
119129

120-
if (ev.key === '/' && !searchDialog.open) {
121-
searchDialog.showModal();
130+
const focusedTag = document.activeElement?.tagName;
131+
const isTypingInInput = focusedTag === 'INPUT' || focusedTag === 'TEXTAREA';
132+
const isShortcut =
133+
(ev.key === '/' && !isTypingInInput) ||
134+
(ev.key === 'k' && (ev.ctrlKey || ev.metaKey));
135+
136+
if (isShortcut && !searchDialog.open) {
137+
ev.preventDefault();
138+
openSearch();
122139
}
123140
})
124141
} else {

0 commit comments

Comments
 (0)