use dioxus::prelude::*;
use super::util::{attr, heading, heading_level, join_style};
use crate::class::compose;
use crate::hooks::use_id_or;
pub(crate) fn has_content(element: &Element) -> bool {
match element {
Ok(node) => *node != VNode::placeholder(),
Err(_) => true,
}
}
fn optional(element: Option<&Element>) -> Option<Element> {
element.filter(|content| has_content(content)).cloned()
}
#[derive(Clone, Copy, PartialEq)]
enum HeaderPart {
Page,
Section,
}
impl HeaderPart {
fn name(self) -> &'static str {
match self {
Self::Page => "page-header",
Self::Section => "section-header",
}
}
}
struct HeaderParts {
actions: Option<Element>,
body: Element,
count: Option<String>,
description: Option<Element>,
eyebrow: Option<Element>,
leading: Option<Element>,
level: u8,
part: HeaderPart,
title: Element,
}
fn header_parts(parts: HeaderParts) -> Element {
let part = parts.part.name();
let class = |name: &str| format!("ui-{part}__{name}");
let slot = |name: &str| format!("{part}-{name}");
let title_attributes = vec![
attr("class", class("title")),
attr("data-slot", slot("title")),
];
let title = parts.title;
let count = parts.count;
let title_content = rsx! {
{title}
if let Some(count) = count {
span { class: "ui-section-header__count", "data-slot": "section-header-count",
span { class: "ui-sr-only", " (" }
"{count}"
span { class: "ui-sr-only", ")" }
}
}
};
let has_body = has_content(&parts.body);
rsx! {
if let Some(leading) = parts.leading {
div { class: class("leading"), "data-slot": slot("leading"), {leading} }
}
div { class: class("content"), "data-slot": slot("content"),
if let Some(eyebrow) = parts.eyebrow {
p { class: format!("ui-eyebrow ui-{part}__eyebrow"), "data-slot": slot("eyebrow"), {eyebrow} }
}
{heading(parts.level, title_attributes, title_content)}
if let Some(description) = parts.description {
p { class: class("description"), "data-slot": slot("description"), {description} }
}
}
if let Some(actions) = parts.actions {
div { class: class("actions"), "data-slot": slot("actions"), {actions} }
}
if has_body {
div { class: class("body"), "data-slot": slot("body"), {parts.body} }
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct PageHeaderProps {
pub title: Element,
pub eyebrow: Option<Element>,
pub description: Option<Element>,
pub actions: Option<Element>,
pub leading: Option<Element>,
pub heading_level: Option<i64>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = header)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn PageHeader(props: PageHeaderProps) -> Element {
let class = compose([Some("ui-page-header"), props.class.as_deref()]);
let parts = header_parts(HeaderParts {
actions: optional(props.actions.as_ref()),
body: props.children,
count: None,
description: optional(props.description.as_ref()),
eyebrow: optional(props.eyebrow.as_ref()),
leading: optional(props.leading.as_ref()),
level: heading_level(props.heading_level, 1),
part: HeaderPart::Page,
title: props.title,
});
rsx! {
header { class, "data-slot": "page-header", ..props.attributes, {parts} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum SectionHeaderSize {
Sm,
#[default]
Md,
}
#[derive(Props, Clone, PartialEq)]
pub struct SectionHeaderProps {
pub title: Element,
#[props(default)]
pub size: SectionHeaderSize,
#[props(into)]
pub count: Option<String>,
pub eyebrow: Option<Element>,
pub description: Option<Element>,
pub actions: Option<Element>,
pub leading: Option<Element>,
pub heading_level: Option<i64>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = header)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn SectionHeader(props: SectionHeaderProps) -> Element {
let class = compose([
Some("ui-section-header"),
(props.size == SectionHeaderSize::Sm).then_some("ui-section-header--sm"),
props.class.as_deref(),
]);
let count = props
.count
.as_deref()
.map(str::trim)
.filter(|count| !count.is_empty())
.map(str::to_string);
let parts = header_parts(HeaderParts {
actions: optional(props.actions.as_ref()),
body: props.children,
count,
description: optional(props.description.as_ref()),
eyebrow: optional(props.eyebrow.as_ref()),
leading: optional(props.leading.as_ref()),
level: heading_level(props.heading_level, 2),
part: HeaderPart::Section,
title: props.title,
});
rsx! {
header { class, "data-slot": "section-header", ..props.attributes, {parts} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum HeroLayout {
#[default]
Split,
Centered,
}
impl HeroLayout {
pub fn as_str(self) -> &'static str {
match self {
Self::Split => "split",
Self::Centered => "centered",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct HeroProps {
pub title: Element,
pub eyebrow: Option<Element>,
pub description: Option<Element>,
pub actions: Option<Element>,
pub footnote: Option<Element>,
pub aside: Option<Element>,
#[props(default)]
pub layout: HeroLayout,
pub heading_level: Option<i64>,
#[props(into)]
pub id: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = section)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Hero(props: HeroProps) -> Element {
let base_id = use_id_or(props.id.as_deref());
let title_id = format!("{base_id}-title");
let layout = props.layout.as_str();
let layout_class = format!("ui-hero--{layout}");
let class = compose([
Some("ui-hero"),
Some(layout_class.as_str()),
props.class.as_deref(),
]);
let title_attributes = vec![
attr("class", "ui-hero__title"),
attr("data-slot", "hero-title"),
attr("id", title_id.clone()),
];
let title = heading(
heading_level(props.heading_level, 1),
title_attributes,
props.title,
);
let eyebrow = optional(props.eyebrow.as_ref());
let description = optional(props.description.as_ref());
let actions = optional(props.actions.as_ref());
let footnote = optional(props.footnote.as_ref());
let aside = optional(props.aside.as_ref());
let has_body = has_content(&props.children);
rsx! {
section {
aria_labelledby: title_id,
class,
"data-layout": layout,
"data-slot": "hero",
id: props.id,
..props.attributes,
div { class: "ui-hero__content", "data-slot": "hero-content",
if let Some(eyebrow) = eyebrow {
p { class: "ui-eyebrow ui-hero__eyebrow", "data-slot": "hero-eyebrow", {eyebrow} }
}
{title}
if let Some(description) = description {
p { class: "ui-hero__description", "data-slot": "hero-description", {description} }
}
if let Some(actions) = actions {
div { class: "ui-hero__actions", "data-slot": "hero-actions", {actions} }
}
if let Some(footnote) = footnote {
div { class: "ui-hero__footnote", "data-slot": "hero-footnote", {footnote} }
}
if has_body {
div { class: "ui-hero__body", "data-slot": "hero-body", {props.children} }
}
}
if let Some(aside) = aside {
div { class: "ui-hero__aside", "data-slot": "hero-aside", {aside} }
}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum StepsLayout {
#[default]
List,
Rail,
}
impl StepsLayout {
pub fn as_str(self) -> &'static str {
match self {
Self::List => "list",
Self::Rail => "rail",
}
}
}
const MAX_STEPS_START: i64 = 1_000_000;
pub(crate) fn steps_start(start: Option<i64>) -> i64 {
start
.filter(|value| (-MAX_STEPS_START..=MAX_STEPS_START).contains(value))
.unwrap_or(1)
}
#[derive(Props, Clone, PartialEq)]
pub struct StepsProps {
#[props(default)]
pub layout: StepsLayout,
pub start: Option<i64>,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = ol)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Steps(props: StepsProps) -> Element {
let layout = props.layout.as_str();
let layout_class = format!("ui-steps--{layout}");
let class = compose([
Some("ui-steps"),
Some(layout_class.as_str()),
props.class.as_deref(),
]);
let start = steps_start(props.start);
let offset = (start != 1).then(|| format!("--ui-steps-offset: {}", start - 1));
let style = join_style([props.style.as_deref(), offset.as_deref()]);
rsx! {
ol {
class,
"data-layout": layout,
"data-slot": "steps",
role: "list",
start: (start != 1).then(|| start.to_string()),
style,
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct StepsItemProps {
pub title: Element,
pub marker: Option<Element>,
#[props(default)]
pub announce_marker: bool,
pub heading_level: Option<i64>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = li)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn StepsItem(props: StepsItemProps) -> Element {
let marker = optional(props.marker.as_ref());
let hidden = !(marker.is_some() && props.announce_marker);
let class = compose([Some("ui-steps__item"), props.class.as_deref()]);
let title = has_content(&props.title).then(|| {
heading(
heading_level(props.heading_level, 3),
vec![
attr("class", "ui-steps__title"),
attr("data-slot", "steps-title"),
],
props.title.clone(),
)
});
let has_body = has_content(&props.children);
rsx! {
li { class, "data-slot": "steps-item", ..props.attributes,
span {
aria_hidden: hidden.then_some("true"),
class: "ui-steps__marker",
"data-slot": "steps-marker",
{marker}
}
div { class: "ui-steps__content", "data-slot": "steps-content",
{title}
if has_body {
div { class: "ui-steps__body", "data-slot": "steps-body", {props.children} }
}
}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum FeatureGridVariant {
#[default]
Plain,
Card,
}
impl FeatureGridVariant {
pub fn as_str(self) -> &'static str {
match self {
Self::Plain => "plain",
Self::Card => "card",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct FeatureGridProps {
#[props(default = 3)]
pub columns: u8,
#[props(default)]
pub variant: FeatureGridVariant,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = ul)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn FeatureGrid(props: FeatureGridProps) -> Element {
let columns = props.columns.clamp(2, 4).to_string();
let variant = props.variant.as_str();
let columns_class = format!("ui-feature-grid--columns-{columns}");
let variant_class = format!("ui-feature-grid--{variant}");
let class = compose([
Some("ui-feature-grid"),
Some(columns_class.as_str()),
Some(variant_class.as_str()),
props.class.as_deref(),
]);
rsx! {
ul {
class,
"data-columns": columns,
"data-slot": "feature-grid",
"data-variant": variant,
role: "list",
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct FeatureGridItemProps {
pub title: Element,
pub description: Option<Element>,
#[props(into)]
pub href: Option<String>,
pub icon: Option<Element>,
pub index: Option<Element>,
pub heading_level: Option<i64>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = li)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn FeatureGridItem(props: FeatureGridItemProps) -> Element {
let class = compose([Some("ui-feature-grid__item"), props.class.as_deref()]);
let icon = optional(props.icon.as_ref());
let index = if icon.is_none() {
optional(props.index.as_ref())
} else {
None
};
let description = optional(props.description.as_ref());
let title_content = match props.href.clone() {
Some(href) => rsx! {
a { class: "ui-feature-grid__link", "data-slot": "feature-grid-link", href, {props.title.clone()} }
},
None => props.title.clone(),
};
let title = heading(
heading_level(props.heading_level, 3),
vec![
attr("class", "ui-feature-grid__title"),
attr("data-slot", "feature-grid-title"),
],
title_content,
);
let has_body = has_content(&props.children);
rsx! {
li { class, "data-slot": "feature-grid-item", ..props.attributes,
if let Some(icon) = icon {
span { aria_hidden: "true", class: "ui-feature-grid__icon", "data-slot": "feature-grid-icon", {icon} }
}
if let Some(index) = index {
span { aria_hidden: "true", class: "ui-feature-grid__index", "data-slot": "feature-grid-index", {index} }
}
{title}
if let Some(description) = description {
p { class: "ui-feature-grid__description", "data-slot": "feature-grid-description", {description} }
}
if has_body {
div { class: "ui-feature-grid__body", "data-slot": "feature-grid-body", {props.children} }
}
}
}
}
#[cfg(test)]
mod tests {
use super::steps_start;
#[test]
fn steps_start_falls_back_outside_the_range() {
assert_eq!(steps_start(None), 1);
assert_eq!(steps_start(Some(3)), 3);
assert_eq!(steps_start(Some(-2)), -2);
assert_eq!(steps_start(Some(1_000_000)), 1_000_000);
assert_eq!(steps_start(Some(1_000_001)), 1);
assert_eq!(steps_start(Some(i64::MIN)), 1);
}
}