use dioxus::prelude::*;
use super::illustration_data::{IllustrationShape, IllustrationVariant};
use super::shared::js_number;
use crate::class::compose;
fn has_label(label: Option<&str>) -> bool {
label.is_some_and(|text| !text.is_empty())
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropVariant {
#[default]
Aurora,
Dots,
Grid,
Rays,
}
impl BackdropVariant {
pub fn as_str(self) -> &'static str {
match self {
Self::Aurora => "aurora",
Self::Dots => "dots",
Self::Grid => "grid",
Self::Rays => "rays",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropTone {
Accent,
#[default]
Brand,
Neutral,
}
impl BackdropTone {
pub fn as_str(self) -> &'static str {
match self {
Self::Accent => "accent",
Self::Brand => "brand",
Self::Neutral => "neutral",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum BackdropIntensity {
Subtle,
#[default]
Medium,
Strong,
}
impl BackdropIntensity {
pub fn as_str(self) -> &'static str {
match self {
Self::Subtle => "subtle",
Self::Medium => "medium",
Self::Strong => "strong",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct BackdropProps {
#[props(default)]
pub intensity: BackdropIntensity,
#[props(default)]
pub tone: BackdropTone,
#[props(default)]
pub variant: BackdropVariant,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Backdrop(props: BackdropProps) -> Element {
let variant = format!("ui-backdrop--{}", props.variant.as_str());
let tone = format!("ui-backdrop--{}", props.tone.as_str());
let intensity = format!("ui-backdrop--{}", props.intensity.as_str());
let class = compose([
Some("ui-backdrop"),
Some(variant.as_str()),
Some(tone.as_str()),
Some(intensity.as_str()),
props.class.as_deref(),
]);
rsx! {
div { class, ..props.attributes,
div { class: "ui-backdrop__content", {props.children} }
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicSize {
Sm,
#[default]
Md,
Lg,
}
impl GraphicSize {
pub fn as_str(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicTone {
Accent,
#[default]
Brand,
Neutral,
}
impl GraphicTone {
pub fn as_str(self) -> &'static str {
match self {
Self::Accent => "accent",
Self::Brand => "brand",
Self::Neutral => "neutral",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum GraphicVariant {
Glow,
Grid,
#[default]
Orbit,
}
impl GraphicVariant {
pub fn as_str(self) -> &'static str {
match self {
Self::Glow => "glow",
Self::Grid => "grid",
Self::Orbit => "orbit",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct GraphicProps {
#[props(into)]
pub label: Option<String>,
#[props(default)]
pub size: GraphicSize,
#[props(default)]
pub tone: GraphicTone,
#[props(default)]
pub variant: GraphicVariant,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Graphic(props: GraphicProps) -> Element {
let labelled = has_label(props.label.as_deref());
let variant = format!("ui-graphic--{}", props.variant.as_str());
let tone = format!("ui-graphic--{}", props.tone.as_str());
let size = format!("ui-graphic--{}", props.size.as_str());
let class = compose([
Some("ui-graphic"),
Some(variant.as_str()),
Some(tone.as_str()),
Some(size.as_str()),
props.class.as_deref(),
]);
rsx! {
div {
aria_hidden: (!labelled).then_some("true"),
aria_label: props.label,
class,
role: labelled.then_some("img"),
..props.attributes,
div { class: "ui-graphic__content", {props.children} }
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum AnimatedLogoAnimation {
#[default]
Reveal,
Sequence,
}
impl AnimatedLogoAnimation {
pub fn as_str(self) -> &'static str {
match self {
Self::Reveal => "reveal",
Self::Sequence => "sequence",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct AnimatedLogoProps {
#[props(default)]
pub animation: AnimatedLogoAnimation,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = span)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn AnimatedLogo(props: AnimatedLogoProps) -> Element {
let class = compose([Some("ui-animated-logo"), props.class.as_deref()]);
rsx! {
span {
class,
"data-animation": props.animation.as_str(),
"data-ui-animated-logo": "true",
..props.attributes,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct AnimatedPortraitProps {
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn AnimatedPortrait(props: AnimatedPortraitProps) -> Element {
let class = compose([
Some(
"ui-animated-portrait relative isolate w-full animate-reveal-lcp motion-reduce:animate-none",
),
props.class.as_deref(),
]);
rsx! {
div { class, ..props.attributes, {props.children} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum IllustrationSize {
Sm,
#[default]
Md,
Lg,
}
impl IllustrationSize {
pub fn as_str(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum IllustrationTone {
Accent,
#[default]
Auto,
Brand,
Neutral,
}
impl IllustrationTone {
pub fn as_str(self) -> &'static str {
match self {
Self::Accent => "accent",
Self::Auto => "auto",
Self::Brand => "brand",
Self::Neutral => "neutral",
}
}
}
fn point_list(values: &[f64]) -> String {
let (pairs, _) = values.as_chunks::<2>();
pairs
.iter()
.map(|[x, y]| format!("{},{}", js_number(*x), js_number(*y)))
.collect::<Vec<_>>()
.join(" ")
}
fn render_shape(shape: IllustrationShape) -> Element {
match shape {
IllustrationShape::Circle { cx, cy, r } => rsx! {
circle { cx: js_number(cx), cy: js_number(cy), r: js_number(r) }
},
IllustrationShape::RoundedRect {
x,
y,
width,
height,
radius,
} => rsx! {
rect {
height: js_number(height),
rx: js_number(radius),
width: js_number(width),
x: js_number(x),
y: js_number(y),
}
},
IllustrationShape::Line(points) => {
let coordinate = |index: usize| points.get(index).copied().map(js_number);
rsx! {
line { x1: coordinate(0), x2: coordinate(2), y1: coordinate(1), y2: coordinate(3) }
}
}
IllustrationShape::Polygon(points) => rsx! {
polygon { points: point_list(points) }
},
IllustrationShape::Polyline(points) => rsx! {
polyline { points: point_list(points) }
},
}
}
#[derive(Props, Clone, PartialEq)]
pub struct IllustrationProps {
#[props(into)]
pub label: Option<String>,
#[props(default)]
pub size: IllustrationSize,
#[props(default)]
pub tone: IllustrationTone,
#[props(default)]
pub variant: IllustrationVariant,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = span)]
pub attributes: Vec<Attribute>,
}
#[component]
pub fn Illustration(props: IllustrationProps) -> Element {
let labelled = has_label(props.label.as_deref());
let variant = format!("ui-illustration--{}", props.variant.as_str());
let tone = format!("ui-illustration--{}", props.tone.as_str());
let size = format!("ui-illustration--{}", props.size.as_str());
let class = compose([
Some("ui-illustration"),
Some(variant.as_str()),
Some(tone.as_str()),
Some(size.as_str()),
props.class.as_deref(),
]);
rsx! {
span {
aria_hidden: (!labelled).then_some("true"),
aria_label: props.label,
class,
role: labelled.then_some("img"),
..props.attributes,
svg { "aria-hidden": "true", fill: "none", view_box: "0 0 120 120",
circle { class: "ui-illustration__wash", cx: "60", cy: "60", r: "48" }
for shape in props.variant.shapes() {
{render_shape(*shape)}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::point_list;
#[test]
fn point_lists_pair_coordinates() {
assert_eq!(point_list(&[1.0, 2.0, 3.5, 4.0]), "1,2 3.5,4");
assert_eq!(point_list(&[1.0, 2.0, 3.0]), "1,2");
assert_eq!(point_list(&[]), "");
}
}