Skip to main content

tablo_ui/components/composites/
alert.rs

1//! Tablo's alert: a message set in the page, ruled on its leading edge in its tone. The markup
2//! carries `data-slot="alert"` and `data-tone`.
3
4use topcoat::{
5    Result,
6    context::Cx,
7    view::{Attributes, Child, StaticClass, View, class, component, view},
8};
9
10/// An alert's tone.
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum AlertVariant {
13    /// Information, in the foreground's tone.
14    #[default]
15    Neutral,
16    /// An error or a refusal.
17    Destructive,
18}
19
20impl AlertVariant {
21    const fn name(self) -> &'static str {
22        match self {
23            Self::Neutral => "neutral",
24            Self::Destructive => "destructive",
25        }
26    }
27
28    fn classes(self) -> StaticClass {
29        match self {
30            Self::Neutral => class!(
31                "border-border bg-card text-foreground before:bg-foreground/25",
32                "[&>svg]:text-muted-foreground",
33            ),
34            Self::Destructive => class!(
35                "border-destructive/30 bg-destructive/[0.06] text-destructive before:bg-destructive",
36            ),
37        }
38    }
39}
40
41const BASE: StaticClass = class!(
42    "relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 overflow-hidden rounded-lg",
43    "border py-3 pr-4 pl-4.5 text-sm has-[>svg]:grid-cols-[1rem_1fr] has-[>svg]:gap-x-3",
44    "before:absolute before:inset-y-0 before:left-0 before:w-[3px] before:content-['']",
45    "[&>svg]:size-4 [&>svg]:translate-y-0.5",
46);
47
48/// The alert. It carries no `role="alert"`, which interrupts a screen reader the moment the
49/// element appears: right for a message arriving during the visit, not for one rendered with the
50/// page. Pass it among the `attrs` where that is what you want.
51#[component]
52pub async fn alert(
53    cx: &Cx,
54    #[default] variant: AlertVariant,
55    #[default] mut attrs: Attributes,
56    #[default] child: Child<'_>,
57) -> Result<impl View> {
58    Ok(view! {
59        <div
60            class=(class!(BASE, variant.classes(), attrs.remove("class")))
61            data-slot="alert"
62            data-tone=(variant.name())
63            (attrs)
64        >
65            (child)
66        </div>
67    })
68}
69
70/// The alert's first line.
71#[component]
72pub async fn alert_title(
73    cx: &Cx,
74    #[default] mut attrs: Attributes,
75    #[default] child: Child<'_>,
76) -> Result<impl View> {
77    Ok(view! {
78        <p
79            class=(class!(
80                "col-start-2 font-medium tracking-tight",
81                attrs.remove("class"),
82            ))
83            (attrs)
84        >
85            (child)
86        </p>
87    })
88}
89
90/// The lines under the title.
91#[component]
92pub async fn alert_description(
93    cx: &Cx,
94    #[default] mut attrs: Attributes,
95    #[default] child: Child<'_>,
96) -> Result<impl View> {
97    Ok(view! {
98        <div
99            class=(class!(
100                "col-start-2 text-sm text-muted-foreground",
101                attrs.remove("class"),
102            ))
103            (attrs)
104        >
105            (child)
106        </div>
107    })
108}