English
The shell and the routes
Step 1. The shell
Why. Every signed-in page lives in the same frame, the shell. It adapts to three widths, using Tailwind's breakpoints md (768 px) and xl (1280 px) (Table 17.1). One code base serves all of them with responsive classes such as hidden md:flex ("hidden on phones, a flex container from md up"); there is no separate mobile site.
Table 17.1. The layouts of the shell
| Width | Navigation | Panes |
|---|---|---|
phone (below md) | a bottom tab bar: Chats, Discover, Settings (hidden inside a conversation) | one at a time: the chat list on /chats, otherwise the page |
tablet (md to xl) | a navigation rail on the left | the chat list beside the main pane |
desktop (xl and up) | the rail | a wider list; the chat info panel docks on the right (topic 21) |
The chat list is the subject of topic 18. In this topic the shell has the rail, the tabs, the main pane and the theme switch; the yellow lines are temporary and change in the next topic.
Do. Create the shell:
angular-ts
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavigationEnd, Router, RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
import { filter, map } from 'rxjs';
import { AuthService } from '../../core/auth/auth.service';
import { ThemeService } from '../../core/ui/theme.service';
import { ICONS } from '../../shared/ui/icons';
/**
* The signed-in layout.
* - Phone (< md): one pane at a time. /chats shows the list, a chat fills the screen, and a bottom
* tab bar switches between Chats, Discover and Settings (hidden inside a conversation).
* - Tablet (md–xl): nav rail + chat list + main pane.
* - Desktop (≥ xl): the same with a wider list; chat info docks on the right of the conversation.
*/
@Component({
selector: 'app-shell',
imports: [RouterOutlet, RouterLink, RouterLinkActive, ICONS],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div class="flex h-dvh overflow-hidden">
<!-- Nav rail (tablet and desktop) -->
<nav
class="hidden w-18 shrink-0 flex-col items-center gap-2 border-r border-line bg-surface py-4 md:flex"
aria-label="Main"
>
<a
routerLink="/chats"
class="mb-4 flex size-11 items-center justify-center rounded-xl bg-brand-600 text-white"
aria-label="Chatex home"
>
<svg lucideMessagesSquare class="size-6"></svg>
</a>
@for (item of nav; track item.path) {
<a
[routerLink]="item.path"
routerLinkActive="!bg-brand-50 !text-brand-700 dark:!bg-brand-950 dark:!text-brand-300"
class="relative flex size-11 items-center justify-center rounded-xl text-muted transition-colors hover:bg-subtle hover:text-fg"
[attr.aria-label]="item.label"
[title]="item.label"
>
@switch (item.icon) {
@case ('chats') {
<svg lucideMessagesSquare class="size-5"></svg>
}
@case ('discover') {
<svg lucideCompass class="size-5"></svg>
}
@case ('settings') {
<svg lucideSettings class="size-5"></svg>
}
}
</a>
}
<div class="mt-auto flex flex-col items-center gap-2">
<button
type="button"
class="icon-btn"
(click)="toggleTheme()"
[attr.aria-label]="theme.isDark() ? 'Switch to light theme' : 'Switch to dark theme'"
[title]="theme.isDark() ? 'Light theme' : 'Dark theme'"
>
@if (theme.isDark()) {
<svg lucideSun class="size-5"></svg>
} @else {
<svg lucideMoon class="size-5"></svg>
}
</button>
</div>
</nav>
<!-- Main pane -->
<main
class="min-w-0 flex-1 flex-col md:flex"
[class.hidden]="onChatsRoot()"
[class.flex]="!onChatsRoot()"
>
<div class="flex min-h-0 flex-1 flex-col">
<router-outlet />
</div>
</main>
</div>
<!-- Bottom tab bar (phones), hidden inside a conversation -->
@if (!inConversation()) {
<nav
class="pb-safe fixed inset-x-0 bottom-0 z-30 flex border-t border-line bg-surface/95 backdrop-blur md:hidden"
aria-label="Main"
>
@for (item of nav; track item.path) {
<a
[routerLink]="item.path"
routerLinkActive="!text-brand-600 dark:!text-brand-400"
[routerLinkActiveOptions]="{
exact: item.path !== '/settings' && item.path !== '/discover',
}"
class="relative flex min-h-14 flex-1 flex-col items-center justify-center gap-0.5 text-[11px] text-muted"
>
@switch (item.icon) {
@case ('chats') {
<svg lucideMessagesSquare class="size-5"></svg>
}
@case ('discover') {
<svg lucideCompass class="size-5"></svg>
}
@case ('settings') {
<svg lucideSettings class="size-5"></svg>
}
}
{{ item.label }}
</a>
}
</nav>
}
`,
})
export class Shell {
protected readonly auth = inject(AuthService);
protected readonly theme = inject(ThemeService);
private readonly router = inject(Router);
protected readonly nav = [
{ path: '/chats', label: 'Chats', icon: 'chats' },
{ path: '/discover', label: 'Discover', icon: 'discover' },
{ path: '/settings', label: 'Settings', icon: 'settings' },
] as const;
private readonly url = toSignal(
this.router.events.pipe(
filter((e) => e instanceof NavigationEnd),
map(() => this.router.url),
),
{ initialValue: this.router.url },
);
private readonly path = computed(() => this.url().split(/[?#]/)[0]);
protected readonly onChatsRoot = computed(() => this.path() === '/chats');
protected readonly inConversation = computed(() => /^\/chats\/[^/]+/.test(this.path()));
protected toggleTheme(): void {
this.theme.set(this.theme.isDark() ? 'light' : 'dark');
}
}nav…as constis the list of the three destinations.as constmakes the strings literal types, so@switch (item.icon)with@case ('chats')is checked by the compiler.routerLinknavigates without reloading the page;routerLinkActiveadds classes while its link is active. The!prefix is Tailwind's important modifier: the active colours win over the hover colours. In the tab bar,routerLinkActiveOptionswithexact: truekeeps "Chats" from being active on every/chats/...URL.toSignal(observable, { initialValue })turns the router's event stream into a signal.router.eventsis an Observable of every navigation event;filterkeeps theNavigationEndevents andmaptakes the URL.path,onChatsRootandinConversationarecomputedfrom it, and the template shows or hides panes with[class.hidden]and[class.flex].toSignalunsubscribes by itself when the component is destroyed.- The theme switch toggles between light and dark with
ThemeServiceand shows the sun or the moon. The settings page (topic 21) also offers "system". <router-outlet />in the main pane is where the router renders the child routes of the shell.pb-safeon the tab bar keeps it above the home indicator of phones;backdrop-blurwith a translucent background lets content scroll under it.
The "Select a chat" placeholder fills the main pane when no chat is open. Its "New chat" button arrives with the new-chat dialog in topic 18, so the class is empty for now:
angular-ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { ICONS } from '../../shared/ui/icons';
/** The main pane on tablets and desktops when no chat is open (phones show the list instead). */
@Component({
selector: 'app-no-chat-page',
imports: [ICONS],
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
class: 'flex flex-1 flex-col items-center justify-center gap-4 bg-canvas p-8 text-center',
},
template: `
<span
class="flex size-20 items-center justify-center rounded-3xl bg-brand-50 text-brand-600 dark:bg-brand-950 dark:text-brand-300"
>
<svg lucideMessagesSquare class="size-10"></svg>
</span>
<div>
<h2 class="text-lg font-semibold">Select a chat</h2>
<p class="mt-1 text-sm text-muted">Pick a conversation from the list or start a new one.</p>
</div>
`,
})
export class NoChatPage {} The host property of @Component puts classes on the component's own element: <app-no-chat-page> becomes a flex column that fills the pane.
Step 2. Lazy routes
Why. The router maps URLs to components. If every page were imported in app.routes.ts, the whole application would be in the first download. With lazy loading, a route names a function that imports its component on demand; the build puts each such component into its own chunk, downloaded the first time the route is visited.
Do. Replace the generated app.routes.ts. Later topics add the routes of the conversation, the discover page and the settings.
ts
import { Routes } from '@angular/router';
import { authGuard, guestGuard } from './core/auth/guards';
export const routes: Routes = [
{
path: 'login',
canActivate: [guestGuard],
title: 'Sign in · Chatex',
loadComponent: () => import('./features/auth/login.page').then((m) => m.LoginPage),
},
{
path: 'register',
canActivate: [guestGuard],
title: 'Create account · Chatex',
loadComponent: () => import('./features/auth/register.page').then((m) => m.RegisterPage),
},
{
path: '',
canActivate: [authGuard],
loadComponent: () => import('./features/shell/shell').then((m) => m.Shell),
children: [
{ path: '', pathMatch: 'full', redirectTo: 'chats' },
{
path: 'chats',
loadComponent: () => import('./features/shell/no-chat.page').then((m) => m.NoChatPage),
},
],
},
{ path: '**', redirectTo: 'chats' },
];loadComponent: () => import('./features/auth/login.page').then((m) => m.LoginPage)— a dynamicimport()returns a Promise of the module; the router awaits it and rendersLoginPage.titlesets the browser tab's title while the route is active.canActivate: [guestGuard]keeps signed-in users away from the sign-in pages;[authGuard]on the empty path protects the shell and everything below it — the children inherit the protection.childrenare rendered in the shell's<router-outlet />. The empty child path redirects tochats, so/shows/chats.{ path: '**', redirectTo: 'chats' }catches unknown URLs. Routes are matched in order, so the wildcard must come last.
Check
powershell
npm run buildThe output now lists lazy chunk files next to the initial ones:
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-CH2kvnUj.js | - | 45.56 kB | 10.70 kB
chunk-Cpz8IVMC.js | register-page | 5.70 kB | 2.13 kB
chunk-X5XkpZYT.js | shell | 4.71 kB | 1.83 kB
chunk-C-Q74XFF.js | no-chat-page | 862 bytes | 862 bytes
chunk-CeyQRxek.js | login-page | 128 bytes | 128 bytesThe unnamed chunk holds code that several lazy pages share, such as the forms library.
Step 3. Try it
Do. Start the API and the client (./Run.ps1 api, ./Run.ps1 web) and open http://localhost:4200.
Check
- The guard redirects
/to/login, because there is no session. The tab title is "Sign in · Chatex". - Click Sign in with empty fields: both fields turn red with "Enter your user name or email." and "Enter your password.". Enter a wrong password for a non-existing user: "Wrong user name, email or password." appears above the form.
- Open Create an account and register a user, for example
alicewith the passwordPassw0rd!. The app goes to/chats: the rail on the left, "Select a chat" in the middle. - Try to register
aliceagain in a private window: the message "This user name is already taken." appears under the user name and disappears as soon as you edit the field. - Reload the page: you stay signed in. In the Network tab,
POST /api/auth/refreshnow answers200, and the initializer let the guard through. - Click the moon in the rail: the theme switches to dark and survives a reload.
- Make the window narrow (or use the device toolbar of the developer tools): the rail disappears and the bottom tab bar appears. On
/chatsthe phone layout shows only the (still missing) chat list, so the screen is empty apart from the tabs; topic 18 fills it.
There is no sign-out button yet; it arrives with the settings page in topic 21. Until then, sign out by deleting the cookie chatex_refresh in the developer tools (Application → Cookies) and reloading.