refactor(frontend): decouple app shell from PrimeNG layout system (#307)
* refactor(layout): decouple shell from PrimeNG * fix(frontend): review fixes for layout decoupling - Rewrite layout service spec to match new API surface - Remove dead ToastModule import from AppLayoutComponent - Add missing type="button" to profile and logout buttons - Add missing i18n keys to all non-English locales * chore(frontend): Layout shell consistency * chore(frontend): shell and routing improvements * chore(frontend): remove stray shimmer effect * chore(frontend): code cleanup * chore(ui): Fix navitem types consistency * chore(frontend): simplify layout structure + imports * chore(frontend): remove empty files * chore(frontend): Menu component cleanup * chore(frontend): remove nav translation layer * chore(frontend): revert stray artwork change * chore(frontend): revert author animation tweak * chore(frontend): review fixes * fix(frontend): restore topbar template after rebase conflict
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<ng-container *transloco="let t; prefix: 'layout'">
|
||||
<div class="layout-wrapper" [ngClass]="containerClass()">
|
||||
<app-topbar></app-topbar>
|
||||
<div class="layout-sidebar">
|
||||
<app-menu></app-menu>
|
||||
</div>
|
||||
<div class="layout-main-container">
|
||||
<div class="layout-main">
|
||||
<router-outlet></router-outlet>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="layout-mask" [attr.aria-label]="t('closeNavigation')" (click)="closeMobileSidebar()"></button>
|
||||
</div>
|
||||
</ng-container>
|
||||
@@ -0,0 +1,113 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { Component } from '@angular/core';
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
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 { LocalStorageService } from '../../service/local-storage.service';
|
||||
import { LayoutService } from '../layout.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-menu',
|
||||
standalone: true,
|
||||
template: '',
|
||||
})
|
||||
class StubMenuComponent {}
|
||||
|
||||
@Component({
|
||||
selector: 'app-topbar',
|
||||
standalone: true,
|
||||
template: '',
|
||||
})
|
||||
class StubTopBarComponent {}
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
template: '',
|
||||
})
|
||||
class DummyRouteComponent {}
|
||||
|
||||
describe('AppLayoutComponent', () => {
|
||||
let fixture: ComponentFixture<AppLayoutComponent>;
|
||||
let router: Router;
|
||||
let layoutService: LayoutService;
|
||||
|
||||
const localStorageService = {
|
||||
get: vi.fn(() => 225),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
TestBed.resetTestingModule();
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
AppLayoutComponent,
|
||||
TranslocoTestingModule.forRoot({ langs: {} }),
|
||||
],
|
||||
providers: [
|
||||
provideRouter([
|
||||
{ path: '', component: DummyRouteComponent },
|
||||
{ path: 'next', component: DummyRouteComponent },
|
||||
]),
|
||||
{ provide: LocalStorageService, useValue: localStorageService },
|
||||
],
|
||||
});
|
||||
|
||||
TestBed.overrideComponent(AppLayoutComponent, {
|
||||
remove: {
|
||||
imports: [AppMenuComponent, AppTopBarComponent],
|
||||
},
|
||||
add: {
|
||||
imports: [StubMenuComponent, StubTopBarComponent],
|
||||
},
|
||||
});
|
||||
|
||||
fixture = TestBed.createComponent(AppLayoutComponent);
|
||||
router = TestBed.inject(Router);
|
||||
layoutService = TestBed.inject(LayoutService);
|
||||
|
||||
fixture.detectChanges();
|
||||
await router.navigateByUrl('/');
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
document.body.classList.remove('blocked-scroll');
|
||||
fixture?.destroy();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('closes the mobile sidebar when the mask is clicked', () => {
|
||||
layoutService.mobileSidebarOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
const mask = fixture.nativeElement.querySelector('.layout-mask') as HTMLDivElement;
|
||||
mask.click();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(layoutService.mobileSidebarOpen()).toBe(false);
|
||||
});
|
||||
|
||||
it('toggles the body scroll lock with mobile sidebar state', () => {
|
||||
layoutService.mobileSidebarOpen.set(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(document.body.classList.contains('blocked-scroll')).toBe(true);
|
||||
|
||||
layoutService.mobileSidebarOpen.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);
|
||||
fixture.detectChanges();
|
||||
|
||||
await router.navigateByUrl('/next');
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(layoutService.mobileSidebarOpen()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { DOCUMENT, NgClass } from '@angular/common';
|
||||
import { computed, Component, DestroyRef, effect, inject, OnInit } 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 { AppMenuComponent } from '../layout-menu/app.menu.component';
|
||||
import { AppTopBarComponent } from '../layout-topbar/app.topbar.component';
|
||||
import { TranslocoDirective } from '@jsverse/transloco';
|
||||
import { LocalStorageService } from '../../service/local-storage.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-layout',
|
||||
imports: [
|
||||
RouterOutlet,
|
||||
AppMenuComponent,
|
||||
AppTopBarComponent,
|
||||
NgClass,
|
||||
TranslocoDirective
|
||||
],
|
||||
templateUrl: './app.layout.component.html'
|
||||
})
|
||||
export class AppLayoutComponent implements OnInit {
|
||||
readonly layoutService = inject(LayoutService);
|
||||
private readonly router = inject(Router);
|
||||
private readonly localStorageService = inject(LocalStorageService);
|
||||
private readonly document = inject(DOCUMENT);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
|
||||
readonly containerClass = computed(() => ({
|
||||
'layout-static': true,
|
||||
'layout-static-inactive': !this.layoutService.sidebarOpen(),
|
||||
'layout-mobile-active': this.layoutService.mobileSidebarOpen()
|
||||
}));
|
||||
|
||||
constructor() {
|
||||
effect((onCleanup) => {
|
||||
const body = this.document.body;
|
||||
body.classList.toggle('blocked-scroll', this.layoutService.mobileSidebarOpen());
|
||||
onCleanup(() => {
|
||||
body.classList.remove('blocked-scroll');
|
||||
});
|
||||
});
|
||||
|
||||
this.router.events
|
||||
.pipe(
|
||||
filter((event): event is NavigationEnd => event instanceof NavigationEnd),
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
)
|
||||
.subscribe(() => {
|
||||
this.layoutService.closeMobileSidebar();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
const width = this.localStorageService.get<number>('sidebarWidth') ?? 225;
|
||||
this.document.documentElement.style.setProperty('--sidebar-width', `${width}px`);
|
||||
}
|
||||
|
||||
closeMobileSidebar(): void {
|
||||
this.layoutService.closeMobileSidebar();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user