A multi-line text field that wraps and grows with its content.
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::button::Button;
use crate::ui::textarea::Textarea;
pub fn textarea_demo(window: &mut Window, cx: &mut App) -> impl IntoElement {
// Like `TextInput`, a textarea is an Entity: in your own view, create it once with `cx.new`.
let message = window.use_keyed_state("message", cx, |_, cx| {
Textarea::new(cx).placeholder("Type your message here.")
});
let empty = message.read(cx).text().trim().is_empty();
div()
.w(px(360.))
.flex()
.flex_col()
.gap_2()
.child(message.clone())
.child(
Button::new("send", "Send message")
.disabled(empty)
.on_click(move |_, window, cx| {
message.update(cx, |message, cx| message.set_text("", window, cx));
}),
)
}Installation
Copies src/ui/textarea.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/textarea.rs.src/ui/textarea.rs use gpui::Context; use crate::ui::input::TextInput; /// A multi-line text field. It is a `TextInput` in multi-line mode, so it shares the input's /// editing, IME and clipboard support, its `InputEvent`s and its `text()` / `set_text()` API: /// /// ```ignore /// let message = cx.new(|cx| Textarea::new(cx).placeholder("Type your message here.")); /// ``` pub struct Textarea; impl Textarea { /// Three rows tall by default; it grows as the text wraps. Enter inserts a newline. pub fn new(cx: &mut Context<TextInput>) -> TextInput { TextInput::new(cx).multi_line(3) } }Register the module
// src/ui/mod.rs pub mod textarea;
Usage
use crate::ui::textarea::*;Examples
Field
Wrap a textarea in a Field for a label and helper text.
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::field::Field;
use crate::ui::textarea::Textarea;
pub fn textarea_field(window: &mut Window, cx: &mut App) -> impl IntoElement {
let message = window.use_keyed_state("textarea-field-message", cx, |_, cx| {
Textarea::new(cx).placeholder("Type your message here.")
});
div().w(px(360.)).child(
Field::new()
.label("Message")
.description("Enter your message below.")
.child(message),
)
}Disabled
disabled(true) dims the textarea and ignores focus and typing. Pass the same flag to the Field to dim its label.
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::field::Field;
use crate::ui::textarea::Textarea;
pub fn textarea_disabled(window: &mut Window, cx: &mut App) -> impl IntoElement {
let message = window.use_keyed_state("textarea-disabled-message", cx, |_, cx| {
Textarea::new(cx)
.placeholder("Type your message here.")
.disabled(true)
});
div().w(px(360.)).child(
Field::new()
.label("Message")
.disabled(true)
.child(message),
)
}Invalid
invalid(true) draws the destructive border, like aria-invalid. Give the Field an error so the label and message match.
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::field::Field;
use crate::ui::textarea::Textarea;
pub fn textarea_invalid(window: &mut Window, cx: &mut App) -> impl IntoElement {
let message = window.use_keyed_state("textarea-invalid-message", cx, |_, cx| {
Textarea::new(cx)
.placeholder("Type your message here.")
.invalid(true)
});
div().w(px(360.)).child(
Field::new()
.label("Message")
.error("Please enter a valid message.")
.child(message),
)
}Button
Pair a textarea with a full-width submit button. The button is disabled while the message is blank, and sending clears the text with set_text.
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::button::Button;
use crate::ui::textarea::Textarea;
pub fn textarea_button(window: &mut Window, cx: &mut App) -> impl IntoElement {
let message = window.use_keyed_state("textarea-button-message", cx, |_, cx| {
Textarea::new(cx).placeholder("Type your message here.")
});
let empty = message.read(cx).text().trim().is_empty();
div()
.w(px(360.))
.flex()
.flex_col()
.gap_2()
.child(message.clone())
.child(
Button::new("textarea-button-send", "Send message")
.disabled(empty)
.on_click(move |_, window, cx| {
message.update(cx, |message, cx| message.set_text("", window, cx));
}),
)
}API Reference
Textarea
A multi-line text field. It is a TextInput in multi-line mode, so it shares the input's editing, IME and clipboard support, its InputEvents and its text() / set_text() API: ``ignore let message = cx.new(|cx| Textarea::new(cx).placeholder("Type your message here."));
Textarea::new(cx: &mut Context<TextInput>) -> TextInputThree rows tall by default; it grows as the text wraps. Enter inserts a newline.