Skip to main content

lumen_dioxus/content/
text.rs

1//! Text and labelling primitives: `Eyebrow`, `FloatingBadge`, `Item`, `Kbd`, `Marker`, `Note`,
2//! `Pill`, and `Typography`.
3
4use dioxus::prelude::*;
5
6use crate::class::{Glass, compose};
7
8/// Props for [`Eyebrow`].
9#[derive(Props, Clone, PartialEq)]
10pub struct EyebrowProps {
11    /// Extra classes appended after the Lumen class.
12    #[props(into)]
13    pub class: Option<String>,
14    /// Any other `<p>` or global attribute.
15    #[props(extends = GlobalAttributes, extends = p)]
16    pub attributes: Vec<Attribute>,
17    /// Eyebrow text.
18    pub children: Element,
19}
20
21/// A short kicker shown above a heading.
22#[component]
23pub fn Eyebrow(props: EyebrowProps) -> Element {
24    let class = compose([Some("ui-eyebrow"), props.class.as_deref()]);
25
26    rsx! {
27        p { class, ..props.attributes, {props.children} }
28    }
29}
30
31/// Props for [`FloatingBadge`].
32#[derive(Props, Clone, PartialEq)]
33pub struct FloatingBadgeProps {
34    /// Extra classes appended after the Lumen class.
35    #[props(into)]
36    pub class: Option<String>,
37    /// Any other `<span>` or global attribute.
38    #[props(extends = GlobalAttributes, extends = span)]
39    pub attributes: Vec<Attribute>,
40    /// Badge content, usually a count.
41    pub children: Element,
42}
43
44/// A badge pinned to the corner of a positioned parent, such as an avatar or icon button.
45#[component]
46pub fn FloatingBadge(props: FloatingBadgeProps) -> Element {
47    let class = compose([Some("ui-floating-badge"), props.class.as_deref()]);
48
49    rsx! {
50        span { class, ..props.attributes, {props.children} }
51    }
52}
53
54/// The element an [`Item`] renders.
55#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
56#[non_exhaustive]
57pub enum ItemElement {
58    /// A `<div>`.
59    #[default]
60    Div,
61    /// An `<article>`.
62    Article,
63    /// A `<li>`, for items inside a list.
64    Li,
65    /// A `<section>`.
66    Section,
67}
68
69/// Props for [`Item`].
70#[derive(Props, Clone, PartialEq)]
71pub struct ItemProps {
72    /// The rendered element. Use [`ItemElement::Li`] inside `ul` and `ol`.
73    #[props(default)]
74    pub r#as: ItemElement,
75    /// Glass intensity.
76    #[props(default, into)]
77    pub glass: Glass,
78    /// Extra classes appended after the Lumen classes.
79    #[props(into)]
80    pub class: Option<String>,
81    /// Any other global attribute.
82    #[props(extends = GlobalAttributes)]
83    pub attributes: Vec<Attribute>,
84    /// Item content: media, title, description, and actions.
85    pub children: Element,
86}
87
88/// A list row or tile that lines up media, text, and actions.
89#[component]
90pub fn Item(props: ItemProps) -> Element {
91    let glass = props.glass.class("ui-item");
92    let class = compose([Some("ui-item"), glass.as_deref(), props.class.as_deref()]);
93
94    match props.r#as {
95        ItemElement::Div => rsx! {
96            div { class, "data-slot": "item", ..props.attributes, {props.children} }
97        },
98        ItemElement::Article => rsx! {
99            article { class, "data-slot": "item", ..props.attributes, {props.children} }
100        },
101        ItemElement::Li => rsx! {
102            li { class, "data-slot": "item", ..props.attributes, {props.children} }
103        },
104        ItemElement::Section => rsx! {
105            section { class, "data-slot": "item", ..props.attributes, {props.children} }
106        },
107    }
108}
109
110/// Props for [`Kbd`].
111#[derive(Props, Clone, PartialEq)]
112pub struct KbdProps {
113    /// Extra classes appended after the Lumen class.
114    #[props(into)]
115    pub class: Option<String>,
116    /// Any other `<kbd>` or global attribute.
117    #[props(extends = GlobalAttributes, extends = kbd)]
118    pub attributes: Vec<Attribute>,
119    /// The key or shortcut.
120    pub children: Element,
121}
122
123/// A keyboard key or shortcut.
124#[component]
125pub fn Kbd(props: KbdProps) -> Element {
126    let class = compose([Some("ui-kbd"), props.class.as_deref()]);
127
128    rsx! {
129        kbd { class, ..props.attributes, {props.children} }
130    }
131}
132
133/// Tone of a [`Marker`].
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
135#[non_exhaustive]
136pub enum MarkerVariant {
137    /// Brand tone.
138    #[default]
139    Default,
140    /// Positive status.
141    Success,
142    /// Cautionary status.
143    Warning,
144    /// Error status.
145    Danger,
146}
147
148impl MarkerVariant {
149    /// The class suffix and `data-variant` value.
150    pub fn as_str(self) -> &'static str {
151        match self {
152            Self::Default => "default",
153            Self::Success => "success",
154            Self::Warning => "warning",
155            Self::Danger => "danger",
156        }
157    }
158}
159
160/// Props for [`Marker`].
161#[derive(Props, Clone, PartialEq)]
162pub struct MarkerProps {
163    /// Tone.
164    #[props(default)]
165    pub variant: MarkerVariant,
166    /// Extra classes appended after the Lumen classes.
167    #[props(into)]
168    pub class: Option<String>,
169    /// Any other `<span>` or global attribute.
170    #[props(extends = GlobalAttributes, extends = span)]
171    pub attributes: Vec<Attribute>,
172    /// Marker content, such as a step number.
173    pub children: Element,
174}
175
176/// A small numbered or labelled point, such as a step or map pin label.
177#[component]
178pub fn Marker(props: MarkerProps) -> Element {
179    let variant = (props.variant != MarkerVariant::Default)
180        .then(|| format!("ui-marker--{}", props.variant.as_str()));
181    let class = compose([
182        Some("ui-marker"),
183        variant.as_deref(),
184        props.class.as_deref(),
185    ]);
186
187    rsx! {
188        span {
189            class,
190            "data-variant": props.variant.as_str(),
191            ..props.attributes,
192            {props.children}
193        }
194    }
195}
196
197/// Where a [`Note`] draws its accent border.
198#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
199#[non_exhaustive]
200pub enum NoteBorderPosition {
201    /// Border on the inline start edge.
202    #[default]
203    Left,
204    /// Border on the inline end edge.
205    Right,
206    /// No accent border.
207    None,
208}
209
210impl NoteBorderPosition {
211    /// The class suffix.
212    pub fn as_str(self) -> &'static str {
213        match self {
214            Self::Left => "left",
215            Self::Right => "right",
216            Self::None => "none",
217        }
218    }
219}
220
221/// Props for [`Note`].
222#[derive(Props, Clone, PartialEq)]
223pub struct NoteProps {
224    /// Optional label shown above the content.
225    #[props(into)]
226    pub label: Option<String>,
227    /// Accent border position.
228    #[props(default)]
229    pub border_position: NoteBorderPosition,
230    /// Extra classes appended after the Lumen classes.
231    #[props(into)]
232    pub class: Option<String>,
233    /// Any other `<div>` or global attribute.
234    #[props(extends = GlobalAttributes, extends = div)]
235    pub attributes: Vec<Attribute>,
236    /// Note content.
237    pub children: Element,
238}
239
240/// An editorial aside with an optional label and an accent border.
241#[component]
242pub fn Note(props: NoteProps) -> Element {
243    let border = format!("ui-note--border-{}", props.border_position.as_str());
244    let class = compose([
245        Some("ui-note"),
246        Some(border.as_str()),
247        props.class.as_deref(),
248    ]);
249    let label = props.label.filter(|label| !label.is_empty());
250
251    rsx! {
252        div { class, ..props.attributes,
253            if let Some(label) = label {
254                p { class: "ui-note__label", {label} }
255            }
256            div { class: "ui-note__content", {props.children} }
257        }
258    }
259}
260
261/// Visual treatment of a [`Pill`].
262#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
263#[non_exhaustive]
264pub enum PillVariant {
265    /// Quiet neutral fill.
266    #[default]
267    Neutral,
268    /// Brand fill.
269    Brand,
270    /// Bordered.
271    Outline,
272}
273
274impl PillVariant {
275    /// The class suffix and `data-variant` value.
276    pub fn as_str(self) -> &'static str {
277        match self {
278            Self::Neutral => "neutral",
279            Self::Brand => "brand",
280            Self::Outline => "outline",
281        }
282    }
283}
284
285/// Props for [`Pill`].
286#[derive(Props, Clone, PartialEq)]
287pub struct PillProps {
288    /// Visual treatment.
289    #[props(default)]
290    pub variant: PillVariant,
291    /// Optional count shown after the label.
292    #[props(into)]
293    pub count: Option<String>,
294    /// Renders the pill as a link to this URL.
295    #[props(into)]
296    pub href: Option<String>,
297    /// Extra classes appended after the Lumen classes.
298    #[props(into)]
299    pub class: Option<String>,
300    /// Any other global attribute.
301    #[props(extends = GlobalAttributes)]
302    pub attributes: Vec<Attribute>,
303    /// Pill label.
304    pub children: Element,
305}
306
307/// A rounded tag or filter chip, optionally a link, with an optional count.
308#[component]
309pub fn Pill(props: PillProps) -> Element {
310    let variant = (props.variant != PillVariant::Neutral)
311        .then(|| format!("ui-pill--{}", props.variant.as_str()));
312    let class = compose([Some("ui-pill"), variant.as_deref(), props.class.as_deref()]);
313    let content = rsx! {
314        {props.children}
315        if let Some(count) = props.count {
316            span { class: "ui-pill__count", {count} }
317        }
318    };
319
320    if let Some(href) = props.href {
321        rsx! {
322            a {
323                class,
324                "data-variant": props.variant.as_str(),
325                href,
326                ..props.attributes,
327                {content}
328            }
329        }
330    } else {
331        rsx! {
332            span {
333                class,
334                "data-variant": props.variant.as_str(),
335                ..props.attributes,
336                {content}
337            }
338        }
339    }
340}
341
342/// Props for [`Typography`].
343#[derive(Props, Clone, PartialEq)]
344pub struct TypographyProps {
345    /// Extra classes appended after the Lumen class.
346    #[props(into)]
347    pub class: Option<String>,
348    /// Any other `<div>` or global attribute.
349    #[props(extends = GlobalAttributes, extends = div)]
350    pub attributes: Vec<Attribute>,
351    /// Headings, paragraphs, and lists to style.
352    pub children: Element,
353}
354
355/// Applies Lumen's type scale to plain HTML content inside it.
356#[component]
357pub fn Typography(props: TypographyProps) -> Element {
358    let class = compose([Some("ui-typography"), props.class.as_deref()]);
359
360    rsx! {
361        div { class, ..props.attributes, {props.children} }
362    }
363}