Skip to main content

tablo_ui/components/composites/
table.rs

1//! Tablo's table: columns headed by captions over a strong rule, rows ruled by hairlines and
2//! marked as the pointer crosses them. The wrapper carries `data-slot="table"`, which each theme's
3//! skin keys on.
4
5use topcoat::{
6    Result,
7    context::Cx,
8    view::{Attributes, Child, StaticClass, View, class, component, view},
9};
10
11const TABLE: StaticClass = class!(
12    "w-full caption-bottom border-collapse text-sm tabular-nums",
13    "[:not(:first-child)>&]:border-t [:not(:last-child)>&]:border-b border-rule",
14);
15const CAPTION: StaticClass = class!("mt-4 text-sm text-muted-foreground");
16const HEAD_ROW: StaticClass = class!("bg-head [&_tr]:border-b [&_tr]:border-rule-strong");
17const ROW: StaticClass = class!(
18    "border-b border-rule transition-colors hover:bg-mark/60",
19    "has-[[data-row-select]_input:checked]:bg-mark",
20);
21const HEAD: StaticClass = class!(
22    "tablo-caption h-10 px-3 text-left align-middle whitespace-nowrap text-muted-foreground",
23    "first:pl-5 last:pr-5",
24);
25const CELL: StaticClass =
26    class!("h-12 px-3 py-1.5 align-middle whitespace-nowrap first:pl-5 last:pr-5");
27
28/// The table, in a wrapper that scrolls it sideways when it is wider than its container.
29#[component]
30pub async fn table(
31    cx: &Cx,
32    #[default] mut attrs: Attributes,
33    #[default] child: Child<'_>,
34) -> Result<impl View> {
35    Ok(view! {
36        <div data-slot="table" class="w-full overflow-x-auto">
37            // The rules sit on the table, so a caption stays outside them.
38            <table class=(class!(TABLE, attrs.remove("class"))) (attrs)>(child)</table>
39        </div>
40    })
41}
42
43/// The `<thead>`, on the head band's tint.
44#[component]
45pub async fn table_header(
46    cx: &Cx,
47    #[default] mut attrs: Attributes,
48    #[default] child: Child<'_>,
49) -> Result<impl View> {
50    Ok(view! {
51        <thead class=(class!(HEAD_ROW, attrs.remove("class"))) (attrs)>(child)</thead>
52    })
53}
54
55/// The `<tbody>`; its last row drops its rule, which the table's edge draws.
56#[component]
57pub async fn table_body(
58    cx: &Cx,
59    #[default] mut attrs: Attributes,
60    #[default] child: Child<'_>,
61) -> Result<impl View> {
62    Ok(view! {
63        <tbody
64            class=(class!("[&_tr:last-child]:border-0", attrs.remove("class")))
65            (attrs)
66        >
67            (child)
68        </tbody>
69    })
70}
71
72/// The `<tfoot>`: totals over a strong rule.
73#[component]
74pub async fn table_footer(
75    cx: &Cx,
76    #[default] mut attrs: Attributes,
77    #[default] child: Child<'_>,
78) -> Result<impl View> {
79    Ok(view! {
80        <tfoot
81            class=(class!(
82                "border-t border-rule-strong bg-head font-medium [&>tr]:last:border-b-0",
83                attrs.remove("class"),
84            ))
85            (attrs)
86        >
87            (child)
88        </tfoot>
89    })
90}
91
92/// A `<tr>`.
93#[component]
94pub async fn table_row(
95    cx: &Cx,
96    #[default] mut attrs: Attributes,
97    #[default] child: Child<'_>,
98) -> Result<impl View> {
99    Ok(view! { <tr class=(class!(ROW, attrs.remove("class"))) (attrs)>(child)</tr> })
100}
101
102/// A column's `<th>`, set as a caption.
103#[component]
104pub async fn table_head(
105    cx: &Cx,
106    #[default] mut attrs: Attributes,
107    #[default] child: Child<'_>,
108) -> Result<impl View> {
109    Ok(view! { <th class=(class!(HEAD, attrs.remove("class"))) (attrs)>(child)</th> })
110}
111
112/// A `<td>`.
113#[component]
114pub async fn table_cell(
115    cx: &Cx,
116    #[default] mut attrs: Attributes,
117    #[default] child: Child<'_>,
118) -> Result<impl View> {
119    Ok(view! { <td class=(class!(CELL, attrs.remove("class"))) (attrs)>(child)</td> })
120}
121
122/// A `<caption>` under the table.
123#[component]
124pub async fn table_caption(
125    cx: &Cx,
126    #[default] mut attrs: Attributes,
127    #[default] child: Child<'_>,
128) -> Result<impl View> {
129    Ok(view! {
130        <caption class=(class!(CAPTION, attrs.remove("class"))) (attrs)>
131            (child)
132        </caption>
133    })
134}