Text that names a form control.
Loading GPUI…
use gpui::{div, prelude::*, Context, Window};
use crate::ui::checkbox::Checkbox;
use crate::ui::label::Label;
#[derive(Default)]
pub struct LabelDemo {
accepted: bool,
}
impl Render for LabelDemo {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_4()
.child(
// The whole row is the click target, so clicking the label toggles the box.
div()
.id("terms")
.flex()
.items_center()
.gap_2()
.cursor_pointer()
.on_click(cx.listener(|this, _, _, cx| {
this.accepted = !this.accepted;
cx.notify();
}))
.child(Checkbox::new("terms-box", self.accepted))
.child(Label::new("Accept terms and conditions")),
)
.child(
div()
.flex()
.items_center()
.gap_2()
.child(Checkbox::new("newsletter-box", false).disabled(true))
.child(Label::new("Subscribe to the newsletter").disabled(true)),
)
}
}Installation
$ cargo hyuskui add label
Copies src/ui/label.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/label.rs.src/ui/label.rs use gpui::{div, prelude::*, rems, App, FontWeight, SharedString, Window}; use crate::theme::ActiveTheme; /// Text that names a form control. Place it next to the control in a row or column. #[derive(IntoElement)] pub struct Label { text: SharedString, disabled: bool, } impl Label { pub fn new(text: impl Into<SharedString>) -> Self { Self { text: text.into(), disabled: false, } } /// Dims the label to match a disabled control next to it. pub fn disabled(mut self, disabled: bool) -> Self { self.disabled = disabled; self } } impl RenderOnce for Label { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { div() .text_sm() .line_height(rems(1.0)) .font_weight(FontWeight::MEDIUM) .text_color(cx.theme().foreground) .when(self.disabled, |this| this.opacity(0.5)) .child(self.text) } }Register the module
// src/ui/mod.rs pub mod label;
Usage
use crate::ui::label::*;Examples
Label in Field
Stack a Label above its input to name each field of a form. For helper text and errors as well, use the Field component.
Loading GPUI…
use gpui::{div, prelude::*, px, App, Window};
use crate::ui::input::TextInput;
use crate::ui::label::Label;
pub fn label_in_field(window: &mut Window, cx: &mut App) -> impl IntoElement {
let name = window.use_keyed_state("label-card-name", cx, |_, cx| {
TextInput::new(cx).placeholder("Evil Rabbit")
});
let number = window.use_keyed_state("label-card-number", cx, |_, cx| {
TextInput::new(cx).placeholder("1234 5678 9012 3456")
});
let expiry = window.use_keyed_state("label-card-expiry", cx, |_, cx| {
TextInput::new(cx).placeholder("MM/YY")
});
let cvv = window.use_keyed_state("label-card-cvv", cx, |_, cx| {
TextInput::new(cx).placeholder("123")
});
let field = |label: &'static str, input| {
div().flex().flex_col().gap_2().child(Label::new(label)).child(input)
};
div()
.w(px(360.0))
.flex()
.flex_col()
.gap_5()
.child(field("Name on Card", name))
.child(field("Card Number", number))
.child(
div()
.flex()
.gap_4()
.child(div().flex_1().child(field("Expires", expiry)))
.child(div().flex_1().child(field("CVV", cvv))),
)
}API Reference
Label
Text that names a form control. Place it next to the control in a row or column.
Method
Label::new(text: impl Into<SharedString>) -> Self.disabled(disabled: bool)Dims the label to match a disabled control next to it.