From a139a26c2148a62a5151f6c60f27af2e20fad99d Mon Sep 17 00:00:00 2001 From: root Date: Sat, 28 Feb 2026 00:37:58 +0530 Subject: [PATCH] refactor: consolidate Algolia search logic into algolia-search.js --- _includes/algolia.html | 171 +---------------------------------------- js/algolia-search.js | 35 ++++++++- 2 files changed, 35 insertions(+), 171 deletions(-) diff --git a/_includes/algolia.html b/_includes/algolia.html index a63c375eb37..8eb775de00b 100644 --- a/_includes/algolia.html +++ b/_includes/algolia.html @@ -1,170 +1 @@ - \ No newline at end of file + diff --git a/js/algolia-search.js b/js/algolia-search.js index 3528c6730f2..0c90a352a55 100644 --- a/js/algolia-search.js +++ b/js/algolia-search.js @@ -45,7 +45,7 @@ const hitTemplate = function(hit) { algoliaSearch.addWidget( instantsearch.widgets.searchBox({ container: '#search-input', - placeholder: 'Search ...', + placeholder: 'Search (' + (/Mac|iPhone|iPad/.test(navigator.platform) ? '⌘K' : 'Ctrl+K') + ')', poweredBy: true, // This is required if you're on the free Community plan magnifier: false, autofocus: false, @@ -133,3 +133,36 @@ document.addEventListener('mouseup', function(e) { } }); +// Cmd+K / Ctrl+K keyboard shortcut to focus search, Escape to dismiss +document.addEventListener('keydown', function(e) { + var isMac = /Mac|iPhone|iPad|iPod/.test(navigator.platform); + if ((isMac ? e.metaKey : e.ctrlKey) && e.key === 'k') { + e.preventDefault(); + var input = document.querySelector('.ais-search-box--input'); + if (input) { + input.focus(); + input.select(); + } + } + + if (e.key === 'Escape' || e.keyCode === 27) { + var results = document.getElementById('search-results'); + var inputContainer = document.getElementById('search-input'); + var activeElement = document.activeElement; + + if (results) { + results.style.display = 'none'; + } + + if (inputContainer && inputContainer.contains(activeElement)) { + activeElement.blur(); + } else if (results && results.contains(activeElement)) { + var algoliaInput = document.querySelector('#search-input .ais-search-box--input'); + if (algoliaInput) { + algoliaInput.focus(); + } else { + activeElement.blur(); + } + } + } +});