Skip to main content

tablo_ui/components/composites/
card.rs

1//! Tablo's card: a surface that opens with a ruled head band. Each part carries its own
2//! `data-slot` (`card`, `card-header`, `card-title`, `card-description`, `card-content`,
3//! `card-footer`), which each theme's skin keys on.
4
5use topcoat::{
6    Result,
7    context::Cx,
8    view::{Attributes, Child, StaticClass, View, class, component, view},
9};
10
11const CARD: StaticClass = class!(
12    "flex flex-col rounded-xl border border-border bg-card text-card-foreground shadow-card",
13    "has-[>[data-slot=table]]:overflow-hidden",
14);
15/// The head band: a tint, a rule under it, its title and description stacked.
16const HEADER: StaticClass = class!(
17    "flex flex-col gap-1 border-b border-rule bg-head px-5 py-3.5 first:rounded-t-[inherit]",
18    "last:rounded-b-[inherit] last:border-b-0",
19);
20const TITLE: StaticClass = class!("tablo-heading text-[0.9375rem] leading-snug text-foreground");
21const DESCRIPTION: StaticClass = class!("text-sm text-muted-foreground");
22const CONTENT: StaticClass = class!("px-5 py-5");
23const FOOTER: StaticClass =
24    class!("flex items-center gap-2 border-t border-rule px-5 py-3.5 last:rounded-b-[inherit]");
25
26/// The card's surface.
27#[component]
28pub async fn card(
29    cx: &Cx,
30    #[default] mut attrs: Attributes,
31    #[default] child: Child<'_>,
32) -> Result<impl View> {
33    Ok(view! {
34        <div class=(class!(CARD, attrs.remove("class"))) data-slot="card" (attrs)>
35            (child)
36        </div>
37    })
38}
39
40/// The head band, holding a [`card_title`] and a [`card_description`].
41#[component]
42pub async fn card_header(
43    cx: &Cx,
44    #[default] mut attrs: Attributes,
45    #[default] child: Child<'_>,
46) -> Result<impl View> {
47    Ok(view! {
48        <div
49            class=(class!(HEADER, attrs.remove("class")))
50            data-slot="card-header"
51            (attrs)
52        >
53            (child)
54        </div>
55    })
56}
57
58/// The card's heading, an `<h3>`.
59#[component]
60pub async fn card_title(
61    cx: &Cx,
62    #[default] mut attrs: Attributes,
63    #[default] child: Child<'_>,
64) -> Result<impl View> {
65    Ok(view! {
66        <h3 class=(class!(TITLE, attrs.remove("class"))) data-slot="card-title" (attrs)>
67            (child)
68        </h3>
69    })
70}
71
72/// A line under the title.
73#[component]
74pub async fn card_description(
75    cx: &Cx,
76    #[default] mut attrs: Attributes,
77    #[default] child: Child<'_>,
78) -> Result<impl View> {
79    Ok(view! {
80        <p
81            class=(class!(DESCRIPTION, attrs.remove("class")))
82            data-slot="card-description"
83            (attrs)
84        >
85            (child)
86        </p>
87    })
88}
89
90/// The card's body.
91#[component]
92pub async fn card_content(
93    cx: &Cx,
94    #[default] mut attrs: Attributes,
95    #[default] child: Child<'_>,
96) -> Result<impl View> {
97    Ok(view! {
98        <div
99            class=(class!(CONTENT, attrs.remove("class")))
100            data-slot="card-content"
101            (attrs)
102        >
103            (child)
104        </div>
105    })
106}
107
108/// A ruled row of actions under the body.
109#[component]
110pub async fn card_footer(
111    cx: &Cx,
112    #[default] mut attrs: Attributes,
113    #[default] child: Child<'_>,
114) -> Result<impl View> {
115    Ok(view! {
116        <div
117            class=(class!(FOOTER, attrs.remove("class")))
118            data-slot="card-footer"
119            (attrs)
120        >
121            (child)
122        </div>
123    })
124}