Skip to content

Commit

Permalink
feat: Support individual vertical gaps but shared horizontal gaps (#476)
Browse files Browse the repository at this point in the history
* feat: Add support for individual vertical gaps but same horizontal

* docs

* typo

* improvement
  • Loading branch information
marc2332 authored Jan 28, 2024
1 parent 28f5008 commit 9545d10
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
4 changes: 2 additions & 2 deletions book/src/guides/layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn app(cx: Scope) -> Element {

### padding

Specify the inner paddings of an element. You can do so by three different ways, just like in CSS.
Specify the inner paddings of an element. You can do so by three different ways.

```rust, no_run
fn app(cx: Scope) -> Element {
Expand Down Expand Up @@ -209,7 +209,7 @@ fn app(cx: Scope) -> Element {

### margin

Specify the margin of an element. You can do so by three different ways, just like in CSS.
Specify the margin of an element. You can do so by three different ways.

```rust, no_run
fn app(cx: Scope) -> Element {
Expand Down
3 changes: 2 additions & 1 deletion crates/elements/src/_docs/attributes/margin.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Specify the margin of an element.
You can do so by three different ways, just like in CSS.
You can do so by four different ways, just like in CSS.

### Example

Expand All @@ -10,6 +10,7 @@ fn app(cx: Scope) -> Element {
rect {
margin: "25", // 25 in all sides
margin: "100 50", // 100 in top and bottom, and 50 in left and right
margin: "2 15 25", // 2 in top, 15 in left and right, and 25 in bottom
margin: "5 7 3 9" // 5 in top, 7 in right, 3 in bottom and 9 in left
}
)
Expand Down
3 changes: 2 additions & 1 deletion crates/elements/src/_docs/attributes/padding.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Specify the inner paddings of an element. You can do so by three different ways, just like in CSS.
Specify the inner paddings of an element. You can do so by four different ways, just like in CSS.

### Example

Expand All @@ -9,6 +9,7 @@ fn app(cx: Scope) -> Element {
rect {
padding: "25", // 25 in all sides
padding: "100 50", // 100 in top and bottom, and 50 in left and right
padding: "2 15 25", // 2 in top, 15 in left and right, and 25 in bottom
padding: "5 7 3 9" // 5 in top, 7 in right, 3 in bottom and 9 in left
}
)
Expand Down
19 changes: 19 additions & 0 deletions crates/state/src/values/gaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ impl Parse for Gaps {
.map_err(|_| ParseGapError)?,
)
}
// Individual vertical but same horizontal
3 => {
let top = values
.next()
.ok_or(ParseGapError)?
.parse::<f32>()
.map_err(|_| ParseGapError)?;
let left_and_right = values
.next()
.ok_or(ParseGapError)?
.parse::<f32>()
.map_err(|_| ParseGapError)?;
let bottom = values
.next()
.ok_or(ParseGapError)?
.parse::<f32>()
.map_err(|_| ParseGapError)?;
paddings = Gaps::new(top, left_and_right, bottom, left_and_right);
}
// Each directions
4 => {
paddings = Gaps::new(
Expand Down
6 changes: 6 additions & 0 deletions crates/state/tests/parse_gaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ fn parse_sides_gaps() {
let gaps = Gaps::parse("1 2 3 4");
assert_eq!(gaps, Ok(Gaps::new(1.0, 2.0, 3.0, 4.0)));
}

#[test]
fn parse_horizontal_axis_and_vertical_sides() {
let gaps = Gaps::parse("5 50 30");
assert_eq!(gaps, Ok(Gaps::new(5.0, 50.0, 30.0, 50.0)));
}
20 changes: 16 additions & 4 deletions examples/paddings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn app(cx: Scope) -> Element {
render!(
rect {
overflow: "clip",
height: "33%",
height: "25%",
width: "100%",
padding: "15",
background: "black",
Expand All @@ -25,7 +25,7 @@ fn app(cx: Scope) -> Element {
}
rect {
overflow: "clip",
height: "33%",
height: "25%",
width: "100%",
padding: "10 30 50 70",
background: "gray",
Expand All @@ -37,10 +37,22 @@ fn app(cx: Scope) -> Element {
}
rect {
overflow: "clip",
height: "33%",
height: "25%",
width: "100%",
padding: "25 125",
background: "white",
background: "black",
rect {
height: "100%",
width: "100%",
background: "yellow"
}
}
rect {
overflow: "clip",
height: "25%",
width: "100%",
padding: "30 50 10",
background: "gray",
rect {
height: "100%",
width: "100%",
Expand Down

0 comments on commit 9545d10

Please sign in to comment.