diff --git a/src/grid/grid_item.rs b/src/grid/grid_item.rs new file mode 100644 index 0000000..a047783 --- /dev/null +++ b/src/grid/grid_item.rs @@ -0,0 +1,40 @@ +use super::use_grid; +use leptos::*; + +#[component] +fn GridItem( + #[prop(default = MaybeSignal::Static(1u16), into)] span: MaybeSignal, + #[prop(optional, into)] offset: MaybeSignal, + children: Children, +) -> impl IntoView { + let grid = use_grid(); + + let style = create_memo(move |_| { + let mut style = String::from("display: grid;"); + let offset = offset.get(); + let span = i32::from(span.get()); + let x_gap = grid.x_gap.get(); + + if offset > 0 { + style.push_str(&format!( + "margin-left: calc((100% - {}px) / {} * {} + {}px)", + (span + offset - 1) * x_gap, + span + offset, + offset, + offset * x_gap + )); + } + style.push_str(&format!( + "grid-column: span {} / span {}", + span + offset, + span + offset + )); + + style + }); + view! { +
+ {children()} +
+ } +} diff --git a/src/grid/mod.rs b/src/grid/mod.rs new file mode 100644 index 0000000..bf7508d --- /dev/null +++ b/src/grid/mod.rs @@ -0,0 +1,46 @@ +mod grid_item; + +pub use grid_item::*; +use leptos::*; + +#[component] +fn Grid( + #[prop(default = MaybeSignal::Static(1u16), into)] cols: MaybeSignal, + #[prop(optional, into)] x_gap: MaybeSignal, + #[prop(optional, into)] y_gap: MaybeSignal, + children: Children, +) -> impl IntoView { + let grid_injection_key = create_rw_signal(GridInjectionKey::new(x_gap)); + provide_context(grid_injection_key); + + let style = create_memo(move |_| { + let mut style = String::from("display: grid;"); + style.push_str(&format!( + "grid-template-columns: repeat({}, minmax(0px, 1fr));", + cols.get() + )); + style.push_str(&format!("grid-gap: {}px ${}px;", y_gap.get(), x_gap.get())); + style + }); + + view! { +
+ {children()} +
+ } +} + +#[derive(Clone)] +pub struct GridInjectionKey { + x_gap: MaybeSignal, +} + +impl GridInjectionKey { + pub fn new(x_gap: MaybeSignal) -> Self { + Self { x_gap } + } +} + +pub fn use_grid() -> GridInjectionKey { + expect_context::() +} diff --git a/src/lib.rs b/src/lib.rs index db5e652..7610a6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod checkbox; mod code; mod color_picker; mod components; +mod grid; mod icon; mod image; mod input; @@ -29,6 +30,7 @@ pub use card::*; pub use checkbox::*; pub use code::*; pub use color_picker::*; +pub use grid::*; pub use icon::*; pub use image::*; pub use input::*; diff --git a/src/menu/mod.rs b/src/menu/mod.rs index 13c5e2b..bc10ced 100644 --- a/src/menu/mod.rs +++ b/src/menu/mod.rs @@ -45,5 +45,5 @@ impl MenuInjectionKey { } pub fn use_menu() -> RwSignal { - use_context::>().expect("MenuInjectionKey not exist") + expect_context::>() }