use super::*;
#[component]
pub fn euv_grid(node: VirtualNode<EuvGridProps>) -> VirtualNode {
let EuvGridProps { cols, gutter }: EuvGridProps = node.try_get_props().unwrap_or_default();
let children: Vec<VirtualNode> = node.get_children().to_vec();
let columns: Vec<VirtualNode> = children
.into_iter()
.enumerate()
.map(|(index, content): (usize, euv::VirtualNode)| {
let col: EuvGridCol = cols.get(index).copied().unwrap_or_default();
grid_column(col, content, gutter)
})
.collect();
html! {
div {
class: if gutter {
c_euv_grid_gutter()
} else {
c_euv_grid()
}
columns
}
}
}
fn grid_column(col: EuvGridCol, content: VirtualNode, gutter: bool) -> VirtualNode {
let span: u32 = col.span.clamp(1, 24);
let width: u64 = u64::from(span) * 100 / 24;
let offset: u64 = u64::from(col.offset) * 100 / 24;
let style: String = if col.offset > 0 {
format!("flex: 0 0 {width}%; margin-left: {offset}%;")
} else {
format!("flex: 0 0 {width}%;")
};
let style_ref: &str = style.as_str();
let offset: bool = col.offset > 0;
html! {
div {
class: match (gutter, offset) {
(true, true) => c_euv_grid_gutter_col_offset(),
(true, false) => c_euv_grid_gutter_col(),
(false, true) => c_euv_grid_col_offset(),
(false, false) => c_euv_grid_col(),
}
style: style_ref
content
}
}
}