Skip to main content

lumen_dioxus/content/
testimonial.rs

1//! `Testimonial` and `TestimonialGroup`.
2// cspell:words figcaption
3
4use dioxus::prelude::*;
5
6use super::util::resolve_citation_href;
7use crate::Avatar;
8use crate::class::compose;
9
10/// Props for [`Testimonial`].
11#[derive(Props, Clone, PartialEq)]
12pub struct TestimonialProps {
13    /// Person quoted. Always rendered in the caption.
14    #[props(into)]
15    pub author: String,
16    /// Author job title.
17    #[props(into)]
18    pub author_role: Option<String>,
19    /// Relationship, company, or project context shown beside the identity.
20    #[props(into)]
21    pub context: Option<String>,
22    /// Decorative portrait URL. The avatar is hidden from assistive technology.
23    #[props(into)]
24    pub avatar_src: Option<String>,
25    /// Decorative avatar initials when there is no portrait.
26    #[props(into)]
27    pub initials: Option<String>,
28    /// Source URL of the quote: absolute `http`/`https` or root-relative. Unsafe values are
29    /// dropped.
30    #[props(into)]
31    pub cite: Option<String>,
32    /// Language of the quote when it differs from the page.
33    #[props(into)]
34    pub quote_lang: Option<String>,
35    /// Larger editorial type for a lead testimonial.
36    #[props(default)]
37    pub featured: bool,
38    /// Extra classes appended after the Lumen class.
39    #[props(into)]
40    pub class: Option<String>,
41    /// Any other `<figure>` or global attribute.
42    #[props(extends = GlobalAttributes, extends = figure)]
43    pub attributes: Vec<Attribute>,
44    /// The quote.
45    pub children: Element,
46}
47
48fn present(value: Option<&String>) -> Option<String> {
49    value.filter(|text| !text.is_empty()).cloned()
50}
51
52/// A customer or peer quote with its author.
53#[component]
54pub fn Testimonial(props: TestimonialProps) -> Element {
55    let cite = props.cite.as_deref().and_then(resolve_citation_href);
56    let avatar_src = present(props.avatar_src.as_ref());
57    let initials = present(props.initials.as_ref());
58    let has_avatar = avatar_src.is_some() || initials.is_some();
59    let class = compose([Some("ui-testimonial"), props.class.as_deref()]);
60
61    rsx! {
62        figure {
63            class,
64            "data-featured": props.featured.then_some("true"),
65            ..props.attributes,
66            span { aria_hidden: "true", class: "ui-testimonial__mark", "\u{201c}" }
67            blockquote {
68                cite,
69                class: "ui-testimonial__quote",
70                lang: props.quote_lang.clone(),
71                {props.children}
72            }
73            figcaption { class: "ui-testimonial__caption",
74                if has_avatar {
75                    Avatar {
76                        src: avatar_src,
77                        fallback: initials.unwrap_or_default(),
78                        alt: "",
79                        aria_hidden: "true",
80                        class: "ui-testimonial__avatar",
81                    }
82                }
83                span { class: "ui-testimonial__identity",
84                    span { class: "ui-testimonial__author", {props.author.clone()} }
85                    if let Some(role) = present(props.author_role.as_ref()) {
86                        span { class: "ui-testimonial__role", {role} }
87                    }
88                }
89                if let Some(context) = present(props.context.as_ref()) {
90                    span { class: "ui-testimonial__context", {context} }
91                }
92            }
93        }
94    }
95}
96
97/// Layout of a [`TestimonialGroup`].
98#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
99#[non_exhaustive]
100pub enum TestimonialGroupLayout {
101    /// Features the first testimonial.
102    #[default]
103    Spotlight,
104    /// Gives every testimonial equal weight.
105    Grid,
106}
107
108impl TestimonialGroupLayout {
109    /// The `data-layout` value.
110    pub fn as_str(self) -> &'static str {
111        match self {
112            Self::Spotlight => "spotlight",
113            Self::Grid => "grid",
114        }
115    }
116}
117
118/// One testimonial rendered from data by a [`TestimonialGroup`].
119#[derive(Clone, Debug, Default, PartialEq, Eq)]
120pub struct TestimonialGroupItem {
121    /// Stable key when several items share an author.
122    pub id: Option<String>,
123    /// Person quoted.
124    pub author: String,
125    /// Author job title.
126    pub author_role: Option<String>,
127    /// Relationship or company context.
128    pub context: Option<String>,
129    /// Decorative portrait URL.
130    pub avatar_src: Option<String>,
131    /// Decorative avatar initials.
132    pub initials: Option<String>,
133    /// Source URL of the quote.
134    pub cite: Option<String>,
135    /// Language of the quote.
136    pub quote_lang: Option<String>,
137    /// The quote, rendered as a paragraph.
138    pub quote: String,
139}
140
141impl TestimonialGroupItem {
142    /// An item with an author and a quote.
143    pub fn new(author: impl Into<String>, quote: impl Into<String>) -> Self {
144        Self {
145            author: author.into(),
146            quote: quote.into(),
147            ..Self::default()
148        }
149    }
150}
151
152/// Props for [`TestimonialGroup`].
153#[derive(Props, Clone, PartialEq)]
154pub struct TestimonialGroupProps {
155    /// Renders testimonials from data. Without items, `children` are used.
156    pub items: Option<Vec<TestimonialGroupItem>>,
157    /// Accessible region name. The group is a landmark only when it has a label.
158    #[props(into)]
159    pub label: Option<String>,
160    /// Spotlight or grid layout.
161    #[props(default)]
162    pub layout: TestimonialGroupLayout,
163    /// Extra classes appended after the Lumen class.
164    #[props(into)]
165    pub class: Option<String>,
166    /// Any other `<div>` or global attribute.
167    #[props(extends = GlobalAttributes, extends = div)]
168    pub attributes: Vec<Attribute>,
169    /// [`Testimonial`]s, when `items` is not set.
170    pub children: Element,
171}
172
173/// A spotlight or grid of testimonials.
174#[component]
175pub fn TestimonialGroup(props: TestimonialGroupProps) -> Element {
176    let label = props
177        .label
178        .as_deref()
179        .map(str::trim)
180        .filter(|label| !label.is_empty())
181        .map(str::to_string);
182    let style = props.items.as_ref().map(|items| {
183        format!(
184            "--ui-testimonial-count: {}; --ui-testimonial-rows: {}",
185            items.len(),
186            items.len().saturating_sub(1).max(1)
187        )
188    });
189    let spotlight = props.layout == TestimonialGroupLayout::Spotlight;
190    let class = compose([Some("ui-testimonial-group"), props.class.as_deref()]);
191    let role = label.is_some().then_some("region");
192
193    rsx! {
194        div {
195            aria_label: label,
196            class,
197            "data-layout": props.layout.as_str(),
198            role,
199            style,
200            ..props.attributes,
201            if let Some(items) = props.items.clone() {
202                for (index, item) in items.into_iter().enumerate() {
203                    Testimonial {
204                        key: "{index}:{item.id.clone().unwrap_or_else(|| item.author.clone())}",
205                        author: item.author,
206                        author_role: item.author_role,
207                        context: item.context,
208                        avatar_src: item.avatar_src,
209                        initials: item.initials,
210                        cite: item.cite,
211                        quote_lang: item.quote_lang,
212                        featured: spotlight && index == 0,
213                        p { {item.quote} }
214                    }
215                }
216            } else {
217                {props.children}
218            }
219        }
220    }
221}