Fix search field
parent
3be4ac0459
commit
3511faa4c6
|
|
@ -23,30 +23,54 @@
|
||||||
const searchElement = document.getElementById('search');
|
const searchElement = document.getElementById('search');
|
||||||
const toggleButton = document.getElementById('toggle-search');
|
const toggleButton = document.getElementById('toggle-search');
|
||||||
|
|
||||||
const ensureSearch = () => {
|
const waitForSearchInput = () => new Promise((resolve) => {
|
||||||
if (searchState.initialized || !searchElement) {
|
const findInput = () => {
|
||||||
|
const input = searchElement.querySelector('input');
|
||||||
|
if (input) {
|
||||||
|
resolve(input);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.requestAnimationFrame(findInput);
|
||||||
|
};
|
||||||
|
|
||||||
|
findInput();
|
||||||
|
});
|
||||||
|
|
||||||
|
const ensureSearch = async () => {
|
||||||
|
if (!searchElement) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (searchState.initialized) {
|
||||||
|
return searchElement.querySelector('input');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (searchState.initializing) {
|
||||||
|
return waitForSearchInput();
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof window.PagefindUI !== 'function') {
|
if (typeof window.PagefindUI !== 'function') {
|
||||||
searchElement.innerHTML = '<p>Search is currently unavailable.</p>';
|
searchElement.innerHTML = '<p>Search is currently unavailable.</p>';
|
||||||
searchState.initialized = true;
|
searchState.initialized = true;
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
searchState.initializing = true;
|
||||||
searchState.instance = new PagefindUI({ element: "#search", showSubResults: false });
|
searchState.instance = new PagefindUI({ element: "#search", showSubResults: false });
|
||||||
searchState.initialized = true;
|
searchState.initialized = true;
|
||||||
|
searchState.initializing = false;
|
||||||
|
return waitForSearchInput();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (toggleButton && searchElement) {
|
if (toggleButton && searchElement) {
|
||||||
toggleButton.addEventListener('click', () => {
|
toggleButton.addEventListener('click', async () => {
|
||||||
const isHidden = searchElement.hasAttribute('hidden');
|
const isHidden = searchElement.hasAttribute('hidden');
|
||||||
|
|
||||||
if (isHidden) {
|
if (isHidden) {
|
||||||
ensureSearch();
|
|
||||||
searchElement.removeAttribute('hidden');
|
searchElement.removeAttribute('hidden');
|
||||||
toggleButton.setAttribute('aria-expanded', 'true');
|
toggleButton.setAttribute('aria-expanded', 'true');
|
||||||
const input = searchElement.querySelector('input');
|
const input = await ensureSearch();
|
||||||
if (input) {
|
if (input) {
|
||||||
input.focus();
|
input.focus();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,17 +30,18 @@
|
||||||
{{ else -}}
|
{{ else -}}
|
||||||
| <a href="{{ .Site.BaseURL }}">DE</a>
|
| <a href="{{ .Site.BaseURL }}">DE</a>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
| <button id="toggle-search" onclick="toggleSearch()" aria-label="Toggle Search">
|
| <button id="toggle-search" type="button" aria-label="Toggle Search" aria-expanded="false" aria-controls="search">
|
||||||
{{ template "feathericon" (dict "UseCDN" .Site.Params.useCDN "Icon" "search") }}
|
{{ template "feathericon" (dict "UseCDN" .Site.Params.useCDN "Icon" "search") }}
|
||||||
</button>
|
</button>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div id="search" style="display: none;"></div>
|
<div id="search" hidden></div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function toggleSearch() {
|
window.codexSearch = {
|
||||||
const searchField = document.getElementById('search');
|
initialized: false,
|
||||||
searchField.style.display = searchField.style.display === 'none' ? 'block' : 'none';
|
initializing: false,
|
||||||
}
|
instance: null
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue