forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

dialog.rs177 lines, 5.3 KiBHistoryRawDownload
1use topcoat::{
2 Result,
3 runtime::Expr,
4 view::{Attributes, Child, StaticClass, View, class, component, view},
5};
6
7/// Classes for the overlay that covers the viewport.
8///
9/// Set `display` only for the open state so the native closed state remains hidden.
10const OVERLAY: StaticClass = class!(
11 "fixed inset-0 z-50 size-full max-h-none max-w-none items-start",
12 "justify-center overflow-y-auto bg-black/50 p-4 text-foreground",
13 "open:flex",
14);
15
16/// Classes that fade the overlay in and out.
17///
18/// `allow-discrete` keeps it displayed through the exit transition. `@starting-style`
19/// supplies the entry transition's initial opacity.
20const FADE: StaticClass = class!(
21 "opacity-0 open:opacity-100 starting:open:opacity-0",
22 "[transition:opacity_160ms_ease-out,display_160ms_allow-discrete]",
23);
24
25/// A panel displayed over the page.
26///
27/// Pass a boolean to `open` for a fixed state, or a runtime expression to control it in
28/// the browser. The overlay blocks clicks on the page behind it. Focus trapping and
29/// Escape dismissal require application scripting.
30///
31/// Pass a `dialog_content` panel as child content. `attrs` are forwarded to the
32/// `<dialog>`, with extra classes added to its classes.
33/// Give the dialog an accessible name with `aria-label`, or use `aria-labelledby`
34/// to reference its title's ID.
35///
36/// ```ignore
37/// view! {
38/// dialog(
39/// open: confirming,
40/// dialog_content(
41/// dialog_header(
42/// dialog_title("Delete workspace")
43/// dialog_description("This cannot be undone.")
44/// )
45/// dialog_footer(
46/// // Closing the dialog is navigating to a page that
47/// // renders it closed.
48/// <a
49/// href="/workspace"
50/// class=(button_variants(ButtonVariant::Ghost, ButtonSize::Md))
51/// >
52/// "Cancel"
53/// </a>
54/// button(variant: ButtonVariant::Destructive, "Delete")
55/// )
56/// )
57/// )
58/// }
59/// ```
60#[component]
61pub async fn dialog(
62 /// Whether the dialog shows.
63 #[into]
64 open: Expr<bool>,
65 /// Extra attributes for the `<dialog>` element.
66 #[default]
67 mut attrs: Attributes,
68 /// The dialog's content.
69 #[default]
70 child: Child<'_>,
71) -> Result<impl View> {
72 Ok(view! {
73 <dialog
74 :open=(open)
75 class=(class!(OVERLAY, FADE, attrs.remove("class")))
76 (attrs)
77 >
78 (child)
79 </dialog>
80 })
81}
82
83/// Classes for the dialog panel. Automatic vertical margins center short panels while
84/// keeping the top of an oversized panel reachable by scrolling.
85const CONTENT: StaticClass = class!(
86 "relative my-auto flex w-full max-w-lg flex-col gap-4 rounded-float",
87 "border border-border bg-popover p-6 text-popover-foreground shadow-float",
88);
89
90/// Classes that scale and fade the panel as the dialog opens or closes.
91const MOTION: StaticClass = class!(
92 "scale-95 opacity-0 in-[[open]]:scale-100 in-[[open]]:opacity-100",
93 "starting:in-[[open]]:scale-95 starting:in-[[open]]:opacity-0",
94 "[transition:scale_160ms_ease-out,opacity_160ms_ease-out]",
95);
96
97/// The content panel inside a dialog.
98///
99/// Pass the dialog's sections as children. To override its maximum width, use a more
100/// specific class such as `[&]:max-w-xl` in `attrs`, or edit the component's classes.
101#[component]
102pub async fn dialog_content(
103 #[default] mut attrs: Attributes,
104 #[default] child: Child<'_>,
105) -> Result<impl View> {
106 Ok(view! {
107 <div class=(class!(CONTENT, MOTION, attrs.remove("class"))) (attrs)>
108 (child)
109 </div>
110 })
111}
112
113/// The opening section of a [`dialog_content`], stacking a [`dialog_title`]
114/// and an optional [`dialog_description`].
115#[component]
116pub async fn dialog_header(
117 #[default] mut attrs: Attributes,
118 #[default] child: Child<'_>,
119) -> Result<impl View> {
120 Ok(view! {
121 <div class=(class!("flex flex-col gap-1.5", attrs.remove("class"))) (attrs)>
122 (child)
123 </div>
124 })
125}
126
127/// The heading of a [`dialog`], rendered as an `<h2>`.
128#[component]
129pub async fn dialog_title(
130 #[default] mut attrs: Attributes,
131 #[default] child: Child<'_>,
132) -> Result<impl View> {
133 Ok(view! {
134 <h2
135 class=(class!("text-lg leading-none font-semibold", attrs.remove("class")))
136 (attrs)
137 >
138 (child)
139 </h2>
140 })
141}
142
143/// Text that explains the dialog's purpose or requested action.
144#[component]
145pub async fn dialog_description(
146 #[default] mut attrs: Attributes,
147 #[default] child: Child<'_>,
148) -> Result<impl View> {
149 Ok(view! {
150 <p
151 class=(class!("text-sm text-muted-foreground", attrs.remove("class")))
152 (attrs)
153 >
154 (child)
155 </p>
156 })
157}
158
159/// A row of actions aligned to the right of the dialog. Actions wrap when they do not
160/// fit.
161#[component]
162pub async fn dialog_footer(
163 #[default] mut attrs: Attributes,
164 #[default] child: Child<'_>,
165) -> Result<impl View> {
166 Ok(view! {
167 <div
168 class=(class!(
169 "flex flex-wrap items-center justify-end gap-2",
170 attrs.remove("class"),
171 ))
172 (attrs)
173 >
174 (child)
175 </div>
176 })
177}