scrollbox.mdx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ---
  2. title: ScrollBox
  3. description: A scrollable container component with customizable scrollbars
  4. order: 7
  5. ---
  6. # ScrollBox
  7. A scrollable container that supports horizontal and vertical scrolling, sticky scroll behavior, viewport culling, and customizable scrollbars.
  8. ## Basic usage
  9. ### Renderable api
  10. ```typescript
  11. import { ScrollBoxRenderable, TextRenderable, BoxRenderable, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const scrollbox = new ScrollBoxRenderable(renderer, {
  14. id: "scrollbox",
  15. width: 40,
  16. height: 20,
  17. })
  18. // Add content to the scrollbox
  19. for (let i = 0; i < 100; i++) {
  20. scrollbox.add(
  21. new BoxRenderable(renderer, {
  22. id: `item-${i}`,
  23. width: "100%",
  24. height: 2,
  25. backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449",
  26. }),
  27. )
  28. }
  29. renderer.root.add(scrollbox)
  30. ```
  31. ### Construct API
  32. ```typescript
  33. import { ScrollBox, Box, Text, createCliRenderer } from "@opentui/core"
  34. const renderer = await createCliRenderer()
  35. renderer.root.add(
  36. ScrollBox(
  37. {
  38. width: 40,
  39. height: 20,
  40. },
  41. ...Array.from({ length: 100 }, (_, i) =>
  42. Box(
  43. { width: "100%", padding: 1, backgroundColor: i % 2 === 0 ? "#292e42" : "#2f3449" },
  44. Text({ content: `Item ${i}` }),
  45. ),
  46. ),
  47. ),
  48. )
  49. ```
  50. ## Sticky scroll
  51. Enable sticky scroll to keep content pinned to an edge as new content arrives. Useful for log viewers or chat interfaces.
  52. ```typescript
  53. const scrollbox = new ScrollBoxRenderable(renderer, {
  54. id: "logs",
  55. width: 60,
  56. height: 20,
  57. stickyScroll: true,
  58. stickyStart: "bottom", // New content will keep the view scrolled to bottom
  59. })
  60. ```
  61. ### Sticky positions
  62. - `"bottom"` - Stay scrolled to bottom (default for chat/logs)
  63. - `"top"` - Stay scrolled to top
  64. - `"left"` - Stay scrolled to left
  65. - `"right"` - Stay scrolled to right
  66. When you scroll away from the sticky position, sticky behavior pauses until you scroll back to the sticky edge.
  67. ## Bidirectional scrolling
  68. Enable scrolling in both directions:
  69. ```typescript
  70. const scrollbox = new ScrollBoxRenderable(renderer, {
  71. id: "canvas",
  72. width: 60,
  73. height: 30,
  74. scrollX: true,
  75. scrollY: true,
  76. })
  77. ```
  78. By default, `scrollY` is `true` and `scrollX` is `false`.
  79. ## Viewport culling
  80. Enable viewport culling for better performance with large content. When enabled, ScrollBox renders only visible children:
  81. ```typescript
  82. const scrollbox = new ScrollBoxRenderable(renderer, {
  83. id: "large-list",
  84. width: 40,
  85. height: 20,
  86. viewportCulling: true, // Only render visible items
  87. })
  88. ```
  89. ## Customizing scrollbars
  90. Style the scrollbars using nested options:
  91. ```typescript
  92. const scrollbox = new ScrollBoxRenderable(renderer, {
  93. id: "styled-scroll",
  94. width: 40,
  95. height: 20,
  96. scrollbarOptions: {
  97. showArrows: true,
  98. trackOptions: {
  99. foregroundColor: "#7aa2f7",
  100. backgroundColor: "#414868",
  101. },
  102. },
  103. // Or customize vertical and horizontal separately
  104. verticalScrollbarOptions: {
  105. trackOptions: { backgroundColor: "#333" },
  106. },
  107. horizontalScrollbarOptions: {
  108. trackOptions: { backgroundColor: "#333" },
  109. },
  110. })
  111. ```
  112. ## Customizing sub-components
  113. ScrollBox contains several internal components that you can style individually:
  114. ```typescript
  115. const scrollbox = new ScrollBoxRenderable(renderer, {
  116. id: "custom-scroll",
  117. width: 40,
  118. height: 20,
  119. rootOptions: {
  120. backgroundColor: "#24283b",
  121. },
  122. wrapperOptions: {
  123. backgroundColor: "#1f2335",
  124. },
  125. viewportOptions: {
  126. backgroundColor: "#1a1b26",
  127. },
  128. contentOptions: {
  129. backgroundColor: "#16161e",
  130. },
  131. })
  132. ```
  133. ## Scroll methods
  134. ### scrollBy
  135. Scroll by a relative amount:
  136. ```typescript
  137. // Scroll down 5 lines
  138. scrollbox.scrollBy(5)
  139. // Scroll with both x and y
  140. scrollbox.scrollBy({ x: 10, y: 5 })
  141. // Scroll by viewport (page)
  142. scrollbox.scrollBy(1, "viewport")
  143. ```
  144. ### scrollTo
  145. Scroll to an absolute position:
  146. ```typescript
  147. // Scroll to top
  148. scrollbox.scrollTo(0)
  149. // Scroll to specific position
  150. scrollbox.scrollTo({ x: 0, y: 100 })
  151. ```
  152. ### scrollChildIntoView
  153. 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.
  154. ```typescript
  155. scrollbox.scrollChildIntoView("table-row-42")
  156. ```
  157. 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.
  158. ## Keyboard navigation
  159. When focused, ScrollBox responds to keyboard input:
  160. - **Arrow keys** - Scroll by line
  161. - **Page Up/Down** - Scroll by page
  162. - **Home/End** - Scroll to start/end
  163. ## Properties
  164. | Property | Type | Default | Description |
  165. | ---------------------------- | ---------------------------------------- | ------- | -------------------------------------- |
  166. | `scrollX` | `boolean` | `false` | Enable horizontal scrolling |
  167. | `scrollY` | `boolean` | `true` | Enable vertical scrolling |
  168. | `stickyScroll` | `boolean` | `false` | Keep scroll position pinned to an edge |
  169. | `stickyStart` | `"top" \| "bottom" \| "left" \| "right"` | - | Which edge to stick to |
  170. | `viewportCulling` | `boolean` | `true` | Only render visible children |
  171. | `scrollAcceleration` | `ScrollAcceleration` | - | Custom scroll acceleration algorithm |
  172. | `rootOptions` | `BoxOptions` | - | Style options for root container |
  173. | `wrapperOptions` | `BoxOptions` | - | Style options for wrapper |
  174. | `viewportOptions` | `BoxOptions` | - | Style options for viewport |
  175. | `contentOptions` | `BoxOptions` | - | Style options for content container |
  176. | `scrollbarOptions` | `ScrollBarOptions` | - | Options for both scrollbars |
  177. | `verticalScrollbarOptions` | `ScrollBarOptions` | - | Options for vertical scrollbar only |
  178. | `horizontalScrollbarOptions` | `ScrollBarOptions` | - | Options for horizontal scrollbar only |
  179. ## Additional properties
  180. | Property | Type | Description |
  181. | -------------- | -------- | -------------------------------------------- |
  182. | `scrollTop` | `number` | Current vertical scroll position (get/set) |
  183. | `scrollLeft` | `number` | Current horizontal scroll position (get/set) |
  184. | `scrollWidth` | `number` | Total scrollable width (read-only) |
  185. | `scrollHeight` | `number` | Total scrollable height (read-only) |
  186. ## Internal components
  187. ScrollBox exposes its internal components for advanced use:
  188. ```typescript
  189. scrollbox.wrapper // BoxRenderable - outer wrapper
  190. scrollbox.viewport // BoxRenderable - visible area
  191. scrollbox.content // ContentRenderable - holds children
  192. scrollbox.horizontalScrollBar // ScrollBarRenderable
  193. scrollbox.verticalScrollBar // ScrollBarRenderable
  194. ```