char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Chat Conversation Tabbed navigation (#27263)8144f3192

char/aliceai-t5-moe
1.9 KiB67 linesraw
1export function useScrollCarousel() {
2	let canScrollLeft = $state(false);
3	let canScrollRight = $state(false);
4	let scrollContainer = $state<HTMLDivElement | undefined>();
5	let contentContainer = $state<HTMLDivElement | undefined>();
6
7	function scrollToCenter(element: HTMLElement) {
8		if (!scrollContainer) return;
9
10		const containerRect = scrollContainer.getBoundingClientRect();
11		const elementRect = element.getBoundingClientRect();
12		const elementCenter = elementRect.left + elementRect.width / 2;
13		const containerCenter = containerRect.left + containerRect.width / 2;
14		const scrollOffset = elementCenter - containerCenter;
15
16		scrollContainer.scrollBy({ behavior: 'smooth', left: scrollOffset });
17	}
18
19	function updateScrollButtons() {
20		if (!scrollContainer) return;
21
22		const { clientWidth, scrollLeft: sl, scrollWidth } = scrollContainer;
23
24		canScrollLeft = sl > 0;
25		canScrollRight = sl < scrollWidth - clientWidth - 1;
26	}
27
28	// Re-evaluate arrow visibility whenever the container or its content resizes,
29	// otherwise the arrows may not appear when overflowing items are added (e.g. new
30	// tabs/attachments) and the user has not scrolled yet.
31	$effect(() => {
32		if (!scrollContainer) return;
33
34		updateScrollButtons();
35
36		const observer = new ResizeObserver(() => updateScrollButtons());
37
38		observer.observe(scrollContainer);
39
40		if (contentContainer) observer.observe(contentContainer);
41
42		return () => observer.disconnect();
43	});
44
45	return {
46		get canScrollLeft() {
47			return canScrollLeft;
48		},
49		get canScrollRight() {
50			return canScrollRight;
51		},
52		get contentContainer() {
53			return contentContainer;
54		},
55		set contentContainer(el: HTMLDivElement | undefined) {
56			contentContainer = el;
57		},
58		get scrollContainer() {
59			return scrollContainer;
60		},
61		set scrollContainer(el: HTMLDivElement | undefined) {
62			scrollContainer = el;
63		},
64		scrollToCenter,
65		updateScrollButtons
66	};
67}