forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstick-to-bottom.ts
More file actions
218 lines (191 loc) · 5.68 KB
/
Copy pathstick-to-bottom.ts
File metadata and controls
218 lines (191 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
Directive,
ElementRef,
OnInit,
OnDestroy,
AfterViewInit,
inject,
input,
output,
} from "@angular/core";
import { ScrollPosition } from "../scroll-position";
import { ResizeObserverService } from "../resize-observer";
import { Subject } from "rxjs";
import {
takeUntil,
debounceTime,
filter,
distinctUntilChanged,
} from "rxjs/operators";
export type ScrollBehavior = "smooth" | "instant" | "auto";
/**
* Directive for implementing stick-to-bottom scroll behavior
* Similar to the React use-stick-to-bottom library
*
* @example
* ```html
* <div copilotStickToBottom
* [enabled]="true"
* [threshold]="10"
* [initialBehavior]="'smooth'"
* [resizeBehavior]="'smooth'"
* (isAtBottomChange)="onBottomChange($event)">
* <!-- Content -->
* </div>
* ```
*/
@Directive({
standalone: true,
selector: "[copilotStickToBottom]",
providers: [ScrollPosition, ResizeObserverService],
})
export class StickToBottom implements OnInit, AfterViewInit, OnDestroy {
enabled = input<boolean>(true);
threshold = input<number>(10);
initialBehavior = input<ScrollBehavior>("smooth");
resizeBehavior = input<ScrollBehavior>("smooth");
debounceMs = input<number>(100);
isAtBottomChange = output<boolean>();
scrollToBottomRequested = output<void>();
private elementRef = inject(ElementRef);
private scrollService = inject(ScrollPosition);
private resizeService = inject(ResizeObserverService);
private destroy$ = new Subject<void>();
private contentElement?: HTMLElement;
private wasAtBottom = true;
private hasInitialized = false;
private userHasScrolled = false;
ngOnInit(): void {
// Setup will happen in ngAfterViewInit
}
ngAfterViewInit(): void {
const element = this.elementRef.nativeElement as HTMLElement;
// Find or create content wrapper
this.contentElement = element.querySelector(
"[data-stick-to-bottom-content]",
) as HTMLElement;
if (!this.contentElement) {
this.contentElement = element;
}
this.setupScrollMonitoring();
this.setupResizeMonitoring();
this.setupContentMutationObserver();
// Initial scroll to bottom if enabled
setTimeout(() => {
this.hasInitialized = true;
if (this.enabled()) {
this.scrollToBottom(this.initialBehavior());
}
}, 0);
}
private setupScrollMonitoring(): void {
if (!this.enabled()) return;
const element = this.elementRef.nativeElement;
// Monitor scroll position
this.scrollService
.monitorScrollPosition(element, this.threshold())
.pipe(
takeUntil(this.destroy$),
debounceTime(this.debounceMs()),
distinctUntilChanged((a, b) => a.isAtBottom === b.isAtBottom),
)
.subscribe((state) => {
const wasAtBottom = this.wasAtBottom;
this.wasAtBottom = state.isAtBottom;
// Detect user scroll
if (!state.isAtBottom && wasAtBottom && this.hasInitialized) {
this.userHasScrolled = true;
} else if (state.isAtBottom) {
this.userHasScrolled = false;
}
// Emit change
this.isAtBottomChange.emit(state.isAtBottom);
});
}
private setupResizeMonitoring(): void {
if (!this.enabled() || !this.contentElement) return;
// Monitor content resize
this.resizeService
.observeElement(this.contentElement, 0, 250)
.pipe(
takeUntil(this.destroy$),
filter(() => this.enabled() && !this.userHasScrolled),
)
.subscribe((state) => {
// Auto-scroll on resize if we were at bottom
if (this.wasAtBottom && !state.isResizing) {
this.scrollToBottom(this.resizeBehavior());
}
});
// Monitor container resize
const element = this.elementRef.nativeElement;
this.resizeService
.observeElement(element, 0, 250)
.pipe(
takeUntil(this.destroy$),
filter(
() => this.enabled() && !this.userHasScrolled && this.wasAtBottom,
),
)
.subscribe(() => {
// Adjust scroll on container resize
this.scrollToBottom(this.resizeBehavior());
});
}
private setupContentMutationObserver(): void {
if (!this.enabled() || !this.contentElement) return;
const mutationObserver = new MutationObserver(() => {
if (this.enabled() && this.wasAtBottom && !this.userHasScrolled) {
// Content changed, scroll to bottom if we were there
requestAnimationFrame(() => {
this.scrollToBottom(this.resizeBehavior());
});
}
});
mutationObserver.observe(this.contentElement, {
childList: true,
subtree: true,
characterData: true,
});
// Cleanup on destroy
this.destroy$.subscribe(() => {
mutationObserver.disconnect();
});
}
/**
* Public method to scroll to bottom
* Can be called from parent component
*/
public scrollToBottom(behavior: ScrollBehavior = "smooth"): void {
const element = this.elementRef.nativeElement;
const smooth = behavior === "smooth";
this.scrollService.scrollToBottom(element, smooth);
this.userHasScrolled = false;
this.scrollToBottomRequested.emit();
}
/**
* Check if currently at bottom
*/
public isAtBottom(): boolean {
return this.scrollService.isAtBottom(
this.elementRef.nativeElement,
this.threshold(),
);
}
/**
* Get current scroll state
*/
public getScrollState() {
const element = this.elementRef.nativeElement;
return {
scrollTop: element.scrollTop,
scrollHeight: element.scrollHeight,
clientHeight: element.clientHeight,
isAtBottom: this.isAtBottom(),
};
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}