Ratatui Widgets: Components, Styling and Practical Examples

Tour the built-in widget catalog, learn how styles, spans and blocks compose, and pick the right widget for each part of your interface.

WidgetsRatatui Editorial Team8 min read
Ratatui Widgets: Components, Styling and Practical Examples featured image

Ratatui ships with a catalog of widgets that covers most of what a terminal interface needs: bordered panels, wrapped text, selectable lists, tables, tabs, progress gauges, charts and a free-form drawing canvas. Knowing what each one does, and how styling flows through them, is the fastest way to turn a rough layout into a polished tool. This guide walks through the built-in widgets, explains the styling and text model they share, and finishes with a popup pattern you will reuse often.

All code below compiles against Ratatui 0.30.2. Snippets that are not complete programs are bodies of a function with the signature fn ui(frame: &mut Frame, area: Rect), the same shape used on our widgets reference page.

Widgets Are Values, Not Objects

In many GUI toolkits you create a button once and keep a handle to it. Ratatui works differently. A widget is a plain value that describes what to draw for one frame. You construct it inside terminal.draw(), configure it with builder methods, and hand it to frame.render_widget(), which consumes it. On the next frame you build it again from your current data.

That might sound wasteful, but widgets are cheap structs, and rebuilding them keeps the screen in perfect sync with your state. Data that must survive between frames, such as which row of a table is selected, lives in a separate state object owned by your app. Widgets that use such state implement StatefulWidget and are drawn with frame.render_stateful_widget(widget, area, &mut state).

The Styling System

Every widget accepts a Style, which combines a foreground color, a background color and modifiers such as bold, dim, italic, underlined or reversed. Colors come in three flavors:

  • Named ANSI colors such as Color::Red or Color::Cyan. They adapt to the user's terminal theme, which makes them the safest default.
  • Indexed colors (Color::Indexed(208)) from the 256-color palette supported by almost all modern terminals.
  • True colors (Color::Rgb(15, 118, 110)) for exact brand colors. They need a terminal with 24-bit color support.

The Stylize trait adds shorthand methods to strings, spans, lines, styles and most widgets. Instead of Style::new().fg(Color::Green).add_modifier(Modifier::BOLD) you can write .green().bold(), and .on_blue() sets the background. Styles are patched rather than replaced: a span's style is layered on top of its line's style, which is layered on top of the widget's style. That lets you set a base color once on a block and override only what differs.

The Text Model: Span, Line and Text

Styled content is built in three layers. A Span is a string with one style. A Line is a horizontal sequence of spans and can carry its own alignment. A Text is a list of lines. Plain strings convert into all three automatically through Into, so you only reach for the explicit types when you need mixed styling:

Mixed styling
let text = Text::from(vec![
    Line::from(vec!["Build ".into(), "passed".green().bold(), " in 4.2s".into()]),
    Line::from("Warnings: 0").dim(),
    Line::from("v0.30.2").right_aligned().cyan(),
]);
frame.render_widget(Paragraph::new(text), area);

Block and Paragraph: The Foundations

Block draws borders, titles and padding. Use Block::bordered() for all four borders, choose a style with .border_type(BorderType::Rounded), and add titles at the top or bottom with .title() and .title_bottom(). Most widgets accept a block through .block(), which keeps your panels visually consistent.

Paragraph renders text inside an area. It supports alignment, word wrapping with .wrap(Wrap { trim: true }) and scrolling with .scroll((y, x)). It is the right choice for help screens, log tails, descriptions and status messages.

Block + Paragraph
let help = Paragraph::new("Use the arrow keys to move between panels. Press ? at any time to show this help again.")
    .wrap(Wrap { trim: true })
    .block(
        Block::bordered()
            .border_type(BorderType::Rounded)
            .title(" Help ")
            .padding(Padding::horizontal(1)),
    );
frame.render_widget(help, area);

List and Table: Working with Collections

A List shows items vertically and highlights the selected one with a symbol and style. Its companion ListState stores the selection and scroll offset; methods such as select_next(), select_previous(), select_first() and select_last() make keyboard navigation a one-liner.

A Table adds columns. You provide rows, a header and a width constraint per column, and TableState tracks the selected row. Column widths use the same constraints as layouts, so Constraint::Fill(1) stretches a column while Constraint::Length(8) keeps it fixed. Since Ratatui 0.29 the highlight method is named row_highlight_style, and you can also highlight columns and individual cells.

Table with selection
let rows = [
    Row::new(["api", "running", "42 ms"]),
    Row::new(["worker", "degraded", "310 ms"]).yellow(),
];
let table = Table::new(rows, [Constraint::Fill(1), Constraint::Length(10), Constraint::Length(8)])
    .header(Row::new(["Service", "Status", "p95"]).bold().underlined())
    .row_highlight_style(Style::new().reversed())
    .highlight_symbol("> ")
    .block(Block::bordered().title(" Services "));
let mut state = TableState::default().with_selected(0);
frame.render_stateful_widget(table, area, &mut state);

In a real app, keep the TableState in your App struct and pass &mut app.table_state when rendering so the selection persists across frames.

Tabs, Gauge and LineGauge

Tabs renders a row of titles with one highlighted. It does not switch content by itself; your app stores the selected index, passes it to .select() and renders the matching view underneath. Gauge displays progress as a filled bar with a centered label, configured with .percent() or .ratio(). LineGauge is a slimmer one-line variant that fits neatly into status bars and lists of tasks.

Tabs and gauges
let [tabs_area, gauge_area, line_area] = Layout::vertical([
    Constraint::Length(3),
    Constraint::Length(3),
    Constraint::Length(1),
])
.areas(area);
frame.render_widget(
    Tabs::new(["Overview", "Logs", "Settings"]).select(0).block(Block::bordered()),
    tabs_area,
);
frame.render_widget(
    Gauge::default().block(Block::bordered().title(" Upload ")).percent(64).green(),
    gauge_area,
);
frame.render_widget(
    LineGauge::default().ratio(0.3).label("tests").filled_style(Style::new().cyan()),
    line_area,
);

Sparkline, Chart and BarChart

For data, Ratatui offers three levels of detail. Sparkline draws a compact series of bars from a slice of u64 values and is ideal for small history panels. Chart plots one or more datasets of (f64, f64) points on labeled axes, either as lines or scatter points; using Marker::Braille gives four times the vertical and two times the horizontal resolution of normal characters. BarChart compares categories, supports grouped bars and can render horizontally.

Charts need explicit axis bounds. If your data range changes, compute the minimum and maximum each frame and pass them to Axis::bounds(), otherwise points outside the bounds are simply not drawn.

Canvas and Scrollbar

Canvas gives you a coordinate system independent of the terminal grid. Inside its paint closure you draw shapes such as lines, rectangles, circles and points, and Ratatui maps them to characters using the marker you choose. It is how people build maps, network graphs and simple games in the terminal.

Scrollbar visualizes position within scrollable content. It is stateful: ScrollbarState::new(content_length).position(offset) tells it how much content exists and where the viewport is. Render it in the same area as the content it describes, typically with ScrollbarOrientation::VerticalRight, and update the position whenever the user scrolls.

Performance Notes for Widgets

Creating widgets every frame is cheap, but the data you feed them may not be. Formatting thousands of strings or cloning large vectors inside draw adds up. For long lists and tables, pass only the visible slice of rows when you can, or keep pre-formatted rows in state and update them only when the underlying data changes. Borrow strings with &str instead of allocating new String values, since Line, Span and Row accept borrowed text. Finally, remember that Ratatui's buffer diffing already avoids redundant terminal output, so optimization effort is best spent on your own data preparation rather than on the widgets themselves.

Popups with Clear

Because widgets draw over whatever is already in the buffer, a popup must first erase the area beneath it. The Clear widget does exactly that. Combine it with a centered layout to build dialogs, confirmations and help overlays:

Centered popup
fn popup_area(area: Rect, width: u16, height: u16) -> Rect {
    let [area] = Layout::vertical([Constraint::Length(height)]).flex(Flex::Center).areas(area);
    let [area] = Layout::horizontal([Constraint::Length(width)]).flex(Flex::Center).areas(area);
    area
}

fn render_popup(frame: &mut Frame) {
    let area = popup_area(frame.area(), 40, 5);
    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new("Delete 3 files?  (y/n)")
            .centered()
            .block(Block::bordered().title(" Confirm ").yellow()),
        area,
    );
}

Keeping a Consistent Look

As an interface grows, collect colors and styles in one place, such as a Theme struct with fields for borders, highlights, warnings and muted text. Every render function then reads from the theme instead of hard-coding colors, which keeps panels consistent and makes it trivial to offer a light and a dark variant or respect a user configuration file.

Choosing the Right Widget

Widget selection cheat sheet
You want to showUse
A framed panel or section titleBlock
Prose, logs or help textParagraph
A selectable menu or file listList + ListState
Records with columnsTable + TableState
Switchable viewsTabs
Progress or utilizationGauge / LineGauge
A compact trendSparkline
Time series or x/y dataChart
Category comparisonsBarChart
Shapes, maps, drawingsCanvas
Position in long contentScrollbar
A dialog over other contentClear + any widget

When nothing fits, write your own. Custom widgets implement the same traits as built-in ones, so they plug into layouts without special treatment. Our guide on how to create custom widgets shows the details.

Frequently Asked Questions

List, Table and Scrollbar implement StatefulWidget and use ListState, TableState and ScrollbarState. You can also render lists and tables without state when no selection is needed.

Call .border_type() with a value such as BorderType::Rounded, Double or Thick, and use .border_style() to color the border.

The terminal or a multiplexer like tmux may not support 24-bit color. Enable true color in your terminal settings, or use named and indexed colors that work everywhere.

Not as a tree, but you can achieve the same effect: compute the inner area of a block with block.inner(area), render the block, then render other widgets inside that inner rectangle.

Yes. The Monthly calendar sits behind the widget-calendar feature, which is part of the default all-widgets feature set.

Conclusion

Ratatui's widgets share one consistent model: build a value each frame, style it with layered Style patches, wrap it in a Block and render it into a Rect. Collections and scrollbars add a small state object that your app owns. Once that model clicks, combining widgets into dashboards becomes a matter of layout, which you can master with our layouts and constraints guide. For every method and option, consult the widgets module on docs.rs, and browse complete programs on our examples page.

Ratatui Editorial Team

We write tested guides for Rust developers building terminal user interfaces. Every code sample in this article was compiled against Ratatui 0.30.2. Spotted an issue? Let us know.