improve book searcher UX with debounce, keyboard nav, and theme-aware styles (#3093)

This commit is contained in:
ACX
2026-02-27 17:58:36 -07:00
committed by GitHub
parent 0315670432
commit c6e5cfe534
3 changed files with 90 additions and 48 deletions
@@ -8,7 +8,7 @@
[(ngModel)]="searchQuery"
(input)="onSearchInputChange()"
(focus)="isSearchFocused = true"
(blur)="onSearchBlur()"
(keydown)="onKeydown($event)"
[placeholder]="t('placeholder')"
class="search-input"
/>
@@ -27,11 +27,21 @@
@if (books.length > 0 || searchQuery) {
<div
class="search-dropdown layout-menu"
[class.show]="(books.length > 0)"
[class.show]="isDropdownOpen"
>
@if (books.length > 0) {
@for (book of books; track book) {
<div class="search-dropdown-item" (click)="onBookClick(book)">
@if (isLoading) {
@for (i of [1,2,3]; track i) {
<div class="search-skeleton-item">
<p-skeleton width="50px" height="67px" borderRadius="4px"/>
<div class="search-skeleton-details">
<p-skeleton width="80%" height="1rem"/>
<p-skeleton width="60%" height="0.8rem"/>
</div>
</div>
}
} @else if (books.length > 0) {
@for (book of books; track book; let idx = $index) {
<div class="search-dropdown-item" [class.active]="activeIndex === idx" (click)="onBookClick(book)">
<div class="search-item-content">
<img
[attr.src]="urlHelper.getThumbnailUrl(book.id, book.metadata?.coverUpdatedOn)"
@@ -60,7 +70,6 @@
}
</div>
</div>
<p-divider></p-divider>
</div>
}
} @else {
@@ -45,7 +45,7 @@
transform: translateY(-50%);
display: none;
@media (min-width: 1024px) {
@media (min-width: 640px) {
display: inline-flex;
}
}
@@ -54,7 +54,8 @@
position: absolute;
top: calc(100% + 0.5rem);
left: 0;
width: 300px;
width: 100%;
max-width: 600px;
background-color: var(--card-background);
border: 1px solid var(--border-color);
border-radius: 8px;
@@ -62,18 +63,13 @@
0 2px 4px -1px rgba(0, 0, 0, 0.06),
0 10px 15px -3px rgba(0, 0, 0, 0.1);
z-index: 1000;
max-height: 70vh;
max-height: min(70vh, calc(100dvh - 4rem));
overflow-y: auto;
opacity: 0;
height: 0;
transform: translateY(-10px);
transition: opacity 0.2s ease, transform 0.2s ease, height 0.2s ease;
@media (min-width: 640px) {
width: 100%;
max-width: 600px;
}
&::-webkit-scrollbar {
width: 8px;
}
@@ -103,6 +99,7 @@
flex-direction: column;
cursor: pointer;
transition: background-color 0.2s ease;
border-bottom: 1px solid color-mix(in srgb, var(--border-color) 50%, transparent);
&:first-child {
border-radius: 8px 8px 0 0;
@@ -110,13 +107,11 @@
&:last-child {
border-radius: 0 0 8px 8px;
::ng-deep .p-divider {
display: none;
}
border-bottom: none;
}
&:hover {
&:hover,
&.active {
background-color: var(--surface-hover);
}
@@ -126,15 +121,6 @@
color: var(--text-secondary-color);
font-style: italic;
}
::ng-deep .p-divider {
margin: 0;
&::before {
border-top-color: var(--border-color);
opacity: 0.5;
}
}
}
.search-item-content {
@@ -154,6 +140,7 @@
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: var(--surface-ground);
aspect-ratio: 2/3;
}
.search-book-details {
@@ -161,7 +148,7 @@
min-width: 0;
display: flex;
flex-direction: column;
gap: 0.5rem;
gap: 0.25rem;
}
.search-book-title-container {
@@ -254,12 +241,21 @@
}
&.series-badge {
background-color: rgba(59, 130, 246, 0.1);
color: rgb(59, 130, 246);
}
&.year-badge {
background-color: rgba(34, 197, 94, 0.1);
color: rgb(34, 197, 94);
background-color: color-mix(in srgb, var(--p-primary-color) 10%, transparent);
color: var(--p-primary-color);
}
}
.search-skeleton-item {
display: flex;
flex-direction: row;
gap: 1rem;
padding: 0.75rem 1rem;
}
.search-skeleton-details {
display: flex;
flex-direction: column;
gap: 0.5rem;
flex: 1;
}
@@ -1,13 +1,13 @@
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Component, ElementRef, HostListener, inject, OnDestroy, OnInit} from '@angular/core';
import {BehaviorSubject, of, Subscription} from 'rxjs';
import {catchError, switchMap} from 'rxjs/operators';
import {catchError, debounceTime, distinctUntilChanged, switchMap, tap} from 'rxjs/operators';
import {Book} from '../../model/book.model';
import {FormsModule} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {BookService} from '../../service/book.service';
import {Button} from 'primeng/button';
import {SlicePipe} from '@angular/common';
import {Divider} from 'primeng/divider';
import {Skeleton} from 'primeng/skeleton';
import {UrlHelperService} from '../../../../shared/service/url-helper.service';
import {Router} from '@angular/router';
import {IconField} from 'primeng/iconfield';
@@ -23,7 +23,7 @@ import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
InputTextModule,
Button,
SlicePipe,
Divider,
Skeleton,
IconField,
InputIcon,
TranslocoDirective,
@@ -34,6 +34,8 @@ import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
export class BookSearcherComponent implements OnInit, OnDestroy {
searchQuery: string = '';
books: Book[] = [];
isLoading = false;
activeIndex = -1;
#searchSubject = new BehaviorSubject<string>('');
#subscription!: Subscription;
isSearchFocused = false;
@@ -42,14 +44,24 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
private router = inject(Router);
protected urlHelper = inject(UrlHelperService);
private readonly t = inject(TranslocoService);
private headerFilter = new HeaderFilter(this.#searchSubject.asObservable());
private elRef = inject(ElementRef);
private headerFilter = new HeaderFilter(
this.#searchSubject.pipe(debounceTime(200), distinctUntilChanged())
);
ngOnInit(): void {
this.#subscription = this.bookService.bookState$.pipe(
tap(() => {
if (this.searchQuery.trim().length >= 2) {
this.isLoading = true;
}
}),
switchMap(bookState => this.headerFilter.filter(bookState)),
catchError(() => of({books: [], loaded: true, error: null}))
).subscribe({
next: (filteredState) => {
this.isLoading = false;
this.activeIndex = -1;
const term = this.searchQuery.trim();
this.books = term.length >= 2
? (filteredState.books || []).slice(0, 50)
@@ -90,18 +102,43 @@ export class BookSearcherComponent implements OnInit, OnDestroy {
clearSearch(): void {
this.searchQuery = '';
this.books = [];
this.isLoading = false;
}
get isDropdownOpen(): boolean {
return this.books.length > 0;
return this.books.length > 0 || this.isLoading;
}
onSearchBlur(): void {
setTimeout(() => {
if (!this.isDropdownOpen) {
this.isSearchFocused = false;
}
}, 200);
@HostListener('document:click', ['$event'])
onDocumentClick(event: Event): void {
if (!this.elRef.nativeElement.contains(event.target)) {
this.clearSearch();
this.isSearchFocused = false;
}
}
onKeydown(event: KeyboardEvent): void {
if (!this.isDropdownOpen) return;
switch (event.key) {
case 'ArrowDown':
this.activeIndex = Math.min(this.activeIndex + 1, this.books.length - 1);
event.preventDefault();
break;
case 'ArrowUp':
this.activeIndex = Math.max(this.activeIndex - 1, 0);
event.preventDefault();
break;
case 'Enter':
if (this.activeIndex >= 0 && this.activeIndex < this.books.length) {
this.onBookClick(this.books[this.activeIndex]);
}
break;
case 'Escape':
this.clearSearch();
(event.target as HTMLElement).blur();
break;
}
}
ngOnDestroy(): void {