feat(ui): New sidebar & search UI (#831)
This commit is contained in:
@@ -1,14 +1,31 @@
|
||||
<ng-container *transloco="let t; prefix: 'layout'">
|
||||
<div class="layout-wrapper" [ngClass]="containerClass()">
|
||||
<app-topbar></app-topbar>
|
||||
<div class="layout-sidebar">
|
||||
<div class="layout-wrapper" [ngClass]="containerClass()" [style.--sidebar-stored-width]="storedSidebarWidth()">
|
||||
<app-mobile-topbar></app-mobile-topbar>
|
||||
<nav
|
||||
class="layout-sidebar"
|
||||
[attr.aria-label]="t('menu.primaryNav')"
|
||||
[attr.aria-hidden]="sidebarHidden() ? 'true' : null"
|
||||
[attr.inert]="sidebarHidden() ? '' : null"
|
||||
[cdkTrapFocus]="layoutService.mobileDrawerOpen()"
|
||||
>
|
||||
<app-menu></app-menu>
|
||||
</div>
|
||||
<div class="layout-main-container">
|
||||
<div class="layout-main">
|
||||
<router-outlet></router-outlet>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="layout-sidebar-resize-handle"
|
||||
tabindex="0"
|
||||
role="slider"
|
||||
[attr.aria-label]="t('menu.resizeSidebar')"
|
||||
[attr.aria-valuemin]="sidebarMinWidth"
|
||||
[attr.aria-valuemax]="sidebarMaxWidth"
|
||||
[attr.aria-valuenow]="layoutService.sidebarWidth()"
|
||||
aria-orientation="horizontal"
|
||||
(mousedown)="startResize($event)"
|
||||
(keydown)="onResizeKeydown($event)"
|
||||
(keyup)="onResizeKeyup($event)"
|
||||
></div>
|
||||
</nav>
|
||||
<main class="layout-main">
|
||||
<router-outlet></router-outlet>
|
||||
</main>
|
||||
<button type="button" class="layout-mask" [attr.aria-label]="t('closeNavigation')" (click)="closeMobileSidebar()"></button>
|
||||
</div>
|
||||
</ng-container>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { provideRouter, Router } from '@angular/router';
|
||||
import { TranslocoTestingModule } from '@jsverse/transloco';
|
||||
import { AppLayoutComponent } from './app.layout.component';
|
||||
import { AppMenuComponent } from '../layout-menu/app.menu.component';
|
||||
import { AppTopBarComponent } from '../layout-topbar/app.topbar.component';
|
||||
import { AppMobileTopbarComponent } from '../layout-mobile-topbar/app.mobile-topbar.component';
|
||||
import { LocalStorageService } from '../../service/local-storage.service';
|
||||
import { LayoutService } from '../layout.service';
|
||||
|
||||
@@ -17,11 +17,11 @@ import { LayoutService } from '../layout.service';
|
||||
class StubMenuComponent {}
|
||||
|
||||
@Component({
|
||||
selector: 'app-topbar',
|
||||
selector: 'app-mobile-topbar',
|
||||
standalone: true,
|
||||
template: '',
|
||||
})
|
||||
class StubTopBarComponent {}
|
||||
class StubMobileTopbarComponent {}
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
@@ -34,11 +34,21 @@ describe('AppLayoutComponent', () => {
|
||||
let router: Router;
|
||||
let layoutService: LayoutService;
|
||||
|
||||
const localStorageValues: Record<string, unknown> = {};
|
||||
const localStorageService = {
|
||||
get: vi.fn(() => 225),
|
||||
get: vi.fn((key: string) => localStorageValues[key]),
|
||||
set: vi.fn((key: string, value: unknown) => {
|
||||
localStorageValues[key] = value;
|
||||
}),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
for (const key of Object.keys(localStorageValues)) {
|
||||
delete localStorageValues[key];
|
||||
}
|
||||
localStorageValues['sidebarWidth'] = 225;
|
||||
localStorageService.get.mockClear();
|
||||
localStorageService.set.mockClear();
|
||||
TestBed.resetTestingModule();
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
@@ -56,10 +66,10 @@ describe('AppLayoutComponent', () => {
|
||||
|
||||
TestBed.overrideComponent(AppLayoutComponent, {
|
||||
remove: {
|
||||
imports: [AppMenuComponent, AppTopBarComponent],
|
||||
imports: [AppMenuComponent, AppMobileTopbarComponent],
|
||||
},
|
||||
add: {
|
||||
imports: [StubMenuComponent, StubTopBarComponent],
|
||||
imports: [StubMenuComponent, StubMobileTopbarComponent],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -79,35 +89,110 @@ describe('AppLayoutComponent', () => {
|
||||
});
|
||||
|
||||
it('closes the mobile sidebar when the mask is clicked', () => {
|
||||
layoutService.mobileSidebarOpen.set(true);
|
||||
layoutService.mobileDrawerOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
const mask = fixture.nativeElement.querySelector('.layout-mask') as HTMLDivElement;
|
||||
mask.click();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(layoutService.mobileSidebarOpen()).toBe(false);
|
||||
expect(layoutService.mobileDrawerOpen()).toBe(false);
|
||||
});
|
||||
|
||||
it('toggles the body scroll lock with mobile sidebar state', () => {
|
||||
layoutService.mobileSidebarOpen.set(true);
|
||||
layoutService.mobileDrawerOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(document.body.classList.contains('blocked-scroll')).toBe(true);
|
||||
|
||||
layoutService.mobileSidebarOpen.set(false);
|
||||
layoutService.mobileDrawerOpen.set(false);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(document.body.classList.contains('blocked-scroll')).toBe(false);
|
||||
});
|
||||
|
||||
it('closes the mobile sidebar on navigation', async () => {
|
||||
layoutService.mobileSidebarOpen.set(true);
|
||||
layoutService.mobileDrawerOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
await router.navigateByUrl('/next');
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(layoutService.mobileSidebarOpen()).toBe(false);
|
||||
expect(layoutService.mobileDrawerOpen()).toBe(false);
|
||||
});
|
||||
|
||||
it('uses an explicit sidebar-hidden shell state when the desktop sidebar is hidden', () => {
|
||||
layoutService.isDesktop.set(true);
|
||||
layoutService.sidebarVisible.set(false);
|
||||
fixture.detectChanges();
|
||||
|
||||
const wrapper = fixture.nativeElement.querySelector('.layout-wrapper') as HTMLDivElement;
|
||||
const sidebar = fixture.nativeElement.querySelector('.layout-sidebar') as HTMLElement;
|
||||
|
||||
expect(wrapper.classList.contains('layout-sidebar-hidden')).toBe(true);
|
||||
expect(sidebar.getAttribute('aria-hidden')).toBe('true');
|
||||
expect(sidebar.hasAttribute('inert')).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps the mobile drawer reachable when the desktop sidebar preference is hidden', () => {
|
||||
layoutService.isDesktop.set(false);
|
||||
layoutService.sidebarVisible.set(false);
|
||||
layoutService.mobileDrawerOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
const wrapper = fixture.nativeElement.querySelector('.layout-wrapper') as HTMLDivElement;
|
||||
const sidebar = fixture.nativeElement.querySelector('.layout-sidebar') as HTMLElement;
|
||||
|
||||
expect(wrapper.classList.contains('layout-sidebar-hidden')).toBe(false);
|
||||
expect(sidebar.getAttribute('aria-hidden')).toBeNull();
|
||||
expect(sidebar.hasAttribute('inert')).toBe(false);
|
||||
});
|
||||
|
||||
it('cleans up an active sidebar resize when the component is destroyed', () => {
|
||||
const component = fixture.componentInstance;
|
||||
|
||||
layoutService.sidebarCollapsed.set(false);
|
||||
layoutService.isDesktop.set(true);
|
||||
component.startResize(new MouseEvent('mousedown', { clientX: 200 }));
|
||||
expect(document.body.classList.contains('layout-resizing-cursor')).toBe(true);
|
||||
|
||||
fixture.destroy();
|
||||
document.dispatchEvent(new MouseEvent('mousemove', { clientX: 260 }));
|
||||
|
||||
expect(document.body.classList.contains('layout-resizing-cursor')).toBe(false);
|
||||
expect(layoutService.sidebarWidth()).toBe(225);
|
||||
});
|
||||
|
||||
it('supports keyboard resizing for the desktop sidebar handle', () => {
|
||||
layoutService.sidebarCollapsed.set(false);
|
||||
layoutService.isDesktop.set(true);
|
||||
layoutService.sidebarWidth.set(225);
|
||||
fixture.detectChanges();
|
||||
|
||||
const handle = fixture.nativeElement.querySelector('.layout-sidebar-resize-handle') as HTMLDivElement;
|
||||
handle.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' }));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(layoutService.sidebarWidth()).toBe(241);
|
||||
expect(localStorageService.set).not.toHaveBeenCalledWith('sidebarWidth', 241);
|
||||
|
||||
handle.dispatchEvent(new KeyboardEvent('keyup', { key: 'ArrowRight' }));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(localStorageService.set).toHaveBeenCalledWith('sidebarWidth', 241);
|
||||
expect(handle.getAttribute('role')).toBe('slider');
|
||||
expect(handle.getAttribute('tabindex')).toBe('0');
|
||||
});
|
||||
|
||||
it('uses the collapsed rail width while preserving the stored expanded width', () => {
|
||||
layoutService.sidebarWidth.set(225);
|
||||
layoutService.sidebarCollapsed.set(true);
|
||||
layoutService.isDesktop.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
const wrapper = fixture.nativeElement.querySelector('.layout-wrapper') as HTMLDivElement;
|
||||
|
||||
expect(document.documentElement.style.getPropertyValue('--sidebar-width')).toBe('var(--sidebar-collapsed-width)');
|
||||
expect(wrapper.style.getPropertyValue('--sidebar-stored-width')).toBe('225px');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,47 +1,77 @@
|
||||
import { DOCUMENT, NgClass } from '@angular/common';
|
||||
import { computed, Component, DestroyRef, effect, inject, OnInit } from '@angular/core';
|
||||
import { computed, Component, DestroyRef, effect, inject } from '@angular/core';
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
|
||||
import { filter } from 'rxjs';
|
||||
import { LayoutService } from '../layout.service';
|
||||
import { CdkTrapFocus } from '@angular/cdk/a11y';
|
||||
import { LayoutService, SIDEBAR_MAX_WIDTH, SIDEBAR_MIN_WIDTH } from '../layout.service';
|
||||
import { AppMenuComponent } from '../layout-menu/app.menu.component';
|
||||
import { AppTopBarComponent } from '../layout-topbar/app.topbar.component';
|
||||
import { AppMobileTopbarComponent } from '../layout-mobile-topbar/app.mobile-topbar.component';
|
||||
import { TranslocoDirective } from '@jsverse/transloco';
|
||||
import { LocalStorageService } from '../../service/local-storage.service';
|
||||
|
||||
const SIDEBAR_COLLAPSED_WIDTH = 'var(--sidebar-collapsed-width)';
|
||||
const SIDEBAR_KEYBOARD_STEP = 16;
|
||||
const SIDEBAR_KEYBOARD_PAGE_STEP = 48;
|
||||
|
||||
@Component({
|
||||
selector: 'app-layout',
|
||||
imports: [
|
||||
RouterOutlet,
|
||||
AppMenuComponent,
|
||||
AppTopBarComponent,
|
||||
AppMobileTopbarComponent,
|
||||
CdkTrapFocus,
|
||||
NgClass,
|
||||
TranslocoDirective
|
||||
],
|
||||
templateUrl: './app.layout.component.html'
|
||||
})
|
||||
export class AppLayoutComponent implements OnInit {
|
||||
export class AppLayoutComponent {
|
||||
readonly layoutService = inject(LayoutService);
|
||||
readonly sidebarMinWidth = SIDEBAR_MIN_WIDTH;
|
||||
readonly sidebarMaxWidth = SIDEBAR_MAX_WIDTH;
|
||||
|
||||
private readonly router = inject(Router);
|
||||
private readonly localStorageService = inject(LocalStorageService);
|
||||
private readonly document = inject(DOCUMENT);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
|
||||
private resizeStartX = 0;
|
||||
private resizeStartWidth = 0;
|
||||
private isResizing = false;
|
||||
private keyboardResizing = false;
|
||||
|
||||
readonly containerClass = computed(() => ({
|
||||
'layout-static': true,
|
||||
'layout-static-inactive': !this.layoutService.sidebarOpen(),
|
||||
'layout-mobile-active': this.layoutService.mobileSidebarOpen()
|
||||
'layout-sidebar-hidden': this.sidebarHidden(),
|
||||
'layout-mobile-active': this.layoutService.mobileDrawerOpen(),
|
||||
'layout-collapsed': this.layoutService.sidebarCollapsed() && this.layoutService.isDesktop()
|
||||
}));
|
||||
|
||||
readonly sidebarHidden = computed(() => this.layoutService.isDesktop() && !this.layoutService.sidebarVisible());
|
||||
readonly storedSidebarWidth = computed(() => `${this.layoutService.sidebarWidth()}px`);
|
||||
|
||||
constructor() {
|
||||
this.destroyRef.onDestroy(() => {
|
||||
if (this.keyboardResizing) {
|
||||
this.layoutService.setSidebarWidth(this.layoutService.sidebarWidth(), true);
|
||||
}
|
||||
this.cleanupResize(false);
|
||||
});
|
||||
|
||||
effect((onCleanup) => {
|
||||
const body = this.document.body;
|
||||
body.classList.toggle('blocked-scroll', this.layoutService.mobileSidebarOpen());
|
||||
body.classList.toggle('blocked-scroll', this.layoutService.mobileDrawerOpen());
|
||||
onCleanup(() => {
|
||||
body.classList.remove('blocked-scroll');
|
||||
});
|
||||
});
|
||||
|
||||
effect(() => {
|
||||
const collapsed = this.layoutService.sidebarCollapsed();
|
||||
const isDesktop = this.layoutService.isDesktop();
|
||||
const storedWidth = this.layoutService.sidebarWidth();
|
||||
const width = (collapsed && isDesktop) ? SIDEBAR_COLLAPSED_WIDTH : `${storedWidth}px`;
|
||||
this.document.documentElement.style.setProperty('--sidebar-width', width);
|
||||
});
|
||||
|
||||
this.router.events
|
||||
.pipe(
|
||||
filter((event): event is NavigationEnd => event instanceof NavigationEnd),
|
||||
@@ -52,12 +82,86 @@ export class AppLayoutComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
const width = this.localStorageService.get<number>('sidebarWidth') ?? 225;
|
||||
this.document.documentElement.style.setProperty('--sidebar-width', `${width}px`);
|
||||
}
|
||||
|
||||
closeMobileSidebar(): void {
|
||||
this.layoutService.closeMobileSidebar();
|
||||
}
|
||||
|
||||
startResize(event: MouseEvent): void {
|
||||
if (this.layoutService.sidebarCollapsed() || !this.layoutService.isDesktop()) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
this.isResizing = true;
|
||||
this.resizeStartX = event.clientX;
|
||||
this.resizeStartWidth = this.layoutService.sidebarWidth();
|
||||
this.document.body.classList.add('layout-resizing-cursor');
|
||||
this.document.addEventListener('mousemove', this.onResizeMove);
|
||||
this.document.addEventListener('mouseup', this.onResizeEnd);
|
||||
}
|
||||
|
||||
onResizeKeydown(event: KeyboardEvent): void {
|
||||
if (this.layoutService.sidebarCollapsed() || !this.layoutService.isDesktop()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextWidth = this.getKeyboardResizeWidth(event.key, this.layoutService.sidebarWidth());
|
||||
if (nextWidth === null) return;
|
||||
|
||||
event.preventDefault();
|
||||
this.keyboardResizing = true;
|
||||
this.layoutService.setSidebarWidth(nextWidth, false);
|
||||
}
|
||||
|
||||
onResizeKeyup(event: KeyboardEvent): void {
|
||||
if (!this.layoutService.isDesktop()) return;
|
||||
if (!this.isResizeKey(event.key)) return;
|
||||
this.layoutService.setSidebarWidth(this.layoutService.sidebarWidth(), true);
|
||||
this.keyboardResizing = false;
|
||||
}
|
||||
|
||||
private readonly onResizeMove = (event: MouseEvent) => {
|
||||
if (!this.isResizing) return;
|
||||
const dx = event.clientX - this.resizeStartX;
|
||||
this.layoutService.setSidebarWidth(this.resizeStartWidth + dx, false);
|
||||
};
|
||||
|
||||
private readonly onResizeEnd = () => {
|
||||
this.cleanupResize(true);
|
||||
};
|
||||
|
||||
private isResizeKey(key: string): boolean {
|
||||
return this.getKeyboardResizeWidth(key, 0) !== null;
|
||||
}
|
||||
|
||||
private getKeyboardResizeWidth(key: string, currentWidth: number): number | null {
|
||||
switch (key) {
|
||||
case 'ArrowLeft':
|
||||
case 'ArrowDown':
|
||||
return currentWidth - SIDEBAR_KEYBOARD_STEP;
|
||||
case 'ArrowRight':
|
||||
case 'ArrowUp':
|
||||
return currentWidth + SIDEBAR_KEYBOARD_STEP;
|
||||
case 'PageDown':
|
||||
return currentWidth - SIDEBAR_KEYBOARD_PAGE_STEP;
|
||||
case 'PageUp':
|
||||
return currentWidth + SIDEBAR_KEYBOARD_PAGE_STEP;
|
||||
case 'Home':
|
||||
return SIDEBAR_MIN_WIDTH;
|
||||
case 'End':
|
||||
return SIDEBAR_MAX_WIDTH;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private cleanupResize(persistWidth: boolean): void {
|
||||
if (!this.isResizing) return;
|
||||
this.isResizing = false;
|
||||
if (persistWidth) {
|
||||
this.layoutService.setSidebarWidth(this.layoutService.sidebarWidth(), true);
|
||||
}
|
||||
this.document.body.classList.remove('layout-resizing-cursor');
|
||||
this.document.removeEventListener('mousemove', this.onResizeMove);
|
||||
this.document.removeEventListener('mouseup', this.onResizeEnd);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user