| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- ---
- title: ScrollBox
- description: A scrollable container component with customizable scrollbars
- order: 7
- ---
- # ScrollBox
- A scrollable container that supports horizontal and vertical scrolling, sticky scroll behavior, viewport culling, and customizable scrollbars.
- ## Basic usage
- ### Renderable api
- ```typescript
- import { ScrollBoxRenderable, TextRenderable, BoxRenderable, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "scrollbox",
- width: 40,
- height: 20,
- })
- // Add content to the scrollbox
- for (let i = 0; i < 100; i++) {
- scrollbox.add(
- new BoxRenderable(renderer, {
- id: `item-${i}`,
- width: "100%",
- height: 2,
- backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449",
- }),
- )
- }
- renderer.root.add(scrollbox)
- ```
- ### Construct API
- ```typescript
- import { ScrollBox, Box, Text, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- renderer.root.add(
- ScrollBox(
- {
- width: 40,
- height: 20,
- },
- ...Array.from({ length: 100 }, (_, i) =>
- Box(
- { width: "100%", padding: 1, backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449" },
- Text({ content: `Item ${i}` }),
- ),
- ),
- ),
- )
- ```
- ## Sticky scroll
- Enable sticky scroll to keep content pinned to an edge as new content arrives. Useful for log viewers or chat interfaces.
- ```typescript
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "logs",
- width: 60,
- height: 20,
- stickyScroll: true,
- stickyStart: "bottom", // New content will keep the view scrolled to bottom
- })
- ```
- ### 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
- Enable scrolling in both directions:
- ```typescript
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "canvas",
- width: 60,
- height: 30,
- scrollX: true,
- scrollY: true,
- })
- ```
- By default, `scrollY` is `true` and `scrollX` is `false`.
- ## Viewport culling
- Enable viewport culling for better performance with large content. When enabled, ScrollBox renders only visible children:
- ```typescript
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "large-list",
- width: 40,
- height: 20,
- viewportCulling: true, // Only render visible items
- })
- ```
- ## Customizing scrollbars
- Style the scrollbars using nested options:
- ```typescript
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "styled-scroll",
- width: 40,
- height: 20,
- scrollbarOptions: {
- showArrows: true,
- trackOptions: {
- foregroundColor: "#7aa2f7",
- backgroundColor: "#414868",
- },
- },
- // Or customize vertical and horizontal separately
- verticalScrollbarOptions: {
- trackOptions: { backgroundColor: "#333" },
- },
- horizontalScrollbarOptions: {
- trackOptions: { backgroundColor: "#333" },
- },
- })
- ```
- ## Customizing sub-components
- ScrollBox contains several internal components that you can style individually:
- ```typescript
- const scrollbox = new ScrollBoxRenderable(renderer, {
- id: "custom-scroll",
- width: 40,
- height: 20,
- rootOptions: {
- backgroundColor: "#24283b",
- },
- wrapperOptions: {
- backgroundColor: "#1f2335",
- },
- viewportOptions: {
- backgroundColor: "#1a1b26",
- },
- contentOptions: {
- backgroundColor: "#16161e",
- },
- })
- ```
- ## Scroll methods
- ### scrollBy
- Scroll by a relative amount:
- ```typescript
- // Scroll down 5 lines
- scrollbox.scrollBy(5)
- // Scroll with both x and y
- scrollbox.scrollBy({ x: 10, y: 5 })
- // Scroll by viewport (page)
- scrollbox.scrollBy(1, "viewport")
- ```
- ### scrollTo
- Scroll to an absolute position:
- ```typescript
- // Scroll to top
- scrollbox.scrollTo(0)
- // Scroll to specific position
- scrollbox.scrollTo({ x: 0, y: 100 })
- ```
- ### scrollChildIntoView
- Scroll the minimum distance needed to show a nested child in the viewport. The method uses DOM-style "nearest" behavior: if the child already fits, the call is a no-op; otherwise it scrolls to reveal the nearest edge.
- ```typescript
- scrollbox.scrollChildIntoView("table-row-42")
- ```
- Use it when you focus an element offscreen (search results, a newly inserted form field, etc.) and want to bring it into view without moving the scroll position more than needed.
- ## Keyboard navigation
- When focused, ScrollBox responds to keyboard input:
- - **Arrow keys** - Scroll by line
- - **Page Up/Down** - Scroll by page
- - **Home/End** - Scroll to start/end
- ## Properties
- | Property | Type | Default | Description |
- | ---------------------------- | ---------------------------------------- | ------- | -------------------------------------- |
- | `scrollX` | `boolean` | `false` | Enable horizontal scrolling |
- | `scrollY` | `boolean` | `true` | Enable vertical scrolling |
- | `stickyScroll` | `boolean` | `false` | Keep scroll position pinned to an edge |
- | `stickyStart` | `"top" \| "bottom" \| "left" \| "right"` | - | Which edge to stick to |
- | `viewportCulling` | `boolean` | `true` | Only render visible children |
- | `scrollAcceleration` | `ScrollAcceleration` | - | Custom scroll acceleration algorithm |
- | `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 only |
- | `horizontalScrollbarOptions` | `ScrollBarOptions` | - | Options for horizontal scrollbar only |
- ## Additional properties
- | Property | Type | Description |
- | -------------- | -------- | -------------------------------------------- |
- | `scrollTop` | `number` | Current vertical scroll position (get/set) |
- | `scrollLeft` | `number` | Current horizontal scroll position (get/set) |
- | `scrollWidth` | `number` | Total scrollable width (read-only) |
- | `scrollHeight` | `number` | Total scrollable height (read-only) |
- ## Internal components
- ScrollBox exposes its internal components for advanced use:
- ```typescript
- scrollbox.wrapper // BoxRenderable - outer wrapper
- scrollbox.viewport // BoxRenderable - visible area
- scrollbox.content // ContentRenderable - holds children
- scrollbox.horizontalScrollBar // ScrollBarRenderable
- scrollbox.verticalScrollBar // ScrollBarRenderable
- ```
|