based/src/ui/primitives/container.rs
2025-01-15 18:28:59 +01:00

47 lines
1.1 KiB
Rust

use maud::{Markup, Render, html};
use crate::ui::UIWidget;
#[allow(non_snake_case)]
/// A component for fixing an element's width to the current breakpoint.
pub fn Container<T: UIWidget + 'static>(inner: T) -> ContainerWidget {
ContainerWidget(Box::new(inner))
}
pub struct ContainerWidget(Box<dyn UIWidget>);
impl Render for ContainerWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for ContainerWidget {
fn can_inherit(&self) -> bool {
true
}
fn base_class(&self) -> Vec<String> {
vec!["container".to_string()]
}
fn extended_class(&self) -> Vec<String> {
let mut c = self.base_class();
c.extend_from_slice(&self.0.extended_class());
c
}
fn render_with_class(&self, class: &str) -> Markup {
if self.0.as_ref().can_inherit() {
self.0
.as_ref()
.render_with_class(&format!("container {class}"))
} else {
html! {
div class=(format!("container {class}")) {
(self.0.as_ref())
}
}
}
}
}