Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gcp/website/frontend3/src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,27 @@ export class ExpandableSearch {
handleDocumentKeydown(e) {
if (e.key === 'Escape' && this.isSearchActive()) {
this.closeSearch();
return;
}

// Press "/" to jump to the search box, unless already typing in a field.
if (e.key === '/' && !e.ctrlKey && !e.metaKey && !e.altKey &&
!this.isTypingContext(e.target)) {
e.preventDefault();
this.openSearch();
}
}

isTypingContext(target) {
if (!target) return false;
if (target.isContentEditable) return true;
// Native form fields, and Material web text fields whose focus retargets to
// the custom element host (e.g. md-filled-text-field, md-textfield-with-*).
const tag = target.tagName ? target.tagName.toLowerCase() : '';
return tag === 'input' || tag === 'textarea' || tag === 'select' ||
tag.includes('textfield') || tag.includes('text-field');
}

initializeSearch() {
const searchContainer = document.querySelector('.search-container-nav');
if (!searchContainer) {
Expand Down