xonecas.io
6615
Yc

yoctoselect / files

yoctoselect / src / viewport.ts

719 bytes
1export function moveDown(highlight: number, total: number): number {
2  if (total === 0) return 0;
3  return highlight >= total - 1 ? 0 : highlight + 1;
4}
5
6export function moveUp(highlight: number, total: number): number {
7  if (total === 0) return 0;
8  return highlight <= 0 ? total - 1 : highlight - 1;
9}
10
11export function clampHighlight(highlight: number, total: number): number {
12  if (total === 0) return 0;
13  return Math.min(highlight, total - 1);
14}
15
16export function adjustScroll(
17  highlight: number,
18  scrollOffset: number,
19  maxVisible: number,
20): number {
21  if (highlight < scrollOffset) return highlight;
22  if (highlight >= scrollOffset + maxVisible) return highlight - maxVisible + 1;
23  return scrollOffset;
24}