Skip to content

Commit

Permalink
fix: remove multiple active results and keep only one
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed May 5, 2023
1 parent 20384da commit 1dd929d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 6 additions & 6 deletions assets/search/js/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class Navigator {

private current(): null | HTMLElement {
const modal = this.modal()
return modal ? modal.querySelector('.search-result.active') : document.querySelector('.search-container .search-result.active')
return modal ?
modal.querySelector('.search-result[aria-selected="true"]') :
document.querySelector('.search-container .search-result[aria-selected="true"]')
}

private go(dir) {
const current = this.current()
let target
if (!current) {
target = current
} else {
if (current) {
current.ariaSelected = 'false'
target = dir === 'prev' ? current.previousElementSibling : current.nextElementSibling
}
target = target ?? this.first()

current?.classList.remove('active')
target.focus()
target.classList.add('active')
target.ariaSelected = 'true'
}

private first() {
Expand Down
17 changes: 16 additions & 1 deletion assets/search/js/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export default class Renderer {
this.toggleMeta(node.querySelector('.search-result-meta'))
e.preventDefault()
})

node.addEventListener('mousemove', () => {
this.activeResult(node)
})
}
}
}
Expand All @@ -195,6 +199,17 @@ export default class Renderer {
observer.observe(container, { childList: true });
}

activeResult(target) {
if (target.ariaSelected === 'true') {
return
}

this.getContainer().querySelectorAll('.search-result').forEach((result) => {
result.ariaSelected = 'false'
});
target.ariaSelected = 'true'
}

private toggleMeta(meta: HTMLElement) {
if (meta.classList.contains('show')) {
meta.classList.remove('show')
Expand Down Expand Up @@ -251,7 +266,7 @@ export default class Renderer {
let results = ''
for (let i = min; i < this.results.length && i < max; i++) {
const result = this.results[i]
results += `<a title="${result.item.title}" href="${result.item.url}" class="search-result">
results += `<a title="${result.item.title}" href="${result.item.url}" class="search-result" aria-selected="false">
<div class="search-result-icon">${this.icon(result.item)}</div>
<div class="search-result-content">
<div class="search-result-title">${this.title(result)}</div>
Expand Down
3 changes: 1 addition & 2 deletions assets/search/scss/_result.scss
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@
margin-top: 0;
}

&:hover,
&.active {
&[aria-selected="true"] {
background: var(--search-result-bg-active);
color: var(--search-result-color-active);

Expand Down

0 comments on commit 1dd929d

Please sign in to comment.