Skip to content

Commit 3c09ee8

Browse files
authored
Merge pull request #20350 from llamafilm/17824-hotkeys
add global search hotkey
2 parents a4f0b76 + f2097cc commit 3c09ee8

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

netbox/project-static/dist/netbox.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netbox/project-static/dist/netbox.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const HOTKEYS: Record<string, () => void> = {
2+
'/': focusGlobalSearch,
3+
};
4+
5+
function focusGlobalSearch(): void {
6+
const searchInput = document.querySelector<HTMLInputElement>('header input[name="q"]')!;
7+
if (searchInput) {
8+
searchInput.focus();
9+
}
10+
}
11+
12+
function handleKeydown(event: KeyboardEvent): void {
13+
// Ignore hotkeys when focused on form elements or when modal is open
14+
if ((event.target as Element).matches('input, textarea, select') || document.body.classList.contains('modal-open')) {
15+
return;
16+
}
17+
18+
const handler = HOTKEYS[event.key];
19+
if (!handler) {
20+
return;
21+
}
22+
23+
event.preventDefault();
24+
handler();
25+
}
26+
27+
export function initHotkeys(): void {
28+
document.addEventListener('keydown', handleKeydown);
29+
}

netbox/project-static/src/netbox.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { initDashboard } from './dashboard';
1414
import { initRackElevation } from './racks';
1515
import { initHtmx } from './htmx';
1616
import { initSavedFilterSelect } from './forms/savedFiltersSelect';
17+
import { initHotkeys } from './hotkeys';
1718

1819
function initDocument(): void {
1920
for (const init of [
@@ -33,6 +34,7 @@ function initDocument(): void {
3334
initRackElevation,
3435
initHtmx,
3536
initSavedFilterSelect,
37+
initHotkeys,
3638
]) {
3739
init();
3840
}

0 commit comments

Comments
 (0)