This commit is contained in:
JMARyA 2025-01-15 18:53:55 +01:00
parent ed739d792f
commit e9a9dad037
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
19 changed files with 278 additions and 177 deletions

View file

@ -8,6 +8,7 @@ use crate::ui::{AttrExtendable, UIWidget, htmx::HTMXAttributes};
/// `<div>` element
///
/// Useful for grouping values together
#[must_use]
pub fn Div() -> DivWidget {
DivWidget(Vec::new(), false, HashMap::new())
}
@ -16,7 +17,7 @@ pub struct DivWidget(Vec<Box<dyn UIWidget>>, bool, HashMap<String, String>);
impl AttrExtendable for DivWidget {
fn add_attr(mut self, key: &str, val: &str) -> Self {
self.2.insert(key.to_string(), val.to_string());
self.2.insert(key.to_string(), val.replace('\'', "\\'"));
self
}
@ -27,7 +28,8 @@ impl AttrExtendable for DivWidget {
impl DivWidget {
/// Add an element to the `<div>`
pub fn add<T: UIWidget + 'static>(mut self, element: T) -> Self {
#[must_use]
pub fn push<T: UIWidget + 'static>(mut self, element: T) -> Self {
self.0.push(Box::new(element));
self
}
@ -39,9 +41,10 @@ impl DivWidget {
/// ```ignore
/// use based::ui::basic::*;
///
/// let div = Div().add_some(Some("hello"), |value| Text(value));
/// let div = Div().push(Some("hello"), |value| Text(value));
/// ```
pub fn add_some<T: UIWidget + 'static, X, U: Fn(&X) -> T>(
#[must_use]
pub fn push_some<T: UIWidget + 'static, X, U: Fn(&X) -> T>(
mut self,
option: Option<&X>,
then: U,
@ -55,7 +58,8 @@ impl DivWidget {
/// Extract the `<div>`s innerHTML
///
/// This will render `<content>` instead of `<div> <content> </div>`
pub fn vanish(mut self) -> Self {
#[must_use]
pub const fn vanish(mut self) -> Self {
self.1 = true;
self
}

View file

@ -20,17 +20,20 @@ impl Render for FlexWidget {
}
impl FlexWidget {
#[must_use]
pub fn full_center(mut self) -> Self {
self.1.push("items-center".to_owned());
self.1.push("justify-center".to_owned());
self
}
pub fn group(mut self) -> Self {
#[must_use]
pub const fn group(mut self) -> Self {
self.2 = true;
self
}
#[must_use]
pub fn justify(mut self, value: Justify) -> Self {
let class = match value {
Justify::Center => "justify-center".to_owned(),
@ -41,11 +44,13 @@ impl FlexWidget {
self
}
#[must_use]
pub fn items_center(mut self) -> Self {
self.1.push("items-center".to_owned());
self
}
#[must_use]
pub fn gap(mut self, amount: u32) -> Self {
self.1.push(format!("gap-{amount}"));
self

View file

@ -2,6 +2,7 @@ use crate::ui::UIWidget;
use maud::{Markup, Render, html};
#[allow(non_snake_case)]
#[must_use]
pub fn Image(src: &str) -> ImageWidget {
ImageWidget {
src: src.to_owned(),
@ -21,8 +22,9 @@ impl Render for ImageWidget {
}
impl ImageWidget {
#[must_use]
pub fn alt(mut self, alt: &str) -> Self {
self.alt = alt.to_owned();
self.alt = alt.to_string();
self
}
}

View file

@ -17,7 +17,7 @@ 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.2.insert(key.to_string(), val.replace('\'', "\\'"));
self
}
@ -67,6 +67,7 @@ impl UIWidget for LinkWidget {
impl LinkWidget {
/// Enable HTMX link capabilities
#[must_use]
pub fn use_htmx(self) -> Self {
let url = self.1.clone();
self.hx_get(&url)

View file

@ -18,6 +18,7 @@ pub mod text;
pub mod width;
#[allow(non_snake_case)]
#[must_use]
pub fn Nothing() -> PreEscaped<String> {
html! {}
}
@ -54,7 +55,8 @@ pub enum Size {
}
impl Size {
pub fn to_string(&self) -> &str {
#[must_use]
pub const fn to_value(&self) -> &str {
match self {
Self::None => "none",
Self::Small => "sm",
@ -87,22 +89,23 @@ pub enum Side {
}
impl Side {
pub fn to_string(&self) -> &str {
#[must_use]
pub const fn to_value(&self) -> &str {
match self {
Side::Start => "s",
Side::End => "e",
Side::Top => "t",
Side::Right => "r",
Side::Bottom => "b",
Side::Left => "l",
Side::StartStart => "ss",
Side::StartEnd => "se",
Side::EndEnd => "ee",
Side::EndStart => "es",
Side::TopLeft => "tl",
Side::TopRight => "tr",
Side::BottomRight => "br",
Side::BottomLeft => "bl",
Self::Start => "s",
Self::End => "e",
Self::Top => "t",
Self::Right => "r",
Self::Bottom => "b",
Self::Left => "l",
Self::StartStart => "ss",
Self::StartEnd => "se",
Self::EndEnd => "ee",
Self::EndStart => "es",
Self::TopLeft => "tl",
Self::TopRight => "tr",
Self::BottomRight => "br",
Self::BottomLeft => "bl",
}
}
}

View file

@ -23,17 +23,20 @@ pub struct PaddingWidget {
}
impl PaddingWidget {
pub fn right(mut self, right: u32) -> Self {
#[must_use]
pub const fn right(mut self, right: u32) -> Self {
self.right = Some(right);
self
}
pub fn y(mut self, y: u32) -> Self {
#[must_use]
pub const fn y(mut self, y: u32) -> Self {
self.y = Some(y);
self
}
pub fn x(mut self, x: u32) -> Self {
#[must_use]
pub const fn x(mut self, x: u32) -> Self {
self.x = Some(x);
self
}

View file

@ -11,12 +11,14 @@ pub fn Rounded<T: UIWidget + 'static>(inner: T) -> RoundedWidget {
pub struct RoundedWidget(Box<dyn UIWidget>, Option<Size>, Option<Side>);
impl RoundedWidget {
pub fn size(mut self, size: Size) -> Self {
#[must_use]
pub const fn size(mut self, size: Size) -> Self {
self.1 = Some(size);
self
}
pub fn side(mut self, side: Side) -> Self {
#[must_use]
pub const fn side(mut self, side: Side) -> Self {
self.2 = Some(side);
self
}
@ -36,12 +38,10 @@ impl UIWidget for RoundedWidget {
fn base_class(&self) -> Vec<String> {
if let Some(side) = &self.2 {
if let Some(size) = &self.1 {
return vec![format!("rounded-{}-{}", side.to_string(), size.to_string())];
}
} else {
if let Some(size) = &self.1 {
return vec![format!("rounded-{}", size.to_string())];
return vec![format!("rounded-{}-{}", side.to_value(), size.to_value())];
}
} else if let Some(size) = &self.1 {
return vec![format!("rounded-{}", size.to_value())];
}
vec!["rounded".to_owned()]
}

View file

@ -4,36 +4,36 @@ use maud::{Markup, Render, html};
pub struct Shadow(Box<dyn UIWidget>, String);
impl Shadow {
pub fn medium<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "md".to_owned())
pub fn medium<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "md".to_owned())
}
pub fn small<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "sm".to_owned())
pub fn small<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "sm".to_owned())
}
pub fn regular<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), String::new())
pub fn regular<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), String::new())
}
pub fn large<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "lg".to_owned())
pub fn large<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "lg".to_owned())
}
pub fn none<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "none".to_owned())
pub fn none<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "none".to_owned())
}
pub fn xl<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "xl".to_owned())
pub fn xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "xl".to_owned())
}
pub fn _2xl<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "2xl".to_owned())
pub fn _2xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "2xl".to_owned())
}
pub fn inner<T: UIWidget + 'static>(inner: T) -> Shadow {
Shadow(Box::new(inner), "inner".to_owned())
pub fn inner<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "inner".to_owned())
}
}

View file

@ -16,12 +16,14 @@ impl Render for SpaceBetweenWidget {
}
impl SpaceBetweenWidget {
pub fn x(mut self, x: ScreenValue) -> Self {
#[must_use]
pub const fn x(mut self, x: ScreenValue) -> Self {
self.1 = Some(x);
self
}
pub fn y(mut self, y: ScreenValue) -> Self {
#[must_use]
pub const fn y(mut self, y: ScreenValue) -> Self {
self.2 = Some(y);
self
}
@ -36,11 +38,11 @@ impl UIWidget for SpaceBetweenWidget {
let mut ret = Vec::new();
if let Some(x) = &self.1 {
ret.push(format!("space-x-{}", x.to_string()));
ret.push(format!("space-x-{}", x.to_value()));
}
if let Some(y) = &self.2 {
ret.push(format!("space-y-{}", y.to_string()));
ret.push(format!("space-y-{}", y.to_value()));
}
ret
@ -108,44 +110,45 @@ pub enum ScreenValue {
}
impl ScreenValue {
pub fn to_string(&self) -> &str {
#[must_use]
pub const fn to_value(&self) -> &str {
match self {
ScreenValue::_0 => "0",
ScreenValue::_0p5 => "0.5",
ScreenValue::_1 => "1",
ScreenValue::_1p5 => "1.5",
ScreenValue::_2 => "2",
ScreenValue::_2p5 => "2.5",
ScreenValue::_3 => "3",
ScreenValue::_3p5 => "3.5",
ScreenValue::_4 => "4",
ScreenValue::_5 => "5",
ScreenValue::_6 => "6",
ScreenValue::_7 => "7",
ScreenValue::_8 => "8",
ScreenValue::_9 => "9",
ScreenValue::_10 => "10",
ScreenValue::_11 => "11",
ScreenValue::_12 => "12",
ScreenValue::_14 => "14",
ScreenValue::_16 => "16",
ScreenValue::_20 => "20",
ScreenValue::_24 => "24",
ScreenValue::_28 => "28",
ScreenValue::_32 => "32",
ScreenValue::_36 => "36",
ScreenValue::_40 => "40",
ScreenValue::_44 => "44",
ScreenValue::_48 => "48",
ScreenValue::_52 => "52",
ScreenValue::_56 => "56",
ScreenValue::_60 => "60",
ScreenValue::_64 => "64",
ScreenValue::_72 => "72",
ScreenValue::_80 => "80",
ScreenValue::_90 => "90",
ScreenValue::px => "px",
ScreenValue::reverse => "reverse",
Self::_0 => "0",
Self::_0p5 => "0.5",
Self::_1 => "1",
Self::_1p5 => "1.5",
Self::_2 => "2",
Self::_2p5 => "2.5",
Self::_3 => "3",
Self::_3p5 => "3.5",
Self::_4 => "4",
Self::_5 => "5",
Self::_6 => "6",
Self::_7 => "7",
Self::_8 => "8",
Self::_9 => "9",
Self::_10 => "10",
Self::_11 => "11",
Self::_12 => "12",
Self::_14 => "14",
Self::_16 => "16",
Self::_20 => "20",
Self::_24 => "24",
Self::_28 => "28",
Self::_32 => "32",
Self::_36 => "36",
Self::_40 => "40",
Self::_44 => "44",
Self::_48 => "48",
Self::_52 => "52",
Self::_56 => "56",
Self::_60 => "60",
Self::_64 => "64",
Self::_72 => "72",
Self::_80 => "80",
Self::_90 => "90",
Self::px => "px",
Self::reverse => "reverse",
}
}
}

View file

@ -3,6 +3,7 @@ use maud::{Markup, Render, html};
#[allow(non_snake_case)]
/// Text UI Widget
#[must_use]
pub fn Text(txt: &str) -> TextWidget {
TextWidget {
inner: None,
@ -29,6 +30,7 @@ pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget {
#[allow(non_snake_case)]
/// `<span>` element
#[must_use]
pub fn Span(txt: &str) -> TextWidget {
TextWidget {
inner: None,
@ -53,63 +55,72 @@ impl TextWidget {
/// Turn `Text` semibold.
///
/// Adds the class `font-semibold`
#[must_use]
pub fn semibold(mut self) -> Self {
self.font = "font-semibold".to_owned();
self.font = "font-semibold".to_string();
self
}
/// Turn `Text` bold.
///
/// Adds the class `font-bold`
#[must_use]
pub fn bold(mut self) -> Self {
self.font = "font-bold".to_owned();
self.font = "font-bold".to_string();
self
}
/// Turn `Text` medium.
///
/// Adds the class `font-medium`
#[must_use]
pub fn medium(mut self) -> Self {
self.font = "font-medium".to_owned();
self.font = "font-medium".to_string();
self
}
/// Turn `Text` size to 2XL.
///
/// Adds the class `text-2xl`
#[must_use]
pub fn _2xl(mut self) -> Self {
self.size = "text-2xl".to_owned();
self.size = "text-2xl".to_string();
self
}
/// Turn `Text` size to xl.
///
/// Adds the class `text-xl`
#[must_use]
pub fn xl(mut self) -> Self {
self.size = "text-xl".to_owned();
self.size = "text-xl".to_string();
self
}
/// Turn `Text` size to small.
///
/// Adds the class `text-sm`
#[must_use]
pub fn sm(mut self) -> Self {
self.size = "text-sm".to_owned();
self.size = "text-sm".to_string();
self
}
pub fn color<T: UIColor>(mut self, color: T) -> Self {
#[must_use]
pub fn color<T: UIColor>(mut self, color: &T) -> Self {
self.color = format!("text-{}", color.color_class());
self
}
#[must_use]
pub fn black(mut self) -> Self {
self.color = "text-black".to_owned();
self.color = "text-black".to_string();
self
}
#[must_use]
pub fn white(mut self) -> Self {
self.color = "text-white".to_owned();
self.color = "text-white".to_string();
self
}
}
@ -148,15 +159,13 @@ impl UIWidget for TextWidget {
p class=(format!("{} {}", class, self.base_class().join(" "))) { (inner) }
}
}
} else if self.span {
html! {
span class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
} else {
if self.span {
html! {
span class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
} else {
html! {
p class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
html! {
p class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
}
}