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