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