Skip to main content

arc_ui/inputs/
textarea.rs

1//! Ported from Arc (MIT) © 2026 Elia Kuratli
2//! Reference: `Arc源码/arc-library/registry/components/textarea/textarea.tsx` & `.module.css`
3//!
4//! P2 Component #16: `textarea`
5//!
6//! - Multi-line editing engine: `gpui_kit::component::input::{Textarea, TextareaState}` wrapped
7//!   with `.appearance(false)`.
8//! - Arc styling: `110px` default min-height, `Radii::CONTROL`, `Spacing::S3` padding, border
9//!   focus highlight with zero layout shift, and shared `FieldMessage` animated helper/error copy.
10
11use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography};
12use gpui_kit::{
13    App, ElementId, Entity, Focusable as _, IntoElement, ParentElement, RenderOnce, SharedString,
14    Styled, Window,
15    base::StyledExt as _,
16    component::input::{Textarea as KitTextarea, TextareaState},
17    div,
18    prelude::*,
19    px,
20};
21
22use super::field_message::render_field_message;
23
24/// Arc `Textarea` — multi-line text field with Arc focus borders and spring-animated field messages.
25#[derive(IntoElement)]
26pub struct Textarea {
27    id: ElementId,
28    label: SharedString,
29    state: Entity<TextareaState>,
30    description: Option<SharedString>,
31    error: Option<SharedString>,
32    min_height: f32,
33    disabled: bool,
34}
35
36impl Textarea {
37    pub fn new(
38        id: impl Into<ElementId>,
39        label: impl Into<SharedString>,
40        state: &Entity<TextareaState>,
41    ) -> Self {
42        Self {
43            id: id.into(),
44            label: label.into(),
45            state: state.clone(),
46            description: None,
47            error: None,
48            min_height: 84.0,
49            disabled: false,
50        }
51    }
52
53    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
54        self.description = Some(description.into());
55        self
56    }
57
58    pub fn error(mut self, error: impl Into<SharedString>) -> Self {
59        self.error = Some(error.into());
60        self
61    }
62
63    pub fn error_opt(mut self, error: Option<SharedString>) -> Self {
64        self.error = error;
65        self
66    }
67
68    pub fn min_height(mut self, min_height: f32) -> Self {
69        self.min_height = min_height.max(64.0);
70        self
71    }
72
73    pub fn disabled(mut self, disabled: bool) -> Self {
74        self.disabled = disabled;
75        self
76    }
77}
78
79impl RenderOnce for Textarea {
80    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
81        let colors = cx.arc_theme().colors;
82        let has_error = self.error.as_ref().is_some_and(|e| !e.is_empty());
83        let is_focused = self.state.read(cx).focus_handle(cx).is_focused(window);
84
85        let border_color = if has_error {
86            colors.danger
87        } else if is_focused {
88            colors.foreground
89        } else {
90            colors.border_strong
91        };
92        let hover_border = colors.foreground;
93
94        let shell = div()
95            .w_full()
96            .min_h(px(self.min_height))
97            .p(Spacing::S3)
98            .rounded(Radii::CONTROL)
99            .border_1()
100            .border_color(border_color)
101            .bg(if self.disabled {
102                colors.surface_muted
103            } else {
104                colors.surface
105            })
106            .when(self.disabled, |this| this.opacity(0.6))
107            .when(!self.disabled && !has_error && !is_focused, |this| {
108                this.hover(move |s| s.border_color(hover_border))
109            })
110            .text_size(Typography::TEXT_SM)
111            .text_color(colors.foreground)
112            .child(
113                KitTextarea::new(&self.state)
114                    .appearance(false)
115                    .disabled(self.disabled)
116                    .w_full()
117                    .h_full(),
118            );
119
120        let desc_id = ElementId::NamedChild(self.id.clone().into(), "description".into());
121        let err_id = ElementId::NamedChild(self.id.clone().into(), "error".into());
122
123        div()
124            .id(self.id)
125            .w_full()
126            .flex()
127            .flex_col()
128            .child(
129                div()
130                    .mb(Spacing::S2)
131                    .text_size(Typography::TEXT_SM)
132                    .font_medium()
133                    .text_color(colors.foreground)
134                    .child(self.label),
135            )
136            .child(shell)
137            .child(render_field_message(
138                desc_id,
139                self.description,
140                false,
141                window,
142                cx,
143            ))
144            .child(render_field_message(err_id, self.error, true, window, cx))
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use arc_tokens::{Accent, ThemeMode, install_arc_theme};
151    use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
152
153    use super::*;
154
155    struct TextareaHost {
156        state: Entity<TextareaState>,
157        error: Option<SharedString>,
158    }
159
160    impl Render for TextareaHost {
161        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
162            div().w(px(400.0)).p(Spacing::S4).child(
163                Textarea::new("notes", "Release notes", &self.state)
164                    .description("Supports Markdown.")
165                    .error_opt(self.error.clone()),
166            )
167        }
168    }
169
170    #[gpui_kit::test]
171    fn textarea_renders_multiline_value_and_error(cx: &mut TestAppContext) {
172        cx.update(|cx| {
173            gpui_kit::init(cx);
174            install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
175        });
176
177        let (view, cx) = cx.add_window_view(|window, cx| {
178            let state = cx.new(|cx| {
179                TextareaState::new(window, cx)
180                    .rows(4)
181                    .soft_wrap(true)
182                    .placeholder("Write a short summary...")
183                    .default_value("Line 1\nLine 2")
184            });
185            TextareaHost { state, error: None }
186        });
187
188        cx.update(|window, cx| {
189            window.render_frame(cx);
190            let state = view.read(cx).state.clone();
191            assert_eq!(state.read(cx).value(), "Line 1\nLine 2");
192
193            view.update(cx, |v, cx| {
194                v.error = Some("Description is required.".into());
195                cx.notify();
196            });
197            window.render_frame(cx);
198        });
199    }
200}