# Angular Components Examples
## Introduction
This project provides a comprehensive collection of working examples for Angular Material components, Angular CDK (Component Dev Kit), and experimental features. It serves as the official example library for the Angular Components project, offering developers ready-to-use code snippets demonstrating proper implementation patterns for Material Design UI components in Angular applications. The package contains hundreds of standalone component examples that showcase everything from basic buttons and inputs to complex data tables, drag-and-drop interfaces, and accessibility features.
The examples are designed to be directly importable and runnable, making them ideal for rapid prototyping, learning Angular Material patterns, and understanding best practices for component integration. Each example is self-contained with its TypeScript component, HTML template, and CSS styling, compiled as ES2022 modules with full TypeScript type definitions. The package supports modular imports, allowing developers to load specific examples without bundling the entire library, and includes examples for Material components, CDK utilities, ARIA-compliant components, and experimental features under active development.
## Key Functions and APIs
### Loading Examples Dynamically
```typescript
import { loadExample, EXAMPLE_COMPONENTS } from '@angular/components-examples';
// Get metadata for a specific example
const buttonExample = EXAMPLE_COMPONENTS['button-overview'];
console.log(buttonExample.title); // "Button overview"
console.log(buttonExample.componentName); // "ButtonOverviewExample"
console.log(buttonExample.files); // Array of file paths
// Dynamically load an example component
const exampleModule = await loadExample('button-overview');
// Returns the module containing ButtonOverviewExample component
// Iterate through all available examples
Object.keys(EXAMPLE_COMPONENTS).forEach(exampleId => {
const example = EXAMPLE_COMPONENTS[exampleId];
console.log(`${exampleId}: ${example.title}`);
});
```
### Material Button Component
```typescript
import { Component } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
@Component({
selector: 'app-button-demo',
template: `
Link Button
`,
imports: [MatButtonModule, MatIconModule],
})
export class ButtonDemoComponent {}
```
### Material Table Component
```typescript
import { Component } from '@angular/core';
import { MatTableModule } from '@angular/material/table';
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{ position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
{ position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
{ position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
{ position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
];
@Component({
selector: 'app-table-demo',
template: `
No.
{{element.position}}
Name
{{element.name}}
Weight
{{element.weight}}
Symbol
{{element.symbol}}
`,
imports: [MatTableModule],
})
export class TableDemoComponent {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
}
```
### Material Dialog Component
```typescript
import { Component, inject, model, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import {
MAT_DIALOG_DATA,
MatDialog,
MatDialogActions,
MatDialogClose,
MatDialogContent,
MatDialogRef,
MatDialogTitle,
} from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
export interface DialogData {
animal: string;
name: string;
}
@Component({
selector: 'app-dialog-demo',
template: `
Name
`,
})
export class FocusMonitorDemoComponent implements AfterViewInit, OnDestroy {
private focusMonitor = inject(FocusMonitor);
@ViewChild('monitoredButton') monitoredButton: ElementRef;
@ViewChild('subtreeElement') subtreeElement: ElementRef;
elementOrigin = 'none';
subtreeOrigin = 'none';
ngAfterViewInit() {
// Monitor focus on a specific element
this.focusMonitor.monitor(this.monitoredButton).subscribe(origin => {
this.elementOrigin = this.formatOrigin(origin);
});
// Monitor focus within a subtree
this.focusMonitor.monitor(this.subtreeElement, true).subscribe(origin => {
this.subtreeOrigin = this.formatOrigin(origin);
});
}
ngOnDestroy() {
this.focusMonitor.stopMonitoring(this.monitoredButton);
this.focusMonitor.stopMonitoring(this.subtreeElement);
}
formatOrigin(origin: FocusOrigin): string {
return origin ? `${origin} focused` : 'blurred';
}
}
```
## Use Cases and Integration
This package is primarily used as a reference library for Angular developers building applications with Angular Material. Developers can import individual examples into their documentation sites, use them as starting points for custom components, or study them to understand proper implementation patterns. The examples are particularly valuable for teams adopting Material Design principles, as they demonstrate not just component usage but also proper accessibility practices, responsive design patterns, and integration with Angular's reactive forms system.
For integration into applications, developers can either copy code directly from the example source files or use the dynamic loading API to embed live examples in documentation sites. The modular structure allows selective imports, keeping bundle sizes small. Common integration patterns include using examples in component galleries, interactive documentation systems, prototyping tools, and educational materials. The package also serves as a test bed for the Angular Material library itself, with each example demonstrating stable, production-ready implementations that have been thoroughly tested across different Angular versions and browser environments.