Skip to content

Commit

Permalink
added max and min height with vh, vw units
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartaccent committed May 28, 2024
1 parent d2a56ab commit 5c9a025
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to this project will be documented in this file.

### v0.0.8

Added new props:

- max-height
- max-width

Added new units:

- vh
- vw

### v0.0.7

Added new props:
Expand Down
16 changes: 15 additions & 1 deletion props/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
UnitTypePercent
UnitTypeRem
UnitTypeEm
UnitTypeVh
UnitTypeVw
UnitTypeAuto
UnitTypeInherit
UnitTypeInitial
Expand All @@ -26,7 +28,7 @@ func (u Unit) String() string {
switch u.Type {
case UnitTypeRaw:
return fmt.Sprintf("%v", u.Size)
case UnitTypePx, UnitTypePercent, UnitTypeRem, UnitTypeEm:
case UnitTypePx, UnitTypePercent, UnitTypeRem, UnitTypeEm, UnitTypeVh, UnitTypeVw:
return formatSize(u)
case UnitTypeAuto:
return "auto"
Expand Down Expand Up @@ -60,6 +62,10 @@ func getFormat(unitType UnitType) string {
return "%.3frem"
case UnitTypeEm:
return "%.3fem"
case UnitTypeVh:
return "%dvh"
case UnitTypeVw:
return "%dvw"
default:
return ""
}
Expand All @@ -85,6 +91,14 @@ func UnitEm(size float64) Unit {
return Unit{Size: size, Type: UnitTypeEm}
}

func UnitVh(size int) Unit {
return Unit{Size: size, Type: UnitTypeVh}
}

func UnitVw(size int) Unit {
return Unit{Size: size, Type: UnitTypeVw}
}

func UnitAuto() Unit {
return Unit{Type: UnitTypeAuto}
}
Expand Down
2 changes: 2 additions & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ type (
MarginLeft props.Unit `css:"margin-left"`
MarginRight props.Unit `css:"margin-right"`
MarginTop props.Unit `css:"margin-top"`
MaxHeight props.Unit `css:"max-height"`
MaxWidth props.Unit `css:"max-width"`
MinHeight props.Unit `css:"min-height"`
MinWidth props.Unit `css:"min-width"`
Opacity props.Unit `css:"opacity"`
Overflow props.Overflow `css:"overflow"`
Expand Down
44 changes: 38 additions & 6 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,27 @@ func TestStyle_MarginTop(t *testing.T) {
}
}

func TestStyle_MaxHeight(t *testing.T) {
testCases := map[props.Unit]string{
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitVh(100): "100vh",
props.UnitAuto(): "auto",
}

for prop, expected := range testCases {
st := &Style{Selector: ".test", Props: Props{MaxHeight: prop}}
css := fmt.Sprintf(".test{max-height:%s;}", expected)
runTest(t, st, css)
}
}

func TestStyle_MaxWidth(t *testing.T) {
testCases := map[props.Unit]string{
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitAuto(): "auto",
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitVw(100): "100vw",
props.UnitAuto(): "auto",
}

for prop, expected := range testCases {
Expand All @@ -957,11 +973,27 @@ func TestStyle_MaxWidth(t *testing.T) {
}
}

func TestStyle_MinHeight(t *testing.T) {
testCases := map[props.Unit]string{
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitVh(100): "100vh",
props.UnitAuto(): "auto",
}

for prop, expected := range testCases {
st := &Style{Selector: ".test", Props: Props{MinHeight: prop}}
css := fmt.Sprintf(".test{min-height:%s;}", expected)
runTest(t, st, css)
}
}

func TestStyle_MinWidth(t *testing.T) {
testCases := map[props.Unit]string{
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitAuto(): "auto",
props.UnitPx(10): "10px",
props.UnitRem(2): "2.000rem",
props.UnitVw(100): "100vw",
props.UnitAuto(): "auto",
}

for prop, expected := range testCases {
Expand Down

0 comments on commit 5c9a025

Please sign in to comment.