Skip to main content

tablo_ui/components/primitives/
label.rs

1// SYNC: topcoat-ui-registry@0.12.1 sha256:89abd07da8fafb8710885adc411a05745c87a4a760b56dea8630feb0fb6d36d4 — do not hand-edit. Sync via `cargo xtask sync-topcoat-ui` (ADR-0007).
2use topcoat::{
3    Result,
4    context::Cx,
5    view::{Attributes, Child, StaticClass, View, class, component, view},
6};
7
8/// Classes that align label content and reflect the disabled state of a nearby or
9/// nested control.
10const LABEL: StaticClass = class!(
11    "flex items-center gap-2 text-sm leading-none font-medium select-none",
12    "peer-disabled:pointer-events-none peer-disabled:opacity-50",
13    "peer-has-[:disabled]:pointer-events-none peer-has-[:disabled]:opacity-50",
14    "has-[+:disabled]:pointer-events-none has-[+:disabled]:opacity-50",
15    "has-[:disabled]:pointer-events-none has-[:disabled]:opacity-50",
16);
17
18/// A label for a form control.
19///
20/// Wrap the control or pass a `for` attribute matching its `id`. Create the id with
21/// [`element_id`](topcoat::view::element_id). Pass the label text as children. `attrs`
22/// are forwarded to the `<label>`, with extra classes added to its classes.
23///
24/// ```ignore
25/// view! {
26///     <div class="flex flex-col gap-2">
27///         let email = element_id(cx);
28///         label(attrs: attributes! { for=(&email) }, "Email")
29///         input(attrs: attributes! { id=(&email) type="email" })
30///     </div>
31/// }
32/// ```
33#[component]
34pub async fn label(
35    cx: &Cx,
36    #[default] mut attrs: Attributes,
37    #[default] child: Child<'_>,
38) -> Result<impl View> {
39    Ok(view! {
40        <label class=(class!(LABEL, attrs.remove("class"))) (attrs)>(child)</label>
41    })
42}