char-fork/llama.cpp

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

Aleksander Grygierui: Refactor data-attrs constants, enum for bool strings (#27002)bdffafa5d

char/aliceai-t5-moe
1.5 KiB51 linesraw
1import { untrack } from 'svelte';
2
3/**
4 * Scrolls the highlighted row of a picker list into view when the scroll
5 * trigger is bumped, without scrolling on mouse hover or result
6 * replacement.
7 */
8export interface UseScrollActiveRowOptions {
9	/** Counter bumped by keyboard nav; `undefined` disables the effect. */
10	getTrigger: () => number | undefined;
11	getContainer: () => HTMLDivElement | null;
12	getIndex: () => number;
13	getCount: () => number;
14	/** Full data attribute marking the row, e.g. `data-picker-index`. */
15	dataAttr: string;
16}
17
18export function useScrollActiveRow(opts: UseScrollActiveRowOptions) {
19	let lastTrigger: number | null = null;
20
21	$effect(() => {
22		const trigger = opts.getTrigger();
23
24		if (trigger === undefined) return;
25
26		// Skip the initial run on mount: the list opens with the first row
27		// already in view, and scrolling here fires before the popover is
28		// positioned, which would scroll the whole page to the top.
29		if (lastTrigger === null) {
30			lastTrigger = trigger;
31
32			return;
33		}
34
35		if (trigger === lastTrigger) return;
36
37		lastTrigger = trigger;
38		untrack(() => {
39			const container = opts.getContainer();
40			const index = opts.getIndex();
41
42			if (!container || index < 0 || index >= opts.getCount()) return;
43
44			const row = container.querySelector(`[${opts.dataAttr}="${index}"]`) as HTMLElement | null;
45
46			row?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
47		});
48	});
49}
50
51export type UseScrollActiveRowReturn = ReturnType<typeof useScrollActiveRow>;