Documentation
¶
Overview ¶
Package carousel provides a peeking single-row carousel TUI component for the Bubble Tea framework.
The active card is rendered full-width in the center. Adjacent cards peek in from the sides, showing only their near edge. Ghost cards are rendered at the extreme edges when no real card exists in that direction.
Usage:
type MyDelegate struct{}
func (d MyDelegate) Render(item any, innerW int) string {
m := item.(MyItem)
return truncate(m.Title, innerW, "…")
}
func (d MyDelegate) IsMarked(item any) bool {
return item.(MyItem).Done
}
items := []any{item1, item2, item3}
c := carousel.New(carousel.Config{
Items: items,
Delegate: MyDelegate{},
Title: "Select an item",
})
c.SetSize(width, height)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Items is the initial list of items to display. May be nil.
Items []any
// Delegate provides item-specific rendering and mark state. Required.
Delegate ItemDelegate
// Title is displayed in the carousel header.
Title string
// ActiveColor is the border color for the focused center card.
// Defaults to lipgloss.Color("#fb6820") (orange) if empty.
ActiveColor lipgloss.Color
// MarkedColor is the border color for marked (saved/completed) inactive cards.
// Defaults to lipgloss.Color("28") (green) if empty.
MarkedColor lipgloss.Color
// DefaultColor is the border color for unvisited, unmarked inactive cards.
// Defaults to lipgloss.Color("240") (gray) if empty.
DefaultColor lipgloss.Color
// Use this to surface caller-specific keyboard hints (e.g., "a Bulk edit").
ExtraFooter string
}
Config holds the configuration for constructing a new Model.
type ItemDelegate ¶
type ItemDelegate interface {
// Render returns the card interior content for item. innerW is the number
// of visible columns available for text (accounting for card padding).
// The returned string may contain newlines but should not end with one.
Render(item any, innerW int) string
// IsMarked reports whether item has been marked (e.g., saved or completed).
// Marked items receive the MarkedColor border instead of DefaultColor.
IsMarked(item any) bool
}
ItemDelegate handles item-specific rendering for carousel cards. Implement this interface to provide card interior content and mark state for your item type.
type ItemSelectedMsg ¶
type ItemSelectedMsg struct {
Index int // 0-based index of the selected item in Items
Item any // value of the selected item
}
ItemSelectedMsg is emitted when the user selects the active card by pressing enter, down, j, or space. Handle this message in the parent model to act on the selection.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the carousel state. Create it with New and pass it through Update and View using value semantics, matching the Bubble Tea convention.
func New ¶
New creates a new Model from cfg. Missing color fields receive sensible defaults. Call SetSize before the first View call.
func (Model) MarkedCount ¶
MarkedCount returns the number of items for which Delegate.IsMarked is true.
func (*Model) SetCursor ¶
SetCursor sets the cursor to idx. The value is clamped to the valid range [0, len(Items)-1]. No-op if Items is empty.
func (*Model) SetItems ¶
SetItems replaces the item slice. If the current cursor would be out of range for the new slice, it is clamped to the last valid index.
func (*Model) SetSize ¶
SetSize sets the available terminal dimensions used for layout calculation. This should be called once on init and again whenever a tea.WindowSizeMsg is received by the parent model.
func (Model) Update ¶
Update processes a key message and returns an updated Model and an optional Cmd.
Keys handled by the component:
- left / h — move cursor left (no-op at first card)
- right / l — move cursor right (no-op at last card)
- enter / down / j / space — emit ItemSelectedMsg for the active card
The caller is responsible for handling esc, ctrl+c, and any application-specific keys before forwarding key messages here.
func (Model) View ¶
View renders the carousel to a string ready for display.
The output consists of three sections stacked vertically:
- Header: title, marked/total count, dot position indicator
- Card row: left peek (or ghost), center card, right peek (or ghost)
- Footer: navigation hints
The returned string does not include an outer container or border; the caller is responsible for any wrapping (padding, border, etc.) to match the surrounding application style.
