Component

Trait Component 

Source
pub trait Component {
    // Required methods
    fn focus(&mut self) -> Option<Cmd>;
    fn blur(&mut self);
    fn focused(&self) -> bool;
}
Expand description

Core trait for components that support focus management.

This trait provides a standardized interface for managing keyboard focus across all bubbletea-widgets components. Components that implement this trait can participate in focus management systems and provide consistent behavior for keyboard navigation.

§Focus States

  • Focused: The component can receive keyboard input and should visually indicate its active state
  • Blurred: The component cannot receive keyboard input and should display in an inactive state

§Implementation Guidelines

When implementing this trait:

  • focus() should set the component’s focused state and may return a command for initialization (e.g., starting a cursor blink timer)
  • blur() should unset the focused state and clean up any focus-related state
  • focused() should return the current focus state consistently

§Examples

§Basic Usage

use bubbletea_widgets::prelude::*;

let mut input = textinput_new();
assert!(!input.focused());

input.focus();
assert!(input.focused());

input.blur();
assert!(!input.focused());

§Focus Management in Applications

use bubbletea_widgets::prelude::*;
use bubbletea_rs::Cmd;

struct App {
    input: TextInput,
    textarea: TextArea,
    focused_component: usize,
}

impl App {
    fn focus_next(&mut self) -> Option<Cmd> {
        // Blur current component
        match self.focused_component {
            0 => self.input.blur(),
            1 => self.textarea.blur(),
            _ => {}
        }

        // Focus next component
        self.focused_component = (self.focused_component + 1) % 2;
        match self.focused_component {
            0 => Some(self.input.focus()),
            1 => self.textarea.focus(),
            _ => None,
        }
    }
}

Required Methods§

Source

fn focus(&mut self) -> Option<Cmd>

Sets the component to focused state.

This method should update the component’s internal state to indicate that it can receive keyboard input. It may return a command for initialization tasks like starting timers or triggering redraws.

§Returns

An optional command to be executed by the bubbletea runtime. Common use cases include starting cursor blink timers or triggering immediate redraws to show the focus state change.

§Examples
use bubbletea_widgets::prelude::*;

let mut input = textinput_new();
let cmd = input.focus();
assert!(input.focused());
// cmd may contain a cursor blink timer start command
Source

fn blur(&mut self)

Sets the component to blurred (unfocused) state.

This method should update the component’s internal state to indicate that it cannot receive keyboard input. It should clean up any focus-related resources like stopping timers.

§Examples
use bubbletea_widgets::prelude::*;

let mut input = textinput_new();
input.focus();
assert!(input.focused());

input.blur();
assert!(!input.focused());
Source

fn focused(&self) -> bool

Returns the current focus state of the component.

§Returns

true if the component is currently focused and can receive keyboard input, false otherwise.

§Examples
use bubbletea_widgets::prelude::*;

let mut input = textinput_new();
assert!(!input.focused()); // Initially unfocused

input.focus();
assert!(input.focused()); // Now focused

Implementors§

Source§

impl Component for bubbletea_widgets::textarea::Model

Focus sets the focus state on the model - port of Go’s Focus()

Source§

impl Component for bubbletea_widgets::textinput::model::Model