use leptos::{prelude::*, tachys::view::any_view::IntoAny};
use turf::inline_style_sheet_values;
use crate::{BorderRadius, SpacingInset, StrokeWidth, ThemeColor};
#[component]
pub fn DemoBox(
#[prop(optional, into)]
label: MaybeProp<String>,
#[prop(optional, into)]
class: MaybeProp<String>,
#[prop(optional, into)]
width: MaybeProp<String>,
#[prop(optional, into)]
height: MaybeProp<String>,
#[prop(optional, into)]
padding: MaybeProp<SpacingInset>,
#[prop(optional, default = false)]
fill: bool,
#[prop(optional, into)]
data_testid: MaybeProp<String>,
#[prop(optional)] children: Option<Children>,
) -> impl IntoView {
let (style_sheet, class_names) = inline_style_sheet_values! {
.Root {
box-sizing: border-box;
}
};
let merged_style = move || {
let mut parts = Vec::new();
if fill {
parts.push("width: 100%".to_string());
parts.push("height: 100%".to_string());
} else {
if let Some(w) = width.get() {
if !w.is_empty() {
parts.push(format!("width: {w}"));
}
}
if let Some(h) = height.get() {
if !h.is_empty() {
parts.push(format!("height: {h}"));
}
}
}
let pad = padding.get().unwrap_or(SpacingInset::all_m());
parts.push(pad.padding_css().trim_end_matches(';').to_string());
parts.push(format!(
"border: {} dashed {}",
StrokeWidth::Thin.css_var(),
ThemeColor::NeutralStroke1.css_var()
));
parts.push(format!("border-radius: {}", BorderRadius::Medium.css_var()));
parts.push(format!(
"color: {}",
ThemeColor::NeutralForeground1.css_var()
));
parts.join("; ")
};
view! {
<style>{style_sheet}</style>
<div
class=move || {
match class.get() {
Some(extra) if !extra.trim().is_empty() => {
format!("{} {}", class_names.root, extra)
}
_ => class_names.root.to_string(),
}
}
style=merged_style
data-testid=move || data_testid.get()
>
{if let Some(children) = children {
children().into_any()
} else if let Some(label_text) = label.get() {
view! { {label_text} }.into_any()
} else {
().into_any()
}}
</div>
}
}