17 lines
715 B
JavaScript
17 lines
715 B
JavaScript
const FOCUSABLE =
|
|
'a, button, input, textarea, select, [role="button"], [role="menuitem"], [tabindex]:not([tabindex="-1"]), [contenteditable="true"]';
|
|
|
|
/**
|
|
* Keep the search field focused when clicking non-interactive chrome.
|
|
*
|
|
* Attach to `mouseup`, not `mousedown`: preventing default on mousedown also
|
|
* blocks text selection. Instead we let the click/drag happen and restore
|
|
* focus afterwards, unless the user just selected some text.
|
|
*/
|
|
export function preserveSearchFocus(e) {
|
|
if (e.target.closest(FOCUSABLE)) return;
|
|
const selection = window.getSelection?.();
|
|
if (selection && !selection.isCollapsed) return;
|
|
document.querySelector('input[type="search"]')?.focus({ preventScroll: true });
|
|
}
|