Añade estilos en línea para los componentes

This commit is contained in:
Manuel Cillero 2022-07-03 20:26:49 +02:00
parent 2cb23317a8
commit 37dbcaa84a
4 changed files with 88 additions and 14 deletions

View file

@ -77,7 +77,7 @@ impl ComponentTrait for Anchor {
target=[target] target=[target]
{ {
(self.left_icon().render(context)) (self.left_icon().render(context))
(" ")(*self.html())(" ") (*self.html())
(self.right_icon().render(context)) (self.right_icon().render(context))
} }
} }
@ -195,13 +195,13 @@ impl Anchor {
pub fn alter_left_icon(&mut self, icon: Icon) -> &mut Self { pub fn alter_left_icon(&mut self, icon: Icon) -> &mut Self {
self.left_icon.clear(); self.left_icon.clear();
self.left_icon.add(icon); self.left_icon.add(icon.with_inline_style("margin-right", Some("5px")));
self self
} }
pub fn alter_right_icon(&mut self, icon: Icon) -> &mut Self { pub fn alter_right_icon(&mut self, icon: Icon) -> &mut Self {
self.right_icon.clear(); self.right_icon.clear();
self.right_icon.add(icon); self.right_icon.add(icon.with_inline_style("margin-left", Some("5px")));
self self
} }

View file

@ -3,17 +3,19 @@ use crate::prelude::*;
pub const ICON_COMPONENT: &str = "pagetop::component::icon"; pub const ICON_COMPONENT: &str = "pagetop::component::icon";
pub struct Icon { pub struct Icon {
renderable: fn() -> bool, renderable : fn() -> bool,
weight : isize, weight : isize,
icon_name : String, classes : Classes,
inline_styles: InlineStyles,
} }
impl ComponentTrait for Icon { impl ComponentTrait for Icon {
fn new() -> Self { fn new() -> Self {
Icon { Icon {
renderable: render_always, renderable : render_always,
weight : 0, weight : 0,
icon_name : "question-circle-fill".to_owned(), classes : Classes::new_with_default("bi-question-circle-fill"),
inline_styles: InlineStyles::new(),
} }
} }
@ -35,8 +37,7 @@ impl ComponentTrait for Icon {
"/theme/icons/bootstrap-icons.css?ver=1.8.2" "/theme/icons/bootstrap-icons.css?ver=1.8.2"
)); ));
let name = concat_string!("bi-", self.icon_name); html! { i class=[self.classes()] style=[self.inline_styles()] {}; }
html! { i class=(name) {}; }
} }
fn as_ref_any(&self) -> &dyn AnyComponent { fn as_ref_any(&self) -> &dyn AnyComponent {
@ -70,6 +71,16 @@ impl Icon {
self self
} }
pub fn with_classes(mut self, classes: &str, op: ClassesOp) -> Self {
self.alter_classes(classes, op);
self
}
pub fn with_inline_style(mut self, style: &str, value: Option<&str>) -> Self {
self.alter_inline_style(style, value);
self
}
// Icon ALTER. // Icon ALTER.
pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self { pub fn alter_renderable(&mut self, renderable: fn() -> bool) -> &mut Self {
@ -83,13 +94,27 @@ impl Icon {
} }
pub fn alter_icon_name(&mut self, name: &str) -> &mut Self { pub fn alter_icon_name(&mut self, name: &str) -> &mut Self {
self.icon_name = name.to_owned(); self.classes.alter(concat_string!("bi-", name).as_str(), ClassesOp::SetDefault);
self
}
pub fn alter_classes(&mut self, classes: &str, op: ClassesOp) -> &mut Self {
self.classes.alter(classes, op);
self
}
pub fn alter_inline_style(&mut self, style: &str, value: Option<&str>) -> &mut Self {
self.inline_styles.add_style(style, value);
self self
} }
// Icon GETTERS. // Icon GETTERS.
pub fn icon_name(&self) -> &str { pub fn classes(&self) -> &Option<String> {
&self.icon_name self.classes.option()
}
pub fn inline_styles(&self) -> Option<String> {
self.inline_styles.option()
} }
} }

View file

@ -16,3 +16,6 @@ pub use optattr::OptAttr;
mod classes; mod classes;
pub use classes::{Classes, ClassesOp}; pub use classes::{Classes, ClassesOp};
mod inline_styles;
pub use inline_styles::InlineStyles;

View file

@ -0,0 +1,46 @@
use crate::concat_string;
struct Style {
property: String,
inline : String,
}
pub struct InlineStyles(Vec<Style>);
impl InlineStyles {
pub fn new() -> Self {
InlineStyles(Vec::new())
}
pub fn add_style(&mut self, property: &str, value: Option<&str>) -> &Self {
let property = property.trim();
match self.0.iter().position(|s| s.property.eq(property)) {
Some(pos) => {
self.0.remove(pos);
if let Some(value) = value {
self.0.insert(pos, Style {
property: property.to_owned(),
inline : concat_string!(property, ":", value.trim(), ";"),
});
}
},
_ => if let Some(value) = value {
self.0.push(Style {
property: property.to_owned(),
inline : concat_string!(property, ":", value.trim(), ";"),
});
}
}
self
}
pub fn option(&self) -> Option<String> {
if self.0.len() == 0 {
None
} else {
let mut inline = "".to_owned();
self.0.iter().for_each(|s| inline.push_str(s.inline.as_str()));
Some(inline)
}
}
}