box.mdx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ---
  2. title: Box
  3. description: Container component with borders and layout capabilities
  4. order: 2
  5. ---
  6. # Box
  7. A container component with borders, background colors, and layout capabilities. Use it to create panels, frames, and organized sections.
  8. ## Basic usage
  9. ### Renderable API
  10. ```typescript
  11. import { BoxRenderable, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const panel = new BoxRenderable(renderer, {
  14. id: "panel",
  15. width: 30,
  16. height: 10,
  17. backgroundColor: "#333366",
  18. borderStyle: "double",
  19. borderColor: "#FFFFFF",
  20. })
  21. renderer.root.add(panel)
  22. ```
  23. ### Construct API
  24. ```typescript
  25. import { Box, Text, createCliRenderer } from "@opentui/core"
  26. const renderer = await createCliRenderer()
  27. renderer.root.add(
  28. Box(
  29. {
  30. width: 30,
  31. height: 10,
  32. backgroundColor: "#333366",
  33. borderStyle: "rounded",
  34. },
  35. Text({ content: "Inside the box!" }),
  36. ),
  37. )
  38. ```
  39. ## Border styles
  40. ```typescript
  41. // No border
  42. {
  43. border: false
  44. }
  45. // Simple border (default style)
  46. {
  47. border: true
  48. }
  49. // Specific border styles
  50. {
  51. borderStyle: "single"
  52. } // Single line: ┌─┐│└─┘
  53. {
  54. borderStyle: "double"
  55. } // Double line: ╔═╗║╚═╝
  56. {
  57. borderStyle: "rounded"
  58. } // Rounded corners: ╭─╮│╰─╯
  59. {
  60. borderStyle: "heavy"
  61. } // Heavy lines: ┏━┓┃┗━┛
  62. ```
  63. ## Titles
  64. Add a title and bottom title to the box border:
  65. ```typescript
  66. const panel = new BoxRenderable(renderer, {
  67. id: "settings",
  68. width: 40,
  69. height: 15,
  70. borderStyle: "rounded",
  71. title: "Settings",
  72. titleAlignment: "center",
  73. bottomTitle: "Footer",
  74. bottomTitleAlignment: "center",
  75. })
  76. ```
  77. ### Title alignment (both top and bottom titles)
  78. ```typescript
  79. {
  80. titleAlignment: "left"
  81. } // ┌─ Title ────────┐
  82. {
  83. titleAlignment: "center"
  84. } // ┌──── Title ─────┐
  85. {
  86. titleAlignment: "right"
  87. } // ┌────────── Title ┐
  88. {
  89. bottomTitleAlignment: "left"
  90. } // └─ Title ────────┘
  91. {
  92. bottomTitleAlignment: "center"
  93. } // └──── Title ─────┘
  94. {
  95. bottomTitleAlignment: "right"
  96. } // └────────── Title ┘
  97. ```
  98. ## Layout container
  99. Box works as a flex container for child elements:
  100. ```typescript
  101. const container = Box(
  102. {
  103. flexDirection: "column",
  104. justifyContent: "space-between",
  105. alignItems: "stretch",
  106. width: 50,
  107. height: 20,
  108. padding: 1,
  109. gap: 1,
  110. },
  111. Text({ content: "Header" }),
  112. Box({ flexGrow: 1, backgroundColor: "#222" }, Text({ content: "Content area" })),
  113. Text({ content: "Footer" }),
  114. )
  115. ```
  116. ## Mouse events
  117. Handle mouse interactions on the box:
  118. ```typescript
  119. const button = new BoxRenderable(renderer, {
  120. id: "button",
  121. width: 12,
  122. height: 3,
  123. border: true,
  124. backgroundColor: "#444",
  125. onMouseDown: () => {
  126. console.log("Button clicked!")
  127. },
  128. onMouseOver: () => {
  129. button.backgroundColor = "#666"
  130. },
  131. onMouseOut: () => {
  132. button.backgroundColor = "#444"
  133. },
  134. })
  135. ```
  136. ## Properties
  137. | Property | Type | Default | Description |
  138. | ---------------------- | ------------------ | -------------- | --------------------------------- |
  139. | `width` | `number \| string` | - | Width in characters or percentage |
  140. | `height` | `number \| string` | - | Height in rows or percentage |
  141. | `backgroundColor` | `string \| RGBA` | - | Background fill color |
  142. | `border` | `boolean` | `false` | Show border |
  143. | `borderStyle` | `string` | `"single"` | Border style |
  144. | `borderColor` | `string \| RGBA` | - | Border color |
  145. | `title` | `string` | - | Title text in border |
  146. | `titleAlignment` | `string` | `"left"` | Title position |
  147. | `bottomTitle` | `string` | - | Bottom title text in border |
  148. | `bottomTitleAlignment` | `string` | `"left"` | Bottom title position |
  149. | `padding` | `number` | `0` | Internal padding |
  150. | `gap` | `number \| string` | - | Gap between children |
  151. | `flexDirection` | `string` | `"column"` | Child layout direction |
  152. | `justifyContent` | `string` | `"flex-start"` | Main axis alignment |
  153. | `alignItems` | `string` | `"stretch"` | Cross axis alignment |
  154. ## Example: Card component
  155. ```typescript
  156. import { Box, Text, t, bold, fg } from "@opentui/core"
  157. function Card(props: { title: string; description: string }) {
  158. return Box(
  159. {
  160. width: 40,
  161. borderStyle: "rounded",
  162. borderColor: "#666",
  163. padding: 1,
  164. margin: 1,
  165. },
  166. Text({
  167. content: t`${bold(fg("#00FFFF")(props.title))}`,
  168. }),
  169. Text({
  170. content: props.description,
  171. fg: "#AAAAAA",
  172. }),
  173. )
  174. }
  175. renderer.root.add(
  176. Box(
  177. { flexDirection: "row", flexWrap: "wrap" },
  178. Card({ title: "Feature 1", description: "Description of feature 1" }),
  179. Card({ title: "Feature 2", description: "Description of feature 2" }),
  180. Card({ title: "Feature 3", description: "Description of feature 3" }),
  181. ),
  182. )
  183. ```