▚ScrollBox
A scrollable container that supports horizontal and vertical scrolling, sticky scroll behavior, viewport culling, and customizable scrollbars.
##Renderable API
1import { ScrollBoxRenderable, BoxRenderable, createCliRenderer } from "@opentui/core"23const renderer = await createCliRenderer()45const scrollbox = new ScrollBoxRenderable(renderer, {6id: "scrollbox",7width: 40,8height: 20,9})1011for (let i = 0; i < 100; i++) {12scrollbox.add(13 new BoxRenderable(renderer, {14 id: `item-${i}`,15 width: "100%",16 height: 2,17 backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449",18 }),19)20}2122renderer.root.add(scrollbox)
##Construct API
1import { ScrollBox, Box, Text, createCliRenderer } from "@opentui/core"23const renderer = await createCliRenderer()45renderer.root.add(6ScrollBox(7 {8 width: 40,9 height: 20,10 },11 ...Array.from({ length: 100 }, (_, i) =>12 Box(13 { width: "100%", padding: 1, backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449" },14 Text({ content: `Item ${i}` }),15 ),16 ),17),18)
##Sticky scroll
Keep content pinned to an edge as new content arrives. Useful for log viewers or chat interfaces:
1const scrollbox = new ScrollBoxRenderable(renderer, {2id: "logs",3width: 60,4height: 20,5stickyScroll: true,6stickyStart: "bottom", // keep scrolled to bottom7})
Sticky positions:
- ▸
"bottom"— stay scrolled to bottom (default for chat/logs) - ▸
"top"— stay scrolled to top - ▸
"left"— stay scrolled to left - ▸
"right"— stay scrolled to right
When you scroll away from the sticky position, sticky behavior pauses until you scroll back to the sticky edge.
##Bidirectional scrolling
1const scrollbox = new ScrollBoxRenderable(renderer, {2id: "canvas",3width: 60,4height: 30,5scrollX: true,6scrollY: true,7})
By default, scrollY is true and scrollX is false.
##Viewport culling
Render only visible children for large content:
1const scrollbox = new ScrollBoxRenderable(renderer, {2id: "large-list",3width: 40,4height: 20,5viewportCulling: true,6})
Viewport culling skips render calls for offscreen children, so their renderBefore and renderAfter hooks do not run. Do not make layout or state depend on render hooks.
##Scroll methods
1scrollbox.scrollBy(5) // scroll down 5 lines2scrollbox.scrollBy({ x: 10, y: 5 }) // relative, both axes3scrollbox.scrollBy(1, "viewport") // scroll by page4scrollbox.scrollTo(0) // scroll to top5scrollbox.scrollTo({ x: 0, y: 100 }) // absolute position6scrollbox.scrollChildIntoView("row-42") // reveal a nested child
scrollChildIntoView uses DOM-style "nearest" behavior: if the child already fits, the call is a no-op; otherwise it scrolls the minimum distance to reveal it.
##Keyboard navigation
##Customizing scrollbars
1const scrollbox = new ScrollBoxRenderable(renderer, {2id: "styled-scroll",3width: 40,4height: 20,5scrollbarOptions: {6 showArrows: true,7 trackOptions: {8 foregroundColor: "#7aa2f7",9 backgroundColor: "#414868",10 },11},12verticalScrollbarOptions: { trackOptions: { backgroundColor: "#333" } },13horizontalScrollbarOptions: { trackOptions: { backgroundColor: "#333" } },14})
##Properties
| prop | type | default | description |
|---|---|---|---|
| scrollX | boolean | false | Enable horizontal scrolling |
| scrollY | boolean | true | Enable vertical scrolling |
| stickyScroll | boolean | false | Keep scroll pinned to an edge |
| stickyStart | "top" | "bottom" | "left" | "right" | - | Which edge to stick to |
| viewportCulling | boolean | true | Only render visible children |
| rootOptions | BoxOptions | - | Style options for root container |
| wrapperOptions | BoxOptions | - | Style options for wrapper |
| viewportOptions | BoxOptions | - | Style options for viewport |
| contentOptions | BoxOptions | - | Style options for content container |
| scrollbarOptions | ScrollBarOptions | - | Options for both scrollbars |
| verticalScrollbarOptions | ScrollBarOptions | - | Options for vertical scrollbar |
| horizontalScrollbarOptions | ScrollBarOptions | - | Options for horizontal scrollbar |
##Internal components
ScrollBox exposes its internal components for advanced use:
1scrollbox.wrapper // BoxRenderable — outer wrapper2scrollbox.viewport // BoxRenderable — visible area3scrollbox.content // ContentRenderable — holds children4scrollbox.horizontalScrollBar // ScrollBarRenderable5scrollbox.verticalScrollBar // ScrollBarRenderable