This commit is contained in:
JMARyA 2025-01-15 18:28:59 +01:00
parent 8208fa8899
commit ed739d792f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
35 changed files with 1675 additions and 447 deletions

77
src/ui/primitives/link.rs Normal file
View file

@ -0,0 +1,77 @@
use std::collections::HashMap;
use maud::{Markup, PreEscaped, Render};
use crate::ui::{
AttrExtendable, UIWidget,
htmx::{HTMXAttributes, Selector, SwapStrategy},
};
#[allow(non_snake_case)]
/// A component for fixing an element's width to the current breakpoint.
pub fn Link<T: UIWidget + 'static>(reference: &str, inner: T) -> LinkWidget {
LinkWidget(Box::new(inner), reference.to_owned(), HashMap::new())
}
pub struct LinkWidget(Box<dyn UIWidget>, String, HashMap<String, String>);
impl AttrExtendable for LinkWidget {
fn add_attr(mut self, key: &str, val: &str) -> Self {
self.2.insert(key.to_string(), val.to_string());
self
}
fn id(self, id: &str) -> Self {
self.add_attr("id", id)
}
}
impl HTMXAttributes for LinkWidget {}
impl Render for LinkWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for LinkWidget {
fn can_inherit(&self) -> bool {
true
}
fn base_class(&self) -> Vec<String> {
vec![]
}
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 {
let attrs = self
.2
.iter()
.map(|(k, v)| format!("{k}='{v}'"))
.collect::<Vec<_>>()
.join(" ");
PreEscaped(format!(
"<a href='{}' class='{class}' {attrs}> {} </a>",
self.1,
self.0.render().0
))
}
}
impl LinkWidget {
/// Enable HTMX link capabilities
pub fn use_htmx(self) -> Self {
let url = self.1.clone();
self.hx_get(&url)
.hx_target(Selector::Query("#main_content".to_string()))
.hx_push_url()
.hx_swap(SwapStrategy::innerHTML)
}
}