Skip to main content

tablo_ui/components/composites/
page.rs

1use topcoat::{
2    Result,
3    context::Cx,
4    view::{Attributes, Child, StaticClass, View, class, component, view},
5};
6
7const PAGE: StaticClass = class!("mx-auto flex w-full max-w-7xl flex-col gap-6 p-4 md:p-8");
8/// The title and the actions share the first line; the description always
9/// takes a line of its own below them, whatever order the children come in.
10const PAGE_HEADER: StaticClass = class!(
11    "flex flex-wrap items-center gap-x-4 gap-y-1.5 \
12     [&>[data-page-description]]:order-last [&>[data-page-description]]:basis-full"
13);
14// The basis keeps a title readable: past it, the actions wrap below instead of squeezing it.
15const PAGE_TITLE: StaticClass = class!(
16    "tablo-heading min-w-0 flex-1 basis-64 truncate text-[length:var(--tablo-title-size)]",
17    "leading-[1.2] text-foreground",
18);
19const PAGE_DESCRIPTION: StaticClass = class!("text-sm text-muted-foreground");
20// Capped at the header's width, so on a narrow screen the buttons wrap among themselves rather
21// than run off the page.
22const PAGE_ACTIONS: StaticClass = class!("flex max-w-full shrink-0 flex-wrap items-center gap-2");
23const PAGE_CONTENT: StaticClass = class!("flex flex-col gap-6");
24
25/// Standard container for a panel page, owning its max width, padding, and vertical rhythm.
26///
27/// ```rust
28/// # use tablo_ui::{page, page_actions, page_content, page_description, page_header, page_title};
29/// # use topcoat::{context::Cx, view::{BoxView, ViewExt, view}};
30/// # fn users<'a>(cx: &'a Cx, create_button: BoxView<'a>, table_view: BoxView<'a>) -> BoxView<'a> {
31/// # view! {
32/// page(
33///     page_header(
34///         page_title("Users")
35///         page_description("Manage your users.")
36///         page_actions((create_button))
37///     )
38///     page_content((table_view))
39/// )
40/// # }
41/// # .boxed()
42/// # }
43/// ```
44#[component]
45pub async fn page(
46    cx: &Cx,
47    #[default] mut attrs: Attributes,
48    #[default] child: Child<'_>,
49) -> Result<impl View> {
50    Ok(view! { <div class=(class!(PAGE, attrs.remove("class"))) (attrs)>(child)</div> })
51}
52
53/// The page's heading row: a [`page_title`], an optional [`page_description`]
54/// under it, and optional [`page_actions`] at the end of the title's line.
55#[component]
56pub async fn page_header(
57    cx: &Cx,
58    #[default] mut attrs: Attributes,
59    #[default] child: Child<'_>,
60) -> Result<impl View> {
61    Ok(view! {
62        <div
63            class=(class!(PAGE_HEADER, attrs.remove("class")))
64            data-page-header=""
65            (attrs)
66        >
67            (child)
68        </div>
69    })
70}
71
72#[component]
73pub async fn page_title(
74    cx: &Cx,
75    #[default] mut attrs: Attributes,
76    #[default] child: Child<'_>,
77) -> Result<impl View> {
78    Ok(view! {
79        <h1
80            class=(class!(PAGE_TITLE, attrs.remove("class")))
81            data-page-title=""
82            (attrs)
83        >
84            (child)
85        </h1>
86    })
87}
88
89#[component]
90pub async fn page_description(
91    cx: &Cx,
92    #[default] mut attrs: Attributes,
93    #[default] child: Child<'_>,
94) -> Result<impl View> {
95    Ok(view! {
96        <p
97            class=(class!(PAGE_DESCRIPTION, attrs.remove("class")))
98            data-page-description=""
99            (attrs)
100        >
101            (child)
102        </p>
103    })
104}
105
106/// The page-level controls (Create, Edit, Back...) at the end of the title's
107/// line in a [`page_header`].
108#[component]
109pub async fn page_actions(
110    cx: &Cx,
111    #[default] mut attrs: Attributes,
112    #[default] child: Child<'_>,
113) -> Result<impl View> {
114    Ok(view! {
115        <div class=(class!(PAGE_ACTIONS, attrs.remove("class"))) (attrs)>(child)</div>
116    })
117}
118
119#[component]
120pub async fn page_content(
121    cx: &Cx,
122    #[default] mut attrs: Attributes,
123    #[default] child: Child<'_>,
124) -> Result<impl View> {
125    Ok(view! {
126        <div class=(class!(PAGE_CONTENT, attrs.remove("class"))) (attrs)>(child)</div>
127    })
128}