xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / core / src / scroll.ts

3.1 KB
1import type { ContainerProps } from "@cel-tui/types";
2import type { LayoutNode } from "./layout.js";
3import { measureTextLineCount } from "./text-layout.js";
4
5function isVerticalScrollTarget(target: LayoutNode): boolean {
6  return target.node.type === "vstack" || target.node.type === "textinput";
7}
8
9function getScrollTargetProps(target: LayoutNode): ContainerProps {
10  if (target.node.type === "text") {
11    throw new Error("Text nodes cannot be scroll targets");
12  }
13  return target.node.props;
14}
15
16function getScrollViewportMainAxisSize(target: LayoutNode): number {
17  const props = getScrollTargetProps(target);
18  const isVertical = isVerticalScrollTarget(target);
19  const mainAxisSize = isVertical ? target.rect.height : target.rect.width;
20  const mainAxisPadding = isVertical
21    ? (props.padding?.y ?? 0) * 2
22    : (props.padding?.x ?? 0) * 2;
23  return Math.max(1, mainAxisSize - mainAxisPadding);
24}
25
26/**
27 * Resolve the mouse wheel scroll step for a scrollable layout node.
28 * Uses the explicit `scrollStep` prop when provided, otherwise an
29 * adaptive default based on the visible main-axis viewport size.
30 */
31export function getScrollStep(target: LayoutNode): number {
32  const rawStep = getScrollTargetProps(target).scrollStep;
33  if (typeof rawStep === "number" && Number.isFinite(rawStep) && rawStep > 0) {
34    return Math.max(1, Math.floor(rawStep));
35  }
36
37  const viewport = getScrollViewportMainAxisSize(target);
38  return Math.max(3, Math.min(8, Math.floor(viewport / 3)));
39}
40
41/**
42 * Compute the maximum scroll offset for a scrollable layout node.
43 * Returns 0 if content fits within the viewport.
44 */
45export function getMaxScrollOffset(target: LayoutNode): number {
46  const { rect, children } = target;
47
48  if (target.node.type === "textinput") {
49    const padX = (target.node.props.padding?.x ?? 0) * 2;
50    const padY = (target.node.props.padding?.y ?? 0) * 2;
51    const contentWidth = Math.max(1, rect.width - padX);
52    const contentHeight = Math.max(0, rect.height - padY);
53    const lineCount = measureTextLineCount(
54      target.node.props.value,
55      contentWidth,
56      "word",
57    );
58    return Math.max(0, lineCount - contentHeight);
59  }
60
61  const isVertical = target.node.type === "vstack";
62  const props = getScrollTargetProps(target);
63  const padX = props.padding?.x ?? 0;
64  const padY = props.padding?.y ?? 0;
65
66  if (isVertical) {
67    let contentHeight = 0;
68    for (const child of children) {
69      const childBottom = child.rect.y + child.rect.height - rect.y;
70      if (childBottom > contentHeight) contentHeight = childBottom;
71    }
72    return Math.max(0, contentHeight + padY - rect.height);
73  }
74
75  let contentWidth = 0;
76  for (const child of children) {
77    const childRight = child.rect.x + child.rect.width - rect.x;
78    if (childRight > contentWidth) contentWidth = childRight;
79  }
80  return Math.max(0, contentWidth + padX - rect.width);
81}
82
83/**
84 * Clamp a scroll offset to the target's valid visible range.
85 * Accepts large values such as `Infinity` for sticky-bottom patterns.
86 */
87export function clampScrollOffset(target: LayoutNode, offset: number): number {
88  if (Number.isNaN(offset)) {
89    return 0;
90  }
91  return Math.max(0, Math.min(offset, getMaxScrollOffset(target)));
92}