-
-
Notifications
You must be signed in to change notification settings - Fork 452
Angular chat & Angular 20 #1228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benjavicente
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
benjavicente:angular-chat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/angular-virtual': minor | ||
| --- | ||
|
|
||
| require angular 20 and up following their lts | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # TanStack Virtual Angular Chat Example | ||
|
|
||
| Demonstrates end-anchored virtualization for chat-style UIs: | ||
|
|
||
| - starts at the newest message | ||
| - keeps the visible message stable when older history is prepended | ||
| - follows appended messages only while the reader is already at the end | ||
| - remains pinned while a dynamically measured reply streams in | ||
|
|
||
| Run it from the repository root with: | ||
|
|
||
| ```sh | ||
| pnpm --filter @tanstack/virtual-example-angular-chat start | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
| "version": 1, | ||
| "cli": { | ||
| "packageManager": "pnpm", | ||
| "analytics": false, | ||
| "cache": { "enabled": false } | ||
| }, | ||
| "projects": { | ||
| "@tanstack/virtual-example-angular-chat": { | ||
| "projectType": "application", | ||
| "root": "", | ||
| "sourceRoot": "src", | ||
| "prefix": "app", | ||
| "architect": { | ||
| "build": { | ||
| "builder": "@angular-devkit/build-angular:application", | ||
| "options": { | ||
| "outputPath": "dist/tanstack/virtual-example-angular-chat", | ||
| "index": "src/index.html", | ||
| "browser": "src/main.ts", | ||
| "polyfills": ["zone.js"], | ||
| "tsConfig": "tsconfig.app.json", | ||
| "assets": [], | ||
| "styles": ["src/styles.css"], | ||
| "scripts": [] | ||
| }, | ||
| "configurations": { | ||
| "production": { "outputHashing": "all" }, | ||
| "development": { | ||
| "optimization": false, | ||
| "extractLicenses": false, | ||
| "sourceMap": true | ||
| } | ||
| }, | ||
| "defaultConfiguration": "production" | ||
| }, | ||
| "serve": { | ||
| "builder": "@angular-devkit/build-angular:dev-server", | ||
| "configurations": { | ||
| "production": { | ||
| "buildTarget": "@tanstack/virtual-example-angular-chat:build:production" | ||
| }, | ||
| "development": { | ||
| "buildTarget": "@tanstack/virtual-example-angular-chat:build:development" | ||
| } | ||
| }, | ||
| "defaultConfiguration": "development" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "name": "@tanstack/virtual-example-angular-chat", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "ng": "ng", | ||
| "start": "ng serve", | ||
| "build": "ng build", | ||
| "watch": "ng build --watch --configuration development" | ||
| }, | ||
| "dependencies": { | ||
| "@angular/animations": "^20.0.0", | ||
| "@angular/common": "^20.0.0", | ||
| "@angular/compiler": "^20.0.0", | ||
| "@angular/core": "^20.0.0", | ||
| "@angular/forms": "^20.0.0", | ||
| "@angular/platform-browser": "^20.0.0", | ||
| "@angular/platform-browser-dynamic": "^20.0.0", | ||
| "@angular/router": "^20.0.0", | ||
| "@tanstack/angular-virtual": "^5.0.8", | ||
| "rxjs": "^7.8.2", | ||
| "tslib": "^2.8.1", | ||
| "zone.js": "0.15.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@angular-devkit/build-angular": "^20.0.0", | ||
| "@angular/cli": "^20.0.0", | ||
| "@angular/compiler-cli": "^20.0.0", | ||
| "typescript": "5.9.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| DestroyRef, | ||
| afterNextRender, | ||
| afterRenderEffect, | ||
| inject, | ||
| signal, | ||
| viewChild, | ||
| viewChildren, | ||
| } from '@angular/core' | ||
| import { injectVirtualizer } from '@tanstack/angular-virtual' | ||
| import type { ElementRef } from '@angular/core' | ||
|
|
||
| type Message = { | ||
| id: string | ||
| author: 'user' | 'assistant' | ||
| text: string | ||
| } | ||
|
|
||
| const replies = [ | ||
| 'I can break that into the smallest next step and keep the current viewport pinned while this answer grows.', | ||
| 'Older messages are loaded above the viewport. The visible row keeps the same screen position after the prepend.', | ||
| 'When the thread is not at the bottom, new output waits below without pulling the reader away from history.', | ||
| ] | ||
|
|
||
| const makeMessage = (index: number): Message => ({ | ||
| id: `message-${index}`, | ||
| author: index % 4 === 0 ? 'user' : 'assistant', | ||
| text: | ||
| index % 4 === 0 | ||
| ? `Can you check item ${index}?` | ||
| : `Message ${index}: ${replies[Math.abs(index) % replies.length]}`, | ||
| }) | ||
|
|
||
| const initialMessages = Array.from({ length: 45 }, (_, index) => | ||
| makeMessage(index), | ||
| ) | ||
|
|
||
| @Component({ | ||
| selector: 'app-root', | ||
| standalone: true, | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| template: ` | ||
| <div class="App"> | ||
| <div class="Toolbar"> | ||
| <div class="ToolbarGroup"> | ||
| <button type="button" (click)="prependHistory()">Load older</button> | ||
| <button type="button" (click)="appendMessage()">Add message</button> | ||
| <button type="button" (click)="streamReply()">Stream reply</button> | ||
| <button type="button" (click)="virtualizer.scrollToEnd()"> | ||
| Latest | ||
| </button> | ||
| </div> | ||
| <div class="Status"> | ||
| @if (loadingHistory()) { | ||
| Loading history | ||
| } @else if (virtualizer.isAtEnd(80)) { | ||
| At latest | ||
| } @else { | ||
| Reading history | ||
| } | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="Shell"> | ||
| <div #scrollElement class="Messages" (scroll)="onScroll()"> | ||
| <div | ||
| class="MessagesSizer" | ||
| [style.height.px]="virtualizer.getTotalSize()" | ||
| > | ||
| @for (row of virtualizer.getVirtualItems(); track row.key) { | ||
| <div | ||
| #virtualItem | ||
| class="MessageRow" | ||
| [attr.data-index]="row.index" | ||
| [style.transform]="'translateY(' + row.start + 'px)'" | ||
| > | ||
| <div | ||
| class="Bubble" | ||
| [class.Bubble-user]="messages()[row.index].author === 'user'" | ||
| [class.Bubble-assistant]=" | ||
| messages()[row.index].author === 'assistant' | ||
| " | ||
| > | ||
| <div class="Meta">{{ messages()[row.index].author }}</div> | ||
| {{ messages()[row.index].text || '...' }} | ||
| </div> | ||
| </div> | ||
| } | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `, | ||
| }) | ||
| export class AppComponent { | ||
| readonly messages = signal(initialMessages) | ||
| readonly loadingHistory = signal(false) | ||
|
|
||
| readonly scrollElement = | ||
| viewChild<ElementRef<HTMLDivElement>>('scrollElement') | ||
| readonly virtualItems = | ||
| viewChildren<ElementRef<HTMLDivElement>>('virtualItem') | ||
|
|
||
| private firstMessageIndex = 0 | ||
| private nextMessageIndex = initialMessages.length | ||
| private autoHistoryTimer: number | undefined | ||
| private streamTimer: number | undefined | ||
| private autoHistoryEnabled = false | ||
|
|
||
| readonly virtualizer = injectVirtualizer<HTMLDivElement, HTMLDivElement>( | ||
| () => ({ | ||
| scrollElement: this.scrollElement(), | ||
| count: this.messages().length, | ||
| estimateSize: () => 74, | ||
| getItemKey: (index) => this.messages()[index].id, | ||
| anchorTo: 'end', | ||
| followOnAppend: true, | ||
| scrollEndThreshold: 80, | ||
| overscan: 6, | ||
| useApplicationRefTick: false, | ||
| }), | ||
| ) | ||
|
|
||
| private readonly measureItems = afterRenderEffect({ | ||
| read: () => { | ||
| this.virtualItems().forEach((element) => { | ||
| this.virtualizer.measureElement(element.nativeElement) | ||
| }) | ||
| }, | ||
| }) | ||
|
|
||
| constructor() { | ||
| const destroyRef = inject(DestroyRef) | ||
|
|
||
| afterNextRender(() => { | ||
| this.virtualizer.scrollToEnd() | ||
| this.autoHistoryTimer = window.setTimeout(() => { | ||
| this.autoHistoryEnabled = true | ||
| }, 250) | ||
| }) | ||
|
|
||
| destroyRef.onDestroy(() => { | ||
| if (this.autoHistoryTimer !== undefined) { | ||
| window.clearTimeout(this.autoHistoryTimer) | ||
| } | ||
| if (this.streamTimer !== undefined) { | ||
| window.clearInterval(this.streamTimer) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| prependHistory(): void { | ||
| if (this.loadingHistory() || this.firstMessageIndex <= -90) return | ||
|
|
||
| this.loadingHistory.set(true) | ||
| window.setTimeout(() => { | ||
| const start = this.firstMessageIndex - 12 | ||
| this.firstMessageIndex = start | ||
| this.messages.update((current) => [ | ||
| ...Array.from({ length: 12 }, (_, offset) => | ||
| makeMessage(start + offset), | ||
| ), | ||
| ...current, | ||
| ]) | ||
| this.loadingHistory.set(false) | ||
| }, 180) | ||
| } | ||
|
|
||
| appendMessage(): void { | ||
| const next = this.nextMessageIndex | ||
| this.nextMessageIndex += 1 | ||
| this.messages.update((current) => [...current, makeMessage(next)]) | ||
| } | ||
|
|
||
| streamReply(): void { | ||
| if (this.streamTimer !== undefined) return | ||
|
|
||
| const id = `stream-${Date.now()}` | ||
| const chunks = [ | ||
| 'Thinking through the failure mode.', | ||
| ' The list should follow only when it was already pinned.', | ||
| ' Prepends should keep the reader anchored to the same message.', | ||
| ' Streaming output should grow without drifting off the bottom.', | ||
| ] | ||
| let chunkIndex = 0 | ||
|
|
||
| this.messages.update((current) => [ | ||
| ...current, | ||
| { id, author: 'assistant', text: '' }, | ||
| ]) | ||
|
|
||
| this.streamTimer = window.setInterval(() => { | ||
| this.messages.update((current) => | ||
| current.map((message) => | ||
| message.id === id | ||
| ? { | ||
| ...message, | ||
| text: chunks.slice(0, chunkIndex + 1).join(''), | ||
| } | ||
| : message, | ||
| ), | ||
| ) | ||
|
|
||
| chunkIndex += 1 | ||
| if (chunkIndex === chunks.length) { | ||
| window.clearInterval(this.streamTimer) | ||
| this.streamTimer = undefined | ||
| } | ||
| }, 280) | ||
| } | ||
|
|
||
| onScroll(): void { | ||
| const scrollElement = this.scrollElement()?.nativeElement | ||
| if ( | ||
| !scrollElement || | ||
| !this.autoHistoryEnabled || | ||
| this.virtualizer.isAtEnd(80) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| if (scrollElement.scrollTop < 120) this.prependHistory() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>TanStack Virtual Angular Chat Example</title> | ||
| <base href="/" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| </head> | ||
| <body> | ||
| <app-root></app-root> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { bootstrapApplication } from '@angular/platform-browser' | ||
| import { AppComponent } from './app/app.component' | ||
|
|
||
| bootstrapApplication(AppComponent).catch((error) => console.error(error)) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changeset should be major rather than minor. Changing the @angular/core peer dependency to >=20.0.0 drops support for existing consumers using Angular versions below 20, so this is a breaking change.