Skip to main content

lumen_dioxus/
surface.rs

1//! Surfaces: `Card` and its parts, `Badge`, and `Alert`.
2
3use dioxus::prelude::*;
4
5use crate::class::{Glass, compose};
6
7/// Spacing inside a [`Card`].
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum CardDensity {
11    /// Default padding.
12    #[default]
13    Comfortable,
14    /// Tighter padding for dense layouts.
15    Compact,
16    /// Roomier padding for feature content.
17    Spacious,
18}
19
20impl CardDensity {
21    /// The class suffix and `data-density` value.
22    pub fn as_str(self) -> &'static str {
23        match self {
24            Self::Comfortable => "comfortable",
25            Self::Compact => "compact",
26            Self::Spacious => "spacious",
27        }
28    }
29}
30
31/// Visual treatment of a [`Card`].
32#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum CardVariant {
35    /// Bordered surface.
36    #[default]
37    Default,
38    /// Hover and focus affordances for a whole-card action.
39    Interactive,
40    /// Quieter muted surface.
41    Muted,
42    /// No surface styling; keeps the card layout.
43    Unstyled,
44}
45
46impl CardVariant {
47    /// The `data-variant` value.
48    pub fn as_str(self) -> &'static str {
49        match self {
50            Self::Default => "default",
51            Self::Interactive => "interactive",
52            Self::Muted => "muted",
53            Self::Unstyled => "unstyled",
54        }
55    }
56}
57
58/// Props for [`Card`].
59#[derive(Props, Clone, PartialEq)]
60pub struct CardProps {
61    /// Inner spacing.
62    #[props(default)]
63    pub density: CardDensity,
64    /// Visual treatment.
65    #[props(default)]
66    pub variant: CardVariant,
67    /// Glass intensity. Needs a renderer with `backdrop-filter` support.
68    #[props(default, into)]
69    pub glass: Glass,
70    /// Extra classes appended after the Lumen classes.
71    #[props(into)]
72    pub class: Option<String>,
73    /// Any other `<div>` or global attribute.
74    #[props(extends = GlobalAttributes, extends = div)]
75    pub attributes: Vec<Attribute>,
76    /// Card parts.
77    pub children: Element,
78}
79
80/// A grouped surface. Compose it with [`CardHeader`], [`CardTitle`], [`CardDescription`],
81/// [`CardContent`], and [`CardFooter`].
82#[component]
83pub fn Card(props: CardProps) -> Element {
84    let density = format!("ui-card--{}", props.density.as_str());
85    let glass_class = props.glass.class("ui-card");
86    let class = compose([
87        Some("ui-card"),
88        Some(density.as_str()),
89        (props.variant == CardVariant::Muted).then_some("ui-card--muted"),
90        (props.variant == CardVariant::Interactive).then_some("ui-card--interactive"),
91        (props.variant == CardVariant::Unstyled).then_some("ui-card--unstyled"),
92        glass_class.as_deref(),
93        props.class.as_deref(),
94    ]);
95
96    rsx! {
97        div {
98            class,
99            "data-density": props.density.as_str(),
100            "data-slot": "card",
101            "data-variant": props.variant.as_str(),
102            ..props.attributes,
103            {props.children}
104        }
105    }
106}
107
108/// Props shared by the card parts.
109#[derive(Props, Clone, PartialEq)]
110pub struct CardPartProps {
111    /// Extra classes appended after the Lumen class.
112    #[props(into)]
113    pub class: Option<String>,
114    /// Any other `<div>` or global attribute.
115    #[props(extends = GlobalAttributes, extends = div)]
116    pub attributes: Vec<Attribute>,
117    /// Content.
118    pub children: Element,
119}
120
121fn card_part(base: &str, slot: &str, props: CardPartProps) -> Element {
122    let class = compose([Some(base), props.class.as_deref()]);
123
124    rsx! {
125        div { class, "data-slot": slot.to_string(), ..props.attributes, {props.children} }
126    }
127}
128
129/// Header area of a [`Card`]: title, description, and header actions.
130#[component]
131pub fn CardHeader(props: CardPartProps) -> Element {
132    card_part("ui-card__header", "card-header", props)
133}
134
135/// Title of a [`Card`].
136#[component]
137pub fn CardTitle(props: CardPartProps) -> Element {
138    card_part("ui-card__title", "card-title", props)
139}
140
141/// Supporting text under a [`CardTitle`].
142#[component]
143pub fn CardDescription(props: CardPartProps) -> Element {
144    card_part("ui-card__description", "card-description", props)
145}
146
147/// Main content of a [`Card`].
148#[component]
149pub fn CardContent(props: CardPartProps) -> Element {
150    card_part("ui-card__content", "card-content", props)
151}
152
153/// Footer of a [`Card`], usually actions.
154#[component]
155pub fn CardFooter(props: CardPartProps) -> Element {
156    card_part("ui-card__footer", "card-footer", props)
157}
158
159/// Tone of a [`Badge`].
160#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
161#[non_exhaustive]
162pub enum BadgeVariant {
163    /// Brand tone.
164    #[default]
165    Default,
166    /// Quiet filled tone.
167    Secondary,
168    /// Bordered tone.
169    Outline,
170    /// Positive status.
171    Success,
172    /// Cautionary status.
173    Warning,
174    /// Error or destructive status.
175    Destructive,
176    /// Neutral information in a soft brand tone.
177    Info,
178}
179
180impl BadgeVariant {
181    /// The class suffix and `data-variant` value.
182    pub fn as_str(self) -> &'static str {
183        match self {
184            Self::Default => "default",
185            Self::Secondary => "secondary",
186            Self::Outline => "outline",
187            Self::Success => "success",
188            Self::Warning => "warning",
189            Self::Destructive => "destructive",
190            Self::Info => "info",
191        }
192    }
193}
194
195/// Props for [`Badge`].
196#[derive(Props, Clone, PartialEq)]
197pub struct BadgeProps {
198    /// Tone.
199    #[props(default)]
200    pub variant: BadgeVariant,
201    /// Leads the label with a decorative status dot in the badge's tone.
202    #[props(default)]
203    pub dot: bool,
204    /// Extra classes appended after the Lumen classes.
205    #[props(into)]
206    pub class: Option<String>,
207    /// Any other `<span>` or global attribute.
208    #[props(extends = GlobalAttributes, extends = span)]
209    pub attributes: Vec<Attribute>,
210    /// Badge label.
211    pub children: Element,
212}
213
214/// A compact status or classification label.
215#[component]
216pub fn Badge(props: BadgeProps) -> Element {
217    let variant = format!("ui-badge--{}", props.variant.as_str());
218    let class = compose([
219        Some("ui-badge"),
220        Some(variant.as_str()),
221        props.class.as_deref(),
222    ]);
223
224    rsx! {
225        span {
226            class,
227            "data-variant": props.variant.as_str(),
228            ..props.attributes,
229            if props.dot {
230                span { aria_hidden: "true", class: "ui-status-dot" }
231            }
232            {props.children}
233        }
234    }
235}
236
237/// Tone of an [`Alert`].
238#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
239#[non_exhaustive]
240pub enum AlertVariant {
241    /// Neutral information.
242    #[default]
243    Default,
244    /// Positive outcome.
245    Success,
246    /// Needs attention.
247    Warning,
248    /// Error or failure.
249    Destructive,
250}
251
252impl AlertVariant {
253    /// The class suffix and `data-variant` value.
254    pub fn as_str(self) -> &'static str {
255        match self {
256            Self::Default => "default",
257            Self::Success => "success",
258            Self::Warning => "warning",
259            Self::Destructive => "destructive",
260        }
261    }
262}
263
264/// Props for [`Alert`].
265#[derive(Props, Clone, PartialEq)]
266pub struct AlertProps {
267    /// Tone.
268    #[props(default)]
269    pub variant: AlertVariant,
270    /// Glass intensity.
271    #[props(default, into)]
272    pub glass: Glass,
273    /// Extra classes appended after the Lumen classes.
274    #[props(into)]
275    pub class: Option<String>,
276    /// Any other `<aside>` or global attribute.
277    #[props(extends = GlobalAttributes, extends = aside)]
278    pub attributes: Vec<Attribute>,
279    /// Alert content.
280    pub children: Element,
281}
282
283/// Inline feedback with a semantic tone.
284#[component]
285pub fn Alert(props: AlertProps) -> Element {
286    let variant = (props.variant != AlertVariant::Default)
287        .then(|| format!("ui-alert--{}", props.variant.as_str()));
288    let glass = props.glass.class("ui-alert");
289    let class = compose([
290        Some("ui-alert"),
291        variant.as_deref(),
292        glass.as_deref(),
293        props.class.as_deref(),
294    ]);
295
296    rsx! {
297        aside {
298            class,
299            "data-variant": props.variant.as_str(),
300            ..props.attributes,
301            {props.children}
302        }
303    }
304}