Overview
TheScrollBox widget creates a scrollable viewport for content that exceeds the container’s size. It supports both horizontal and vertical scrolling and can contain exactly one child widget.
Constructor
ScrollBoxOptions
Width of the scrollbox viewport. Accepts:
- Number: fixed width in cells
- String: percentage (e.g.,
"50%") or"auto"
Height of the scrollbox viewport. Accepts:
- Number: fixed height in rows
- String: percentage (e.g.,
"100%") or"auto"
Border style. Valid values:
"none"- no border"single"- single-line border"double"- double-line border"rounded"- rounded corners"bold"- bold border
Foreground (border/scrollbar) color. Accepts:
- Hex string:
"#FF0000" - Color name:
"red","blue", etc. - 256-color code:
196
Background color. Same format as
fg.Methods
setScroll()
Set the absolute scroll position.Horizontal scroll position in cells.
0 is the leftmost position.Vertical scroll position in rows.
0 is the topmost position.ts/src/widgets/scrollbox.ts:27
getScroll()
Get the current scroll position.Object containing:
x- Horizontal scroll position in cellsy- Vertical scroll position in rows
ts/src/widgets/scrollbox.ts:31
scrollBy()
Scroll relative to the current position.Amount to scroll horizontally. Positive values scroll right, negative scroll left.
Amount to scroll vertically. Positive values scroll down, negative scroll up.
ts/src/widgets/scrollbox.ts:38
Examples
Inherited Methods
ScrollBox inherits all methods from the Widget base class, including:append(child)- Add the single child widgetremoveChild(child)- Remove the child widgetsetVisible(visible)- Control visibilitydestroy()- Clean up resources
Event Handling
ScrollBox widgets typically respond to keyboard events for navigation:- Arrow keys - Scroll in direction
- Page Up/Down - Scroll larger increments
- Home/End - Jump to top/bottom
app.drainEvents().
Single-Child Constraint
ScrollBox can contain exactly one child widget. If you need to scroll multiple widgets:- Create a container
Box - Add your widgets to the Box
- Add the Box as the child of ScrollBox
Notes
- ScrollBox supports both horizontal and vertical scrolling
- Scroll position is automatically clamped to content bounds
- Scrollbars are displayed automatically when content exceeds viewport
- Use
scrollBy()for relative scrolling (e.g., in response to arrow keys) - Use
setScroll()for absolute positioning (e.g., jump to specific line) - The single child widget can be arbitrarily large
- Scrolling is handled by the native layer for performance