English
Tailwind CSS and the theme
Step 1. Tailwind CSS v4
Why. Chatex has no component style files. Every element is styled with utility classes in its template: flex items-center gap-2 rounded-lg px-4 says what a stylesheet rule would say, right where the element is. Tailwind CSS generates exactly the classes that the source code uses, so the stylesheet stays small (about 10 kB at the end of this topic). The alternatives — a component library such as Angular Material, or hand-written CSS per component — either impose a look or scatter the design over many files.
Tailwind v4 is configured in CSS, not in a JavaScript file. It runs as a PostCSS plugin, which the Angular build picks up from .postcssrc.json.
Do. ng new --style tailwind has written the PostCSS configuration; replace it to get LF endings and a final newline:
json
{
"plugins": {
"@tailwindcss/postcss": {}
}
}Step 2. Tokens, utilities and dark mode
Why. A design is a small set of decisions — colours, a font, spacings, a few repeated components such as buttons and inputs. Kept in one stylesheet, they can be changed in one place, and the templates speak in names (btn-primary, bg-surface) instead of raw values.
Do. Replace src/styles.css:
css
@import 'tailwindcss';
/* Dark mode is class based so the user can override the OS setting (see ThemeService). */
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--font-sans:
'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
sans-serif;
--color-brand-50: oklch(96.2% 0.018 272.314);
--color-brand-100: oklch(93% 0.034 272.788);
--color-brand-200: oklch(87% 0.065 274.039);
--color-brand-300: oklch(78.5% 0.115 274.713);
--color-brand-400: oklch(67.3% 0.182 276.935);
--color-brand-500: oklch(58.5% 0.233 277.117);
--color-brand-600: oklch(51.1% 0.262 276.966);
--color-brand-700: oklch(45.7% 0.24 277.023);
--color-brand-800: oklch(39.8% 0.195 277.366);
--color-brand-900: oklch(35.9% 0.144 278.697);
--color-brand-950: oklch(25.7% 0.09 281.288);
}
/* Semantic surface tokens that switch with the theme: bg-canvas, bg-surface, bg-subtle, border-line, text-fg, text-muted. */
@theme inline {
--color-canvas: var(--c-canvas);
--color-surface: var(--c-surface);
--color-subtle: var(--c-subtle);
--color-line: var(--c-line);
--color-fg: var(--c-fg);
--color-muted: var(--c-muted);
--color-bubble: var(--c-bubble);
}
:root {
color-scheme: light;
--c-canvas: oklch(96.7% 0.001 286.375);
--c-surface: oklch(100% 0 0);
--c-subtle: oklch(96.7% 0.001 286.375);
--c-line: oklch(92% 0.004 286.32);
--c-fg: oklch(21% 0.006 285.885);
--c-muted: oklch(55.2% 0.016 285.938);
--c-bubble: oklch(100% 0 0);
}
.dark {
color-scheme: dark;
--c-canvas: oklch(14.1% 0.005 285.823);
--c-surface: oklch(21% 0.006 285.885);
--c-subtle: oklch(27.4% 0.006 286.033);
--c-line: oklch(27.4% 0.006 286.033);
--c-fg: oklch(96.7% 0.001 286.375);
--c-muted: oklch(70.5% 0.015 286.067);
--c-bubble: oklch(27.4% 0.006 286.033);
}
@layer base {
html,
body {
height: 100%;
}
body {
@apply bg-canvas text-fg antialiased;
-webkit-tap-highlight-color: transparent;
}
:focus-visible {
@apply outline-2 outline-offset-2 outline-brand-500;
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
}
@utility btn {
@apply inline-flex min-h-10 items-center justify-center gap-2 rounded-lg px-4 text-sm font-medium transition-colors select-none disabled:cursor-not-allowed disabled:opacity-50;
}
@utility btn-primary {
@apply btn bg-brand-600 text-white hover:bg-brand-700 active:bg-brand-800;
}
@utility btn-secondary {
@apply btn border border-line bg-surface text-fg hover:bg-subtle;
}
@utility btn-ghost {
@apply btn text-fg hover:bg-subtle;
}
@utility btn-danger {
@apply btn bg-red-600 text-white hover:bg-red-700;
}
@utility icon-btn {
@apply inline-flex size-10 shrink-0 items-center justify-center rounded-full text-muted transition-colors hover:bg-subtle hover:text-fg disabled:opacity-40;
}
@utility input {
/* 16px text on phones stops iOS Safari from zooming into focused fields. */
@apply block w-full rounded-lg border border-line bg-surface px-3 py-2 text-base text-fg placeholder:text-muted focus:border-brand-500 focus:ring-2 focus:ring-brand-500/30 focus:outline-none sm:text-sm;
}
@utility input-invalid {
@apply border-red-500 focus:border-red-500 focus:ring-red-500/30;
}
@utility label {
@apply mb-1 block text-sm font-medium text-fg;
}
@utility field-error {
@apply mt-1 text-xs text-red-600 dark:text-red-400;
}
@utility card {
@apply rounded-2xl border border-line bg-surface;
}
@utility badge {
@apply inline-flex min-w-5 items-center justify-center rounded-full px-1.5 text-[11px] leading-5 font-semibold;
}
@utility menu {
@apply min-w-44 overflow-hidden rounded-xl border border-line bg-surface py-1 shadow-lg;
}
@utility menu-item {
@apply flex w-full items-center gap-3 px-3 py-2 text-left text-sm text-fg hover:bg-subtle focus:bg-subtle focus:outline-none;
}
.cdk-overlay-dark-backdrop {
background: rgb(0 0 0 / 0.45);
}
.pt-safe {
padding-top: env(safe-area-inset-top);
}
.pb-safe {
padding-bottom: env(safe-area-inset-bottom);
}
@utility dialog-card {
@apply max-h-[calc(100dvh-2rem)] w-[min(32rem,calc(100vw-1.5rem))] overflow-y-auto rounded-2xl border border-line bg-surface text-fg shadow-xl;
}
/* CDK dialog panes: let the card decide its width. */
.app-dialog-panel {
max-width: 100vw;
}From top to bottom:
@import 'tailwindcss'pulls in Tailwind: its reset (preflight), its theme and the generator of utilities.@custom-variant darkredefines thedark:prefix. By defaultdark:follows the operating system (prefers-color-scheme); here it applies inside an element with the classdark. The user can then choose light, dark or system in the settings (topic 21), andThemeService(the next chapter) puts the class on<html>.@themeadds design tokens. Every--color-brand-*variable creates a whole family of utilities:bg-brand-600,text-brand-700,ring-brand-500/30and so on. The values are OKLCH colours (lightness, chroma, hue), the colour space of Tailwind v4's own palette; the brand colour is an indigo.--font-sanssets the default font stack.@theme inlineadds the semantic tokenscanvas,surface,subtle,line,fg,mutedandbubble. Their values are CSS variables (--c-*) defined twice: in:rootfor the light theme and in.darkfor the dark one. A template writesbg-surface text-fgonce and gets the right colours in both themes, without adark:variant on every element.inlinemakes the utilities usevar(--c-surface)directly, so they follow the variable when the classdarkappears.color-schemetells the browser to draw its own controls (scroll bars, date pickers) in the matching theme.@layer basestyles the page itself: full height, the canvas colour, a visible focus outline for keyboard users, and almost no animation for users who asked their system for reduced motion.@utilitydefines our own utilities from existing ones with@apply:btn,btn-primary,btn-secondary,btn-ghost,btn-danger,icon-btn,input,input-invalid,label,field-error,card,badge,menu,menu-itemanddialog-card. They behave like Tailwind's own utilities: they work with variants (hover:,md:), and Prettier's Tailwind plugin knows them, which is why.prettierrcpoints to this file.inputuses 16 px text on phones (text-base, thensm:text-sm): iOS Safari zooms into any focused field with a smaller font.- Plain rules at the end style elements that Angular CDK creates (the dialog backdrop and panel, topic 17) and add
pt-safe/pb-safe, paddings for the notch and the home indicator of phones (env(safe-area-inset-*)).
Check
powershell
npm run buildsucceeds, and a line such as styles-EJGLXUFW.css | styles | 10.44 kB shows the generated stylesheet (the name contains a hash of the content, and the size depends on the classes the templates use). If you mistype a utility inside @apply, the build fails with a message such as Cannot apply unknown utility class `min-h-10` .
Step 3. The page, its icons and the theme script
Why. index.html is the only real HTML page; Angular renders everything else into its <app-root> element. Three details need care:
- Icons. Browsers and phones ask for several icon formats: an
.icofor old browsers, an SVG that can adapt to the dark theme, a PNG for the iOS home screen, and PNG icons of the web app manifest for "Add to home screen" on Android. - The theme before the first paint. Angular starts only after its JavaScript bundle has loaded. If the dark theme were applied by Angular, a user with the dark theme would see a white flash on every load. A tiny script in
<head>must set the classdarkbefore the page is drawn. - No inline scripts. That tiny script cannot be written inside
index.html: the production Content-Security-Policyscript-src 'self'(topic 24) blocks inline scripts, which is the main defence against cross-site scripting. So it is a file of its own inpublic/.
Do. Copy the icons from the reference — they are binary files — replacing the generated favicon.ico:
powershell
Copy-Item D:\Work\Chatex-Reference\Source\Chatex.Web\public\*.png, D:\Work\Chatex-Reference\Source\Chatex.Web\public\favicon.ico public -ForceCreate the vector icon, the manifest and the theme script:
svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
<style>
@media (prefers-color-scheme: dark) {
.tile { fill: #615fff; }
}
</style>
<rect class="tile" width="32" height="32" rx="8" fill="#4f39f6" />
<path d="M21.66 9.84A8 8 0 1 0 21.66 21.16" fill="none" stroke="#fff" stroke-width="4" stroke-linecap="round" />
<path d="M9.6 19.2 7.5 27l7.6-3.4z" fill="#fff" />
</svg>json
{
"name": "Chatex",
"short_name": "Chatex",
"description": "Chatex — team chat and messenger",
"start_url": "/",
"scope": "/",
"display": "standalone",
"theme_color": "#4f46e5",
"background_color": "#ffffff",
"icons": [
{ "src": "icon-192.png", "sizes": "192x192", "type": "image/png", "purpose": "any" },
{ "src": "icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "any" },
{
"src": "icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}js
// Apply the saved theme before the first paint so dark mode doesn't flash white.
// A file rather than an inline script, so the Content-Security-Policy can stay script-src 'self'.
(function () {
try {
var t = localStorage.getItem('chatex.theme') || 'system';
var dark =
t === 'dark' || (t === 'system' && matchMedia('(prefers-color-scheme: dark)').matches);
document.documentElement.classList.toggle('dark', dark);
} catch (e) {}
})();favicon.svgcontains its own media query: in a dark browser the tile is a lighter indigo.site.webmanifestnames the app, setsdisplay: standalone(no browser bar when started from the home screen) and lists the icons; the maskable icon has a safe zone so Android can cut it into a circle or a squircle.theme-init.jsreads the saved choice under the keychatex.theme— the same keyThemeServicewrites — and toggles the classdark. It is plain JavaScript in an immediately invoked function, so its variables do not leak into the page, andtry/catchcovers browsers wherelocalStorageis blocked.
Everything in public/ is copied unchanged into the output (the assets option of angular.json) and served from the site root.
Replace src/index.html:
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Chatex</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="description" content="Chatex — team chat and messenger" />
<meta name="theme-color" content="#4f46e5" />
<link rel="icon" href="favicon.ico" sizes="48x48" />
<link rel="icon" href="favicon.svg" type="image/svg+xml" />
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
<link rel="manifest" href="site.webmanifest" />
<script src="theme-init.js"></script>
</head>
<body>
<app-root></app-root>
<noscript>Chatex needs JavaScript. Please enable it in your browser.</noscript>
</body>
</html>viewport-fit=coverlets the page extend under the notch;pt-safeandpb-safethen add the right paddings.theme-colorcolours the browser bar on phones;ThemeServiceswitches it to dark grey in the dark theme.<script src="theme-init.js">in<head>runs before the body is drawn. It is a classic blocking script on purpose: it is a few hundred bytes and must finish before the first paint.<noscript>explains the empty page to a browser without JavaScript.
Check
powershell
npm run buildsucceeds, and dist/chatex/browser now contains theme-init.js, site.webmanifest and the icons next to index.html.