use pagetop::prelude::*; use crate::prelude::*; /// Componente para crear un **contenedor de componentes**. /// /// Envuelve un contenido con la etiqueta HTML indicada por [`container::Kind`]. Sólo se renderiza /// si existen componentes hijos (*children*). #[rustfmt::skip] #[derive(AutoDefault)] pub struct Container { id : AttrId, classes : AttrClasses, container_kind : container::Kind, container_width: container::Width, children : Children, } impl Component for Container { fn new() -> Self { Container::default() } fn id(&self) -> Option { self.id.get() } fn setup_before_prepare(&mut self, _cx: &mut Context) { self.alter_classes( ClassesOp::Prepend, [join_pair!( "container", "-", match self.width() { container::Width::Default => String::new(), container::Width::From(bp) => bp.to_string(), container::Width::Fluid => "fluid".to_string(), container::Width::FluidMax(_) => "fluid".to_string(), } )] .join_classes(), ); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { let output = self.children().render(cx); if output.is_empty() { return PrepareMarkup::None; } let style = match self.width() { container::Width::FluidMax(w) if w.is_measurable() => { Some(join!("max-width: ", w.to_string(), ";")) } _ => None, }; match self.container_kind() { container::Kind::Default => PrepareMarkup::With(html! { div id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), container::Kind::Main => PrepareMarkup::With(html! { main id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), container::Kind::Header => PrepareMarkup::With(html! { header id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), container::Kind::Footer => PrepareMarkup::With(html! { footer id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), container::Kind::Section => PrepareMarkup::With(html! { section id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), container::Kind::Article => PrepareMarkup::With(html! { article id=[self.id()] class=[self.classes().get()] style=[style] { (output) } }), } } } impl Container { /// Crea un contenedor de tipo `Main` (`
`). pub fn main() -> Self { Container { container_kind: container::Kind::Main, ..Default::default() } } /// Crea un contenedor de tipo `Header` (`
`). pub fn header() -> Self { Container { container_kind: container::Kind::Header, ..Default::default() } } /// Crea un contenedor de tipo `Footer` (`