lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
//! `Testimonial` and `TestimonialGroup`.
// cspell:words figcaption

use dioxus::prelude::*;

use super::util::resolve_citation_href;
use crate::Avatar;
use crate::class::compose;

/// Props for [`Testimonial`].
#[derive(Props, Clone, PartialEq)]
pub struct TestimonialProps {
    /// Person quoted. Always rendered in the caption.
    #[props(into)]
    pub author: String,
    /// Author job title.
    #[props(into)]
    pub author_role: Option<String>,
    /// Relationship, company, or project context shown beside the identity.
    #[props(into)]
    pub context: Option<String>,
    /// Decorative portrait URL. The avatar is hidden from assistive technology.
    #[props(into)]
    pub avatar_src: Option<String>,
    /// Decorative avatar initials when there is no portrait.
    #[props(into)]
    pub initials: Option<String>,
    /// Source URL of the quote: absolute `http`/`https` or root-relative. Unsafe values are
    /// dropped.
    #[props(into)]
    pub cite: Option<String>,
    /// Language of the quote when it differs from the page.
    #[props(into)]
    pub quote_lang: Option<String>,
    /// Larger editorial type for a lead testimonial.
    #[props(default)]
    pub featured: bool,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<figure>` or global attribute.
    #[props(extends = GlobalAttributes, extends = figure)]
    pub attributes: Vec<Attribute>,
    /// The quote.
    pub children: Element,
}

fn present(value: Option<&String>) -> Option<String> {
    value.filter(|text| !text.is_empty()).cloned()
}

/// A customer or peer quote with its author.
#[component]
pub fn Testimonial(props: TestimonialProps) -> Element {
    let cite = props.cite.as_deref().and_then(resolve_citation_href);
    let avatar_src = present(props.avatar_src.as_ref());
    let initials = present(props.initials.as_ref());
    let has_avatar = avatar_src.is_some() || initials.is_some();
    let class = compose([Some("ui-testimonial"), props.class.as_deref()]);

    rsx! {
        figure {
            class,
            "data-featured": props.featured.then_some("true"),
            ..props.attributes,
            span { aria_hidden: "true", class: "ui-testimonial__mark", "\u{201c}" }
            blockquote {
                cite,
                class: "ui-testimonial__quote",
                lang: props.quote_lang.clone(),
                {props.children}
            }
            figcaption { class: "ui-testimonial__caption",
                if has_avatar {
                    Avatar {
                        src: avatar_src,
                        fallback: initials.unwrap_or_default(),
                        alt: "",
                        aria_hidden: "true",
                        class: "ui-testimonial__avatar",
                    }
                }
                span { class: "ui-testimonial__identity",
                    span { class: "ui-testimonial__author", {props.author.clone()} }
                    if let Some(role) = present(props.author_role.as_ref()) {
                        span { class: "ui-testimonial__role", {role} }
                    }
                }
                if let Some(context) = present(props.context.as_ref()) {
                    span { class: "ui-testimonial__context", {context} }
                }
            }
        }
    }
}

/// Layout of a [`TestimonialGroup`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TestimonialGroupLayout {
    /// Features the first testimonial.
    #[default]
    Spotlight,
    /// Gives every testimonial equal weight.
    Grid,
}

impl TestimonialGroupLayout {
    /// The `data-layout` value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Spotlight => "spotlight",
            Self::Grid => "grid",
        }
    }
}

/// One testimonial rendered from data by a [`TestimonialGroup`].
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TestimonialGroupItem {
    /// Stable key when several items share an author.
    pub id: Option<String>,
    /// Person quoted.
    pub author: String,
    /// Author job title.
    pub author_role: Option<String>,
    /// Relationship or company context.
    pub context: Option<String>,
    /// Decorative portrait URL.
    pub avatar_src: Option<String>,
    /// Decorative avatar initials.
    pub initials: Option<String>,
    /// Source URL of the quote.
    pub cite: Option<String>,
    /// Language of the quote.
    pub quote_lang: Option<String>,
    /// The quote, rendered as a paragraph.
    pub quote: String,
}

impl TestimonialGroupItem {
    /// An item with an author and a quote.
    pub fn new(author: impl Into<String>, quote: impl Into<String>) -> Self {
        Self {
            author: author.into(),
            quote: quote.into(),
            ..Self::default()
        }
    }
}

/// Props for [`TestimonialGroup`].
#[derive(Props, Clone, PartialEq)]
pub struct TestimonialGroupProps {
    /// Renders testimonials from data. Without items, `children` are used.
    pub items: Option<Vec<TestimonialGroupItem>>,
    /// Accessible region name. The group is a landmark only when it has a label.
    #[props(into)]
    pub label: Option<String>,
    /// Spotlight or grid layout.
    #[props(default)]
    pub layout: TestimonialGroupLayout,
    /// Extra classes appended after the Lumen class.
    #[props(into)]
    pub class: Option<String>,
    /// Any other `<div>` or global attribute.
    #[props(extends = GlobalAttributes, extends = div)]
    pub attributes: Vec<Attribute>,
    /// [`Testimonial`]s, when `items` is not set.
    pub children: Element,
}

/// A spotlight or grid of testimonials.
#[component]
pub fn TestimonialGroup(props: TestimonialGroupProps) -> Element {
    let label = props
        .label
        .as_deref()
        .map(str::trim)
        .filter(|label| !label.is_empty())
        .map(str::to_string);
    let style = props.items.as_ref().map(|items| {
        format!(
            "--ui-testimonial-count: {}; --ui-testimonial-rows: {}",
            items.len(),
            items.len().saturating_sub(1).max(1)
        )
    });
    let spotlight = props.layout == TestimonialGroupLayout::Spotlight;
    let class = compose([Some("ui-testimonial-group"), props.class.as_deref()]);
    let role = label.is_some().then_some("region");

    rsx! {
        div {
            aria_label: label,
            class,
            "data-layout": props.layout.as_str(),
            role,
            style,
            ..props.attributes,
            if let Some(items) = props.items.clone() {
                for (index, item) in items.into_iter().enumerate() {
                    Testimonial {
                        key: "{index}:{item.id.clone().unwrap_or_else(|| item.author.clone())}",
                        author: item.author,
                        author_role: item.author_role,
                        context: item.context,
                        avatar_src: item.avatar_src,
                        initials: item.initials,
                        cite: item.cite,
                        quote_lang: item.quote_lang,
                        featured: spotlight && index == 0,
                        p { {item.quote} }
                    }
                }
            } else {
                {props.children}
            }
        }
    }
}