tab-select.mdx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ---
  2. title: TabSelect
  3. description: Horizontal tab-based selection component
  4. order: 6
  5. ---
  6. # TabSelect
  7. Horizontal tab-based selection component with descriptions and scroll support. The component must be focused to receive keyboard input.
  8. ## Basic Usage
  9. ### Renderable API
  10. ```typescript
  11. import { TabSelectRenderable, TabSelectRenderableEvents, createCliRenderer } from "@opentui/core"
  12. const renderer = await createCliRenderer()
  13. const tabs = new TabSelectRenderable(renderer, {
  14. id: "tabs",
  15. width: 60,
  16. options: [
  17. { name: "Home", description: "Dashboard and overview" },
  18. { name: "Files", description: "File management" },
  19. { name: "Settings", description: "Application settings" },
  20. ],
  21. tabWidth: 20,
  22. })
  23. tabs.on(TabSelectRenderableEvents.ITEM_SELECTED, (index, option) => {
  24. console.log("Tab selected:", option.name)
  25. })
  26. tabs.focus()
  27. renderer.root.add(tabs)
  28. ```
  29. ### Construct API
  30. ```typescript
  31. import { TabSelect, createCliRenderer } from "@opentui/core"
  32. const renderer = await createCliRenderer()
  33. const tabs = TabSelect({
  34. width: 60,
  35. tabWidth: 15,
  36. options: [
  37. { name: "Tab 1", description: "First tab" },
  38. { name: "Tab 2", description: "Second tab" },
  39. { name: "Tab 3", description: "Third tab" },
  40. ],
  41. })
  42. tabs.focus()
  43. renderer.root.add(tabs)
  44. ```
  45. ## Keyboard Navigation
  46. When focused, the tab select responds to these keys:
  47. | Key | Action |
  48. | ------------- | -------------------- |
  49. | `Left` / `[` | Move to previous tab |
  50. | `Right` / `]` | Move to next tab |
  51. | `Enter` | Select current tab |
  52. ## Events
  53. ### Item Selected
  54. Emitted when the user presses Enter on a tab:
  55. ```typescript
  56. import { TabSelectRenderableEvents, type TabSelectOption } from "@opentui/core"
  57. tabs.on(TabSelectRenderableEvents.ITEM_SELECTED, (index: number, option: TabSelectOption) => {
  58. console.log(`Selected tab ${index}: ${option.name}`)
  59. // Switch to the corresponding panel
  60. })
  61. ```
  62. ### Selection Changed
  63. Emitted when the highlighted tab changes:
  64. ```typescript
  65. import { TabSelectRenderableEvents, type TabSelectOption } from "@opentui/core"
  66. tabs.on(TabSelectRenderableEvents.SELECTION_CHANGED, (index: number, option: TabSelectOption) => {
  67. console.log(`Hovering: ${option.name}`)
  68. })
  69. ```
  70. ## Properties
  71. | Property | Type | Default | Description |
  72. | -------------------------- | ------------------------ | ------------- | ------------------------------ |
  73. | `width` | `number` | - | Total component width |
  74. | `options` | `TabSelectOption[]` | `[]` | Available tabs |
  75. | `tabWidth` | `number` | `20` | Width of each tab |
  76. | `backgroundColor` | `string \| RGBA` | `transparent` | Background color |
  77. | `textColor` | `string \| RGBA` | `#FFFFFF` | Normal tab text |
  78. | `focusedBackgroundColor` | `string \| RGBA` | `#1a1a1a` | Background color when focused |
  79. | `focusedTextColor` | `string \| RGBA` | `#FFFFFF` | Text color when focused |
  80. | `selectedBackgroundColor` | `string \| RGBA` | `#334455` | Selected tab background |
  81. | `selectedTextColor` | `string \| RGBA` | `#FFFF00` | Selected tab text |
  82. | `selectedDescriptionColor` | `string \| RGBA` | `#CCCCCC` | Description text color |
  83. | `showScrollArrows` | `boolean` | `true` | Show scroll indicators |
  84. | `showDescription` | `boolean` | `true` | Show tab descriptions |
  85. | `showUnderline` | `boolean` | `true` | Show underline on selected tab |
  86. | `wrapSelection` | `boolean` | `false` | Wrap around when navigating |
  87. | `keyBindings` | `TabSelectKeyBinding[]` | - | Custom key bindings |
  88. | `keyAliasMap` | `Record<string, string>` | - | Key alias mappings |
  89. ## Example: Tabbed Interface
  90. ```typescript
  91. import { Box, Text, TabSelect, createCliRenderer } from "@opentui/core"
  92. const renderer = await createCliRenderer()
  93. // Create content panels
  94. const panels = {
  95. home: Box({ padding: 1 }, Text({ content: "Home content here" })),
  96. files: Box({ padding: 1 }, Text({ content: "File browser here" })),
  97. settings: Box({ padding: 1 }, Text({ content: "Settings form here" })),
  98. }
  99. // Create the tabbed container
  100. const container = Box({
  101. width: 60,
  102. height: 20,
  103. borderStyle: "rounded",
  104. })
  105. const tabs = TabSelect({
  106. width: 60,
  107. tabWidth: 20,
  108. options: [
  109. { name: "Home", description: "Dashboard" },
  110. { name: "Files", description: "Browse files" },
  111. { name: "Settings", description: "Preferences" },
  112. ],
  113. })
  114. // Content area
  115. let currentPanel = panels.home
  116. const contentArea = Box({
  117. flexGrow: 1,
  118. padding: 1,
  119. })
  120. contentArea.add(currentPanel)
  121. // Handle tab changes
  122. tabs.on("itemSelected", (index, option) => {
  123. // Remove current panel
  124. if (currentPanel) {
  125. contentArea.remove(currentPanel.id)
  126. }
  127. // Add new panel based on selection
  128. switch (option.name) {
  129. case "Home":
  130. currentPanel = panels.home
  131. break
  132. case "Files":
  133. currentPanel = panels.files
  134. break
  135. case "Settings":
  136. currentPanel = panels.settings
  137. break
  138. }
  139. contentArea.add(currentPanel)
  140. })
  141. container.add(tabs)
  142. container.add(contentArea)
  143. tabs.focus()
  144. renderer.root.add(container)
  145. ```
  146. ## Programmatic Control
  147. ```typescript
  148. // Get current tab index
  149. const currentIndex = tabs.getSelectedIndex()
  150. // Set tab programmatically
  151. tabs.setSelectedIndex(1)
  152. // Update tabs dynamically
  153. tabs.setOptions([
  154. { name: "New Tab 1", description: "Updated" },
  155. { name: "New Tab 2", description: "Also updated" },
  156. ])
  157. ```
  158. ## Scroll Behavior
  159. When there are more tabs than fit in the width, the component automatically handles horizontal scrolling as you navigate with the keyboard.