Skip to main content

tablo_ui/components/composites/
empty_state.rs

1use topcoat::{
2    Result,
3    context::Cx,
4    icon::icon,
5    view::{Attributes, Child, StaticClass, View, class, component, view},
6};
7
8const EMPTY_STATE: StaticClass = class!("flex flex-col items-center gap-2 px-6 py-12 text-center");
9const EMPTY_STATE_ICON: StaticClass = class!(
10    "mb-1 flex size-10 items-center justify-center rounded-full bg-muted \
11     text-muted-foreground [&>svg]:size-5"
12);
13const EMPTY_STATE_TITLE: StaticClass = class!("text-sm font-medium text-foreground");
14const EMPTY_STATE_DETAIL: StaticClass = class!("text-sm text-muted-foreground");
15const EMPTY_STATE_ACTION: StaticClass =
16    class!("mt-2 flex flex-wrap items-center justify-center gap-2");
17
18/// Renders zero-data content inside the region that holds no data.
19///
20/// ```rust
21/// # use tablo_ui::empty_state;
22/// # use topcoat::{context::Cx, view::{BoxView, ViewExt, view}};
23/// # fn empty(cx: &Cx) -> BoxView<'_> {
24/// view! { empty_state(title: "No records yet", detail: "Create the first one.") }
25/// # .boxed()
26/// # }
27/// ```
28#[component]
29pub async fn empty_state(
30    cx: &Cx,
31    /// What is empty, e.g. `"No records yet"`.
32    #[into]
33    title: String,
34    /// Optional muted detail line.
35    #[into]
36    #[default]
37    detail: String,
38    /// Optional controls under the text.
39    #[default]
40    action: Option<Child<'_>>,
41    /// Extra attributes for the container.
42    #[default]
43    mut attrs: Attributes,
44) -> Result<impl View> {
45    Ok(view! {
46        <div class=(class!(EMPTY_STATE, attrs.remove("class"))) (attrs)>
47            <div class=(EMPTY_STATE_ICON) aria-hidden="true">
48                icon(data: crate::icons::INBOX)
49            </div>
50            <p class=(EMPTY_STATE_TITLE)>(title)</p>
51            if !detail.is_empty() {
52                <p class=(EMPTY_STATE_DETAIL)>(detail)</p>
53            }
54            if let Some(action) = action {
55                <div class=(EMPTY_STATE_ACTION)>(action)</div>
56            }
57        </div>
58    })
59}