tablo_ui/components/composites/
toast.rs1use 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
12const 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
18const 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
34const ICON: StaticClass = class!("flex size-4 shrink-0 items-center justify-center");
36
37const CONTENT: StaticClass = class!("flex min-w-0 flex-1 flex-col gap-0.5");
39
40const TITLE: StaticClass = class!("font-medium leading-normal");
42
43const DESCRIPTION: StaticClass = class!("leading-snug text-muted-foreground");
45
46const 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#[component]
75pub async fn toaster(
76 cx: &Cx,
77 #[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#[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#[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#[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#[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#[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#[component]
195pub async fn toast_close(
196 cx: &Cx,
197 #[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}