Skip to content

Commit 06b596d

Browse files
authored
docs: Restore the carrot to the position where the search input was lost (#19459)
1 parent ecd0ede commit 06b596d

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

docs/src/assets/js/search.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const searchClearBtn = document.querySelector('#search__clear-btn');
2525
const poweredByLink = document.querySelector('.search_powered-by-wrapper');
2626
let activeIndex = -1;
2727
let searchQuery;
28+
let caretPosition = 0;
2829

2930
//-----------------------------------------------------------------------------
3031
// Helpers
@@ -202,25 +203,36 @@ if (searchInput)
202203
});
203204

204205

205-
if (searchClearBtn)
206-
searchClearBtn.addEventListener('click', function (e) {
206+
if (searchClearBtn) {
207+
searchClearBtn.addEventListener('click', function () {
207208
searchInput.value = '';
208209
searchInput.focus();
209210
clearSearchResults(true);
210211
searchClearBtn.setAttribute('hidden', '');
211212
});
212213

214+
searchInput.addEventListener("blur", function () {
215+
caretPosition = searchInput.selectionStart;
216+
});
217+
218+
searchInput.addEventListener("focus", function () {
219+
if (searchInput.selectionStart !== caretPosition) {
220+
searchInput.setSelectionRange(caretPosition, caretPosition);
221+
}
222+
});
223+
224+
}
225+
213226
if (poweredByLink) {
214227
poweredByLink.addEventListener('focus', function () {
215-
clearSearchResults();
228+
clearSearchResults();
216229
});
217230
}
218231

219232
if (resultsElement) {
220233
resultsElement.addEventListener('keydown', (e) => {
221234
if (e.key !== "ArrowUp" && e.key !== "ArrowDown" && e.key !== "Tab" && e.key !== 'Shift') {
222235
searchInput.focus();
223-
searchInput.setSelectionRange(searchInput.value.length, searchInput.value.length);
224236
}
225237
});
226238
}

0 commit comments

Comments
 (0)