Skip to main content

ratatui_form/
style.rs

1//! Form styling and theming.
2
3use ratatui::style::{Color, Modifier, Style};
4
5/// Style configuration for forms.
6#[derive(Debug, Clone)]
7pub struct FormStyle {
8    /// Style for the form title.
9    pub title: Style,
10    /// Style for field labels.
11    pub label: Style,
12    /// Style for focused field labels.
13    pub label_focused: Style,
14    /// Style for input fields.
15    pub input: Style,
16    /// Style for focused input fields.
17    pub input_focused: Style,
18    /// Style for placeholder text.
19    pub placeholder: Style,
20    /// Style for error messages.
21    pub error: Style,
22    /// Style for the submit button.
23    pub button: Style,
24    /// Style for the focused submit button.
25    pub button_focused: Style,
26    /// Style for the form border.
27    pub border: Style,
28    /// Style for the focused form border.
29    pub border_focused: Style,
30}
31
32impl Default for FormStyle {
33    fn default() -> Self {
34        Self {
35            title: Style::default()
36                .fg(Color::Cyan)
37                .add_modifier(Modifier::BOLD),
38            label: Style::default().fg(Color::White),
39            label_focused: Style::default()
40                .fg(Color::Cyan)
41                .add_modifier(Modifier::BOLD),
42            input: Style::default().fg(Color::White).bg(Color::DarkGray),
43            input_focused: Style::default().fg(Color::White).bg(Color::Blue),
44            placeholder: Style::default().fg(Color::Gray),
45            error: Style::default().fg(Color::Red),
46            button: Style::default().fg(Color::White).bg(Color::DarkGray),
47            button_focused: Style::default()
48                .fg(Color::Black)
49                .bg(Color::Green)
50                .add_modifier(Modifier::BOLD),
51            border: Style::default().fg(Color::Gray),
52            border_focused: Style::default().fg(Color::Cyan),
53        }
54    }
55}
56
57impl FormStyle {
58    /// Creates a new form style with default values.
59    pub fn new() -> Self {
60        Self::default()
61    }
62
63    /// Sets the title style.
64    pub fn title(mut self, style: Style) -> Self {
65        self.title = style;
66        self
67    }
68
69    /// Sets the label style.
70    pub fn label(mut self, style: Style) -> Self {
71        self.label = style;
72        self
73    }
74
75    /// Sets the focused label style.
76    pub fn label_focused(mut self, style: Style) -> Self {
77        self.label_focused = style;
78        self
79    }
80
81    /// Sets the input style.
82    pub fn input(mut self, style: Style) -> Self {
83        self.input = style;
84        self
85    }
86
87    /// Sets the focused input style.
88    pub fn input_focused(mut self, style: Style) -> Self {
89        self.input_focused = style;
90        self
91    }
92
93    /// Sets the placeholder style.
94    pub fn placeholder(mut self, style: Style) -> Self {
95        self.placeholder = style;
96        self
97    }
98
99    /// Sets the error style.
100    pub fn error(mut self, style: Style) -> Self {
101        self.error = style;
102        self
103    }
104
105    /// Sets the button style.
106    pub fn button(mut self, style: Style) -> Self {
107        self.button = style;
108        self
109    }
110
111    /// Sets the focused button style.
112    pub fn button_focused(mut self, style: Style) -> Self {
113        self.button_focused = style;
114        self
115    }
116
117    /// Creates a dark theme.
118    pub fn dark() -> Self {
119        Self::default()
120    }
121
122    /// Creates a light theme.
123    pub fn light() -> Self {
124        Self {
125            title: Style::default()
126                .fg(Color::Blue)
127                .add_modifier(Modifier::BOLD),
128            label: Style::default().fg(Color::Black),
129            label_focused: Style::default()
130                .fg(Color::Blue)
131                .add_modifier(Modifier::BOLD),
132            input: Style::default().fg(Color::Black).bg(Color::White),
133            input_focused: Style::default().fg(Color::Black).bg(Color::LightBlue),
134            placeholder: Style::default().fg(Color::DarkGray),
135            error: Style::default().fg(Color::Red),
136            button: Style::default().fg(Color::Black).bg(Color::White),
137            button_focused: Style::default()
138                .fg(Color::White)
139                .bg(Color::Blue)
140                .add_modifier(Modifier::BOLD),
141            border: Style::default().fg(Color::DarkGray),
142            border_focused: Style::default().fg(Color::Blue),
143        }
144    }
145}