Skip to main content

tablo_ui/components/composites/
toast.rs

1//! Provides the toast surface and stack.
2
3use topcoat::{
4    Result,
5    context::Cx,
6    icon::icon,
7    view::{Attributes, Child, StaticClass, View, attributes, class, component, view},
8};
9
10use crate::icons;
11
12/// Positions the toast stack bottom-right.
13const TOASTER: StaticClass = class!(
14    "pointer-events-none fixed right-4 bottom-4 z-50 flex w-[356px] \
15     max-w-[calc(100vw-2rem)] flex-col gap-3.5 sm:right-6 sm:bottom-6",
16);
17
18/// Styles the toast surface.
19///
20/// The toast slides in, then fades out 4s later; hovering or focusing it holds it, and the
21/// countdown restarts once it is left. Its close control hides it at once.
22const TOAST: StaticClass = class!(
23    "pointer-events-auto relative flex w-full items-center gap-1.5 rounded-lg \
24     border border-border bg-background p-4 text-[13px] text-foreground shadow-lg \
25     [overflow-wrap:anywhere] \
26     invisible opacity-0 starting:visible starting:translate-y-full starting:opacity-100 \
27     [transition:translate_400ms_cubic-bezier(0.25,0.1,0.25,1),opacity_400ms_ease_4s,visibility_0s_linear_4.4s] \
28     hover:visible hover:opacity-100 hover:[transition-delay:0s] \
29     focus-within:visible focus-within:opacity-100 focus-within:[transition-delay:0s] \
30     has-[[data-dismiss]:checked]:hidden \
31     focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:outline-none",
32);
33
34/// Styles the icon slot.
35const ICON: StaticClass = class!("flex size-4 shrink-0 items-center justify-center");
36
37/// Styles the content column.
38const CONTENT: StaticClass = class!("flex min-w-0 flex-1 flex-col gap-0.5");
39
40/// Styles the toast title.
41const TITLE: StaticClass = class!("font-medium leading-normal");
42
43/// Styles the toast description.
44const DESCRIPTION: StaticClass = class!("leading-snug text-muted-foreground");
45
46/// Styles the close button.
47const CLOSE: StaticClass = class!(
48    "absolute top-0 left-0 flex size-5 -translate-x-[35%] -translate-y-[35%] \
49     cursor-pointer items-center justify-center rounded-full border border-border \
50     bg-background text-foreground transition-colors hover:bg-muted",
51);
52
53/// Renders the toast stack.
54///
55/// ```rust
56/// # use tablo_ui::{icons, toast, toast_close, toast_content, toast_description, toast_icon, toast_title, toaster};
57/// # use topcoat::{context::Cx, icon::icon, view::{BoxView, ViewExt, attributes, view}};
58/// # fn created(cx: &Cx) -> BoxView<'_> {
59/// # view! {
60/// toaster(label: "Notifications",
61///     toast(attrs: attributes! { data-type="success" },
62///         toast_icon(icon(data: icons::CIRCLE_CHECK))
63///         toast_content(
64///             toast_title("Created")
65///             toast_description("The record is live.")
66///         )
67///         toast_close(label: "Close")
68///     )
69/// )
70/// # }
71/// # .boxed()
72/// # }
73/// ```
74#[component]
75pub async fn toaster(
76    cx: &Cx,
77    /// The region's accessible name, such as `"Notifications"`.
78    #[into]
79    label: String,
80    #[default] mut attrs: Attributes,
81    #[default] child: Child<'_>,
82) -> Result<impl View> {
83    Ok(view! {
84        <section
85            aria-label=(label)
86            tabindex="-1"
87            aria-live="polite"
88            aria-relevant="additions text"
89            aria-atomic="false"
90        >
91            <ol
92                class=(class!(TOASTER, attrs.remove("class")))
93                data-sonner-toaster=""
94                data-y-position="bottom"
95                data-x-position="right"
96                (attrs)
97            >
98                (child)
99            </ol>
100        </section>
101    })
102}
103
104/// Renders a toast surface.
105#[component]
106pub async fn toast(
107    cx: &Cx,
108    #[default] mut attrs: Attributes,
109    #[default] child: Child<'_>,
110) -> Result<impl View> {
111    Ok(view! {
112        <li
113            tabindex="0"
114            class=(class!(TOAST, attrs.remove("class")))
115            data-sonner-toast=""
116            data-styled="true"
117            data-visible="true"
118            data-y-position="bottom"
119            data-x-position="right"
120            data-front="true"
121            (attrs)
122        >
123            (child)
124        </li>
125    })
126}
127
128/// Renders the status icon slot.
129#[component]
130pub async fn toast_icon(
131    cx: &Cx,
132    #[default] mut attrs: Attributes,
133    #[default] child: Child<'_>,
134) -> Result<impl View> {
135    Ok(view! {
136        <div
137            class=(class!(ICON, attrs.remove("class")))
138            data-icon=""
139            aria-hidden="true"
140            (attrs)
141        >
142            (child)
143        </div>
144    })
145}
146
147/// Renders the content column.
148#[component]
149pub async fn toast_content(
150    cx: &Cx,
151    #[default] mut attrs: Attributes,
152    #[default] child: Child<'_>,
153) -> Result<impl View> {
154    Ok(view! {
155        <div class=(class!(CONTENT, attrs.remove("class"))) data-content="" (attrs)>
156            (child)
157        </div>
158    })
159}
160
161/// Renders the toast title.
162#[component]
163pub async fn toast_title(
164    cx: &Cx,
165    #[default] mut attrs: Attributes,
166    #[default] child: Child<'_>,
167) -> Result<impl View> {
168    Ok(view! {
169        <div class=(class!(TITLE, attrs.remove("class"))) data-title="" (attrs)>
170            (child)
171        </div>
172    })
173}
174
175/// Renders the toast description.
176#[component]
177pub async fn toast_description(
178    cx: &Cx,
179    #[default] mut attrs: Attributes,
180    #[default] child: Child<'_>,
181) -> Result<impl View> {
182    Ok(view! {
183        <div
184            class=(class!(DESCRIPTION, attrs.remove("class")))
185            data-description=""
186            (attrs)
187        >
188            (child)
189        </div>
190    })
191}
192
193/// Renders the close control: a checkbox whose checked state hides its toast.
194#[component]
195pub async fn toast_close(
196    cx: &Cx,
197    /// The checkbox's accessible name, such as `"Close"`.
198    #[into]
199    label: String,
200    #[default] mut attrs: Attributes,
201) -> Result<impl View> {
202    Ok(view! {
203        <label
204            class=(class!(
205                CLOSE,
206                "has-focus-visible:ring-2 has-focus-visible:ring-ring/50",
207                attrs.remove("class"),
208            ))
209            data-close-button=""
210            (attrs)
211        >
212            <input type="checkbox" class="sr-only" aria-label=(label) data-dismiss="">
213            icon(data: icons::X, attrs: attributes! { class="size-3" })
214        </label>
215    })
216}