Improve search dialog keyboard access
This commit is contained in:
1 parent
19f1f4c458
commit
7669cd1390
1 file changed
+34
-17
@@ -47,6 +47,9 @@
|
||||
let searchInstance = null;
|
||||
let isInitialized = false;
|
||||
let isSearchOpen = false;
|
||||
let previousFocus = null;
|
||||
let previousOverflow = '';
|
||||
let closeTimer = null;
|
||||
let initRetryCount = 0;
|
||||
const MAX_INIT_RETRIES = CONFIG.ui.maxInitRetries || 30; // 最多重试30次 (3秒)
|
||||
|
||||
@@ -73,12 +76,12 @@
|
||||
// ============================================================================
|
||||
const searchHTML = `
|
||||
<div id="typesense-search-mask" class="ts-mask" style="display:none;">
|
||||
<div id="typesense-search-container" class="ts-container">
|
||||
<div id="typesense-search-container" class="ts-container" role="dialog" aria-modal="true" aria-labelledby="typesense-search-title" tabindex="-1">
|
||||
<div class="ts-header">
|
||||
<span class="ts-title">
|
||||
<span class="ts-title" id="typesense-search-title">
|
||||
<i class="fas fa-search"></i> 本站搜索
|
||||
</span>
|
||||
<span id="close-typesense" class="ts-close" aria-label="关闭搜索">×</span>
|
||||
<button type="button" id="close-typesense" class="ts-close" aria-label="关闭搜索">×</button>
|
||||
</div>
|
||||
<div id="searchbox"></div>
|
||||
<div id="stats" class="ts-stats"></div>
|
||||
@@ -138,6 +141,9 @@
|
||||
}
|
||||
.ts-close {
|
||||
cursor: pointer;
|
||||
border: 0;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
font-size: 28px;
|
||||
color: var(--font-color, #333);
|
||||
line-height: 1;
|
||||
@@ -313,11 +319,15 @@
|
||||
// ============================================================================
|
||||
function openSearch() {
|
||||
if (isSearchOpen) return;
|
||||
if (closeTimer) clearTimeout(closeTimer);
|
||||
previousFocus = document.activeElement;
|
||||
previousOverflow = document.body.style.overflow;
|
||||
isSearchOpen = true;
|
||||
mask.style.display = 'block';
|
||||
void mask.offsetWidth;
|
||||
mask.classList.add('active');
|
||||
document.body.style.overflow = 'hidden';
|
||||
container.focus();
|
||||
|
||||
if (!isInitialized) {
|
||||
loadDependencies().then(() => {
|
||||
@@ -335,19 +345,23 @@
|
||||
if (!isSearchOpen) return;
|
||||
isSearchOpen = false;
|
||||
mask.classList.remove('active');
|
||||
setTimeout(function() {
|
||||
document.body.style.overflow = previousOverflow;
|
||||
if (previousFocus instanceof HTMLElement) previousFocus.focus();
|
||||
closeTimer = setTimeout(function() {
|
||||
if (isSearchOpen) return;
|
||||
mask.style.display = 'none';
|
||||
document.body.style.overflow = '';
|
||||
closeTimer = null;
|
||||
}, CONFIG.ui.animationDuration || 300);
|
||||
}
|
||||
|
||||
function focusSearchInput(retryCount) {
|
||||
retryCount = retryCount || 0;
|
||||
const input = document.querySelector('.ais-SearchBox-input');
|
||||
if (input) {
|
||||
if (!isSearchOpen) return;
|
||||
if (input && (document.activeElement === container || document.activeElement === input)) {
|
||||
input.focus();
|
||||
input.select();
|
||||
} else if (retryCount < (CONFIG.ui.maxRetries || 10)) {
|
||||
} else if (!input && retryCount < (CONFIG.ui.maxRetries || 10)) {
|
||||
setTimeout(function() {
|
||||
focusSearchInput(retryCount + 1);
|
||||
}, CONFIG.ui.retryDelay || 100);
|
||||
@@ -379,6 +393,18 @@
|
||||
e.preventDefault();
|
||||
openSearch();
|
||||
}
|
||||
if (e.key === 'Tab' && isSearchOpen) {
|
||||
const focusable = [...container.querySelectorAll('a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])')]
|
||||
.filter((element) => element.getClientRects().length);
|
||||
if (!focusable.length) { e.preventDefault(); container.focus(); return; }
|
||||
const first = focusable[0];
|
||||
const last = focusable[focusable.length - 1];
|
||||
if (e.shiftKey && (document.activeElement === first || document.activeElement === container)) {
|
||||
e.preventDefault(); last.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === last) {
|
||||
e.preventDefault(); first.focus();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
@@ -456,7 +482,7 @@
|
||||
instantsearch.widgets.searchBox({
|
||||
container: '#searchbox',
|
||||
placeholder: CONFIG.ui.placeholder || '输入关键词寻找故事...',
|
||||
autofocus: true,
|
||||
autofocus: false,
|
||||
showReset: true,
|
||||
showSubmit: false,
|
||||
showLoadingIndicator: true
|
||||
@@ -517,15 +543,6 @@
|
||||
|
||||
console.log('✅ Typesense 初始化成功!');
|
||||
|
||||
searchInstance.on('render', function() {
|
||||
if (isSearchOpen) {
|
||||
const input = document.querySelector('.ais-SearchBox-input');
|
||||
if (input && document.activeElement !== input) {
|
||||
input.focus();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 初始化失败:', error);
|
||||
showErrorMessage();
|
||||
|
||||
Reference in new issue
Block a user