use super::*;
pub fn skeleton_line_count(count: usize) -> usize {
count.clamp(SKELETON_LINES_MIN, SKELETON_LINES_MAX)
}
#[component]
pub fn euv_skeleton(node: VirtualNode<EuvSkeletonProps>) -> VirtualNode {
let EuvSkeletonProps {
lines,
width,
height,
rounded,
}: EuvSkeletonProps = node.try_get_props().unwrap_or_default();
let mut block: Vec<VirtualNode> = Vec::with_capacity(skeleton_line_count(lines));
for index in 0..skeleton_line_count(lines) {
block.push(skeleton_line(index, width, height, rounded));
}
html! {
div {
class: c_euv_skeleton()
block
}
}
}
fn skeleton_line(
index: usize,
width: &'static str,
height: &'static str,
rounded: Signal<bool>,
) -> VirtualNode {
let mut style: String = String::with_capacity(64);
let line_width: &str = skeleton_width(width);
style.push_str("width: ");
style.push_str(line_width);
style.push(';');
if !height.is_empty() {
style.push_str(" height: ");
style.push_str(height);
style.push(';');
}
if rounded.get() {
style.push_str(" border-radius: ");
style.push_str(SKELETON_RADIUS);
style.push(';');
}
let style_ref: &str = style.as_str();
html! {
div {
key: index.to_string()
class: c_euv_skeleton_line()
style: style_ref
}
}
}
fn skeleton_width(width: &'static str) -> &'static str {
let is_percent: bool = match width.strip_suffix('%') {
Some(digits) => !digits.is_empty() && digits.bytes().all(|byte: u8| byte.is_ascii_digit()),
None => false,
};
if is_percent {
width
} else {
SKELETON_WIDTH_DEFAULT
}
}