Kurs Angular · Moduł 13: UI i stylowanie
Angular Material
W tej lekcji3
Angular Material to oficjalna biblioteka komponentów UI - jak zestaw ceremoniałny do herbaty.
Instalacja
ng add @angular/material
# Wybierz:
# - Pre-built theme (np. Indigo/Pink)
# - Globalne style typography
# - Animacje BrowserAnimationsModulePodstawowe Komponenty
import { Component } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatMenuModule } from '@angular/material/menu';
@Component({
selector: 'app-dojo-dashboard',
standalone: true,
imports: [
MatButtonModule,
MatCardModule,
MatIconModule,
MatInputModule,
MatFormFieldModule,
MatToolbarModule,
MatMenuModule
],
template: `
<!-- Toolbar -->
<mat-toolbar color="primary">
<span>Dojo Dashboard</span>
<span class="spacer"></span>
<button mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item>Ustawienia</button>
<button mat-menu-item>Wyloguj</button>
</mat-menu>
</mat-toolbar>
<!-- Karty -->
<mat-card>
<mat-card-header>
<mat-card-title>Profil Samuraja</mat-card-title>
<mat-card-subtitle>Klan: Tokugawa</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<mat-form-field appearance="outline">
<mat-label>Imię</mat-label>
<input matInput placeholder="Wpisz imię">
<mat-icon matPrefix>person</mat-icon>
</mat-form-field>
</mat-card-content>
<mat-card-actions align="end">
<button mat-button>Anuluj</button>
<button mat-raised-button color="primary">Zapisz</button>
</mat-card-actions>
</mat-card>
`,
styles: [`
.spacer {
flex: 1 1 auto;
}
mat-card {
max-width: 400px;
margin: 2rem auto;
}
`]
})
export class DojoDashboardComponent {}Własny Theme Material
// custom-theme.scss
@use '@angular/material' as mat;
// Definiujemy kolory
$samurai-primary: mat.m2-define-palette((
50: #fce4ec,
100: #f8bbd9,
500: #c41e3a, // główny
700: #ad1457,
contrast: (
50: rgba(black, 0.87),
500: white,
)
));
$samurai-accent: mat.m2-define-palette(mat.$m2-amber-palette);
// Tworzymy theme
$samurai-theme: mat.m2-define-light-theme((
color: (
primary: $samurai-primary,
accent: $samurai-accent,
),
typography: mat.m2-define-typography-config(),
density: 0,
));
// Aplikujemy theme
@include mat.all-component-themes($samurai-theme);Kod do tej lekcji: src/app/app.component.ts
import { Component, signal, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
encapsulation: ViewEncapsulation.Emulated, // Domyślna enkapsulacja
template: `
<div class="styling-guide">
<h1>Stylowanie w Angular</h1>
<section>
<h2>ViewEncapsulation</h2>
<div class="encapsulation-types">
<div class="type emulated">
<h3>Emulated (domyślna)</h3>
<p>Angular dodaje unikalne atrybuty do selektorów CSS</p>
<code>[_ngcontent-xxx]</code>
</div>
<div class="type shadow">
<h3>ShadowDom</h3>
<p>Używa prawdziwego Shadow DOM</p>
<code>encapsulation: ViewEncapsulation.ShadowDom</code>
</div>
<div class="type none">
<h3>None</h3>
<p>Style globalne - bez enkapsulacji</p>
<code>encapsulation: ViewEncapsulation.None</code>
</div>
</div>
</section>
<section>
<h2>Specjalne selektory</h2>
<div class="selectors-demo">
<div class="selector">
<h3>:host</h3>
<code>:host { display: block; }</code>
<p>Styluje element główny komponentu</p>
</div>
<div class="selector">
<h3>:host-context()</h3>
<code>:host-context(.dark-theme) { ... }</code>
<p>Style zależne od kontekstu rodzica</p>
</div>
<div class="selector">
<h3>::ng-deep</h3>
<code>::ng-deep .child { ... }</code>
<p>Przenika przez enkapsulację (deprecated)</p>
</div>
</div>
</section>
<section>
<h2>CSS Variables (Custom Properties)</h2>
<div class="css-vars-demo">
<div class="controls">
<label>
Kolor główny:
<input type="color" [value]="primaryColor()" (input)="onColorChange($event)">
</label>
<label>
Zaokrąglenie:
<input type="range" min="0" max="30" [value]="borderRadius()" (input)="onRadiusChange($event)">
{{ borderRadius() }}px
</label>
</div>
<div class="preview-box"
[style.--primary-color]="primaryColor()"
[style.--border-radius]="borderRadius() + 'px'">
<h3>Podgląd</h3>
<button>Przycisk</button>
<div class="card">Karta</div>
</div>
</div>
</section>
<section>
<h2>Binding stylów</h2>
<pre><code>{{ stylesCode }}</code></pre>
</section>
<section>
<h2>Tailwind-like utility classes</h2>
<div class="utilities-demo">
<div class="utility-box p-4 m-2 rounded bg-primary">
p-4 m-2 rounded bg-primary
</div>
<div class="utility-box p-2 m-4 rounded-lg bg-secondary">
p-2 m-4 rounded-lg bg-secondary
</div>
<div class="utility-box p-6 m-1 rounded-full bg-accent">
p-6 m-1 rounded-full bg-accent
</div>
</div>
</section>
</div>
`,
styles: [`
:host {
display: block;
}
.styling-guide {
font-family: 'Segoe UI', sans-serif;
padding: 20px;
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
min-height: 100vh;
color: #ecf0f1;
}
h1 {
text-align: center;
color: #3498db;
}
h2 {
color: #3498db;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
section {
background: rgba(255,255,255,0.05);
border-radius: 10px;
padding: 20px;
margin: 20px 0;
}
.encapsulation-types {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
}
.type {
padding: 15px;
border-radius: 8px;
}
.type.emulated {
background: rgba(46, 204, 113, 0.2);
border: 2px solid #2ecc71;
}
.type.shadow {
background: rgba(52, 152, 219, 0.2);
border: 2px solid #3498db;
}
.type.none {
background: rgba(231, 76, 60, 0.2);
border: 2px solid #e74c3c;
}
.type h3 {
margin-top: 0;
}
.type code {
display: block;
background: rgba(0,0,0,0.3);
padding: 8px;
border-radius: 4px;
font-size: 0.85em;
}
.selectors-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
}
.selector {
background: rgba(0,0,0,0.3);
padding: 15px;
border-radius: 8px;
}
.selector h3 {
margin-top: 0;
color: #3498db;
}
.selector code {
display: block;
background: rgba(52, 152, 219, 0.2);
padding: 8px;
border-radius: 4px;
margin-bottom: 10px;
}
.css-vars-demo .controls {
display: flex;
gap: 20px;
flex-wrap: wrap;
margin-bottom: 20px;
}
.css-vars-demo label {
display: flex;
align-items: center;
gap: 10px;
}
.preview-box {
--primary-color: #3498db;
--border-radius: 8px;
background: rgba(0,0,0,0.3);
padding: 20px;
border-radius: 10px;
}
.preview-box h3 {
color: var(--primary-color);
}
.preview-box button {
background: var(--primary-color);
color: white;
border: none;
padding: 10px 20px;
border-radius: var(--border-radius);
cursor: pointer;
margin-right: 10px;
}
.preview-box .card {
display: inline-block;
background: var(--primary-color);
color: white;
padding: 15px 25px;
border-radius: var(--border-radius);
}
pre {
background: #1a252f;
padding: 15px;
border-radius: 8px;
overflow-x: auto;
border-left: 4px solid #3498db;
}
code {
color: #ecf0f1;
font-family: 'Fira Code', monospace;
font-size: 0.85em;
}
/* Utility classes */
.utilities-demo {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.utility-box {
color: white;
font-size: 0.85em;
}
.p-2 { padding: 8px; }
.p-4 { padding: 16px; }
.p-6 { padding: 24px; }
.m-1 { margin: 4px; }
.m-2 { margin: 8px; }
.m-4 { margin: 16px; }
.rounded { border-radius: 4px; }
.rounded-lg { border-radius: 8px; }
.rounded-full { border-radius: 50px; }
.bg-primary { background: #3498db; }
.bg-secondary { background: #2ecc71; }
.bg-accent { background: #9b59b6; }
`]
})
export class AppComponent {
primaryColor = signal('#3498db');
borderRadius = signal(8);
stylesCode = `// Binding pojedynczego stylu
<div [style.color]="textColor">Text</div>
<div [style.font-size.px]="fontSize">Text</div>
// Binding wielu stylów
<div [ngStyle]="{
'color': textColor,
'font-size.px': fontSize,
'background': isActive ? 'green' : 'red'
}">Text</div>
// Binding klas
<div [class.active]="isActive">...</div>
<div [class]="dynamicClass">...</div>
// NgClass dla wielu klas
<div [ngClass]="{
'active': isActive,
'disabled': isDisabled,
'highlight': isHighlighted
}">...</div>
// CSS Variables w Angular
<div [style.--primary-color]="primaryColor">
<button>Uses --primary-color</button>
</div>`;
onColorChange(event: Event): void {
const input = event.target as HTMLInputElement;
this.primaryColor.set(input.value);
}
onRadiusChange(event: Event): void {
const input = event.target as HTMLInputElement;
this.borderRadius.set(+input.value);
}
}Widzisz błąd w tej lekcji?
Sprawdź się
Odpowiedz na pytania z tej lekcji. Wybierz odpowiedź, a od razu zobaczysz, czy jest poprawna.
1. Do czego służy selektor :host w stylach komponentu Angular?
2. Jakim poleceniem dodaje się Angular Material do projektu (konfiguruje theme i animacje)?
To 2 z 3 pytań do tej lekcji. Pozostałe rozwiążesz w grze.
Zadania praktyczne w grze
- Układanie w poziomie
Ułóż w jednej linii import modułu przycisków Angular Material.
- Edytor kodu
Uzupełnij formularz używający Angular Material.
- Układanie w pionie
Uporządkuj elementy karty Material w kolejności występowania w szablonie (od góry do dołu).