Primitives

Button

Interactive button component with multiple states and styling options.

Overview

The Button primitive provides an interactive button component for TUI applications. It supports multiple states, custom styling, and keyboard navigation.

Basic Usage

use ratkit::primitives::button::{Button, ButtonState};
use ratatui::Frame;

fn render_button(frame: &mut Frame) {
    let button = Button::new("Click Me");
    let mut state = ButtonState::default();
    
    frame.render_stateful_widget(button, frame.area(), &mut state);
}

Styling

Customize button appearance with styles:

use ratatui::style::{Style, Color, Modifier};

let button = Button::new("Submit")
    .style(Style::default().fg(Color::White))
    .highlight_style(Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::BOLD));

States

Buttons have three states:

  • Normal - Default state
  • Highlighted - When focused or hovered
  • Pressed - When activated

Event Handling

Handle button interactions:

use ratatui::crossterm::event::{Event, KeyCode};

if let Event::Key(key) = event::read()? {
    match key.code {
        KeyCode::Enter if button_state.is_focused() => {
            // Button clicked
        }
        _ => {}
    }
}

Examples

See the button demo for a complete working example.