Skip to content

Commit bc0b7b1

Browse files
feat: activate search input from keyboard (google#5569)
Refs: google#1802 Signed-off-by: Mike Fiedler <miketheman@gmail.com> Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>
1 parent 6117ff7 commit bc0b7b1

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

gcp/website/frontend3/src/search.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,27 @@ export class ExpandableSearch {
5858
handleDocumentKeydown(e) {
5959
if (e.key === 'Escape' && this.isSearchActive()) {
6060
this.closeSearch();
61+
return;
62+
}
63+
64+
// Press "/" to jump to the search box, unless already typing in a field.
65+
if (e.key === '/' && !e.ctrlKey && !e.metaKey && !e.altKey &&
66+
!this.isTypingContext(e.target)) {
67+
e.preventDefault();
68+
this.openSearch();
6169
}
6270
}
6371

72+
isTypingContext(target) {
73+
if (!target) return false;
74+
if (target.isContentEditable) return true;
75+
// Native form fields, and Material web text fields whose focus retargets to
76+
// the custom element host (e.g. md-filled-text-field, md-textfield-with-*).
77+
const tag = target.tagName ? target.tagName.toLowerCase() : '';
78+
return tag === 'input' || tag === 'textarea' || tag === 'select' ||
79+
tag.includes('textfield') || tag.includes('text-field');
80+
}
81+
6482
initializeSearch() {
6583
const searchContainer = document.querySelector('.search-container-nav');
6684
if (!searchContainer) {

0 commit comments

Comments
 (0)