import { Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { FeedLayoutComponent, FeedItem } from 'ui-essentials';
interface DemoFeedItem extends FeedItem {
title: string;
content: string;
timestamp: Date;
author: string;
likes: number;
type: 'text' | 'image' | 'video';
}
@Component({
selector: 'ui-feed-layout-demo',
standalone: true,
imports: [CommonModule, FormsModule, FeedLayoutComponent],
template: `
Feed Layout Demo
Sizes
@for (size of sizes; track size) {
{{ size | titlecase }}
@for (item of sampleItems.slice(0, 2); track item.id) {
{{ item.title }}
{{ item.content }}
}
}
Interactive Feed with Infinite Scroll
@for (item of feedItems(); track item.id) {
{{ item.title }}
{{ item.content }}
}
📝
No posts yet
Be the first to share something!
States
Loading State
@for (item of sampleItems.slice(0, 1); track item.id) {
{{ item.title }}
{{ item.content }}
}
Error State
Usage Statistics
Total Items:
{{ feedItems().length }}
Load More Calls:
{{ loadMoreCount }}
Refresh Calls:
{{ refreshCount }}
`,
styleUrl: './feed-layout-demo.component.scss'
})
export class FeedLayoutDemoComponent {
sizes = ['sm', 'md', 'lg'] as const;
protected readonly feedItems = signal([]);
protected readonly isLoading = signal(false);
protected readonly hasError = signal(false);
protected errorMessage = '';
protected enableRefreshControl = true;
protected loadMoreCount = 0;
protected refreshCount = 0;
protected sampleItems: DemoFeedItem[] = [
{
id: '1',
title: 'Getting Started with Angular 19',
content: 'Angular 19 brings exciting new features including improved SSR, better performance, and enhanced developer experience.',
timestamp: new Date(Date.now() - 3600000),
author: 'Sarah Chen',
likes: 42,
type: 'text'
},
{
id: '2',
title: 'Building Responsive Layouts',
content: 'Learn how to create flexible, mobile-first designs that work beautifully across all devices.',
timestamp: new Date(Date.now() - 7200000),
author: 'Mike Rodriguez',
likes: 28,
type: 'image'
},
{
id: '3',
title: 'TypeScript Best Practices',
content: 'Discover advanced TypeScript patterns and techniques to write more maintainable code.',
timestamp: new Date(Date.now() - 10800000),
author: 'Alex Kim',
likes: 35,
type: 'video'
}
];
constructor() {
this.addSampleContent();
}
loadMoreItems(): void {
if (this.isLoading() || this.hasError()) return;
this.loadMoreCount++;
this.isLoading.set(true);
// Simulate API call delay
setTimeout(() => {
const currentItems = this.feedItems();
const nextBatch = this.generateItems(3, currentItems.length);
this.feedItems.set([...currentItems, ...nextBatch]);
this.isLoading.set(false);
}, 1500);
}
refreshFeed(): void {
this.refreshCount++;
this.isLoading.set(true);
this.hasError.set(false);
// Simulate refresh delay
setTimeout(() => {
const newItems = this.generateItems(5, 0);
this.feedItems.set(newItems);
this.isLoading.set(false);
}, 1000);
}
retryLoad(): void {
this.hasError.set(false);
this.loadMoreItems();
}
resetFeed(): void {
this.feedItems.set([]);
this.hasError.set(false);
this.isLoading.set(false);
this.loadMoreCount = 0;
this.refreshCount = 0;
this.addSampleContent();
}
toggleError(): void {
if (this.hasError()) {
this.hasError.set(false);
this.errorMessage = '';
} else {
this.hasError.set(true);
this.errorMessage = 'Failed to load more content. Please check your connection.';
}
}
toggleLike(item: DemoFeedItem): void {
const items = this.feedItems();
const index = items.findIndex(i => i.id === item.id);
if (index !== -1) {
const updatedItems = [...items];
updatedItems[index] = { ...item, likes: item.likes > 0 ? 0 : 1 };
this.feedItems.set(updatedItems);
}
}
addSampleContent(): void {
const items = this.generateItems(5, 0);
this.feedItems.set(items);
}
private generateItems(count: number, startId: number): DemoFeedItem[] {
const contentTemplates = [
{
title: 'Web Development Trends',
content: 'Exploring the latest trends in modern web development and what they mean for developers.',
author: 'Jane Doe',
type: 'text' as const
},
{
title: 'UI/UX Design Principles',
content: 'Understanding the fundamental principles that make great user interfaces and experiences.',
author: 'John Smith',
type: 'image' as const
},
{
title: 'Performance Optimization',
content: 'Tips and techniques for optimizing web application performance and user experience.',
author: 'Emma Wilson',
type: 'video' as const
},
{
title: 'Accessibility Matters',
content: 'Why web accessibility is crucial and how to implement it in your projects.',
author: 'David Brown',
type: 'text' as const
},
{
title: 'Mobile-First Design',
content: 'Best practices for designing mobile-first responsive web applications.',
author: 'Lisa Garcia',
type: 'image' as const
}
];
return Array.from({ length: count }, (_, index) => {
const template = contentTemplates[index % contentTemplates.length];
const id = startId + index + 1;
return {
id: id.toString(),
title: `${template.title} #${id}`,
content: template.content,
timestamp: new Date(Date.now() - (index + 1) * 1800000),
author: template.author,
likes: Math.floor(Math.random() * 50),
type: template.type
};
});
}
}