From 5c9a0252c46d93974c0b9668837dec44589b6020 Mon Sep 17 00:00:00 2001 From: Stuart George Date: Tue, 28 May 2024 14:38:03 +0100 Subject: [PATCH] added max and min height with vh, vw units --- CHANGELOG.md | 12 ++++++++++++ props/unit.go | 16 +++++++++++++++- style.go | 2 ++ style_test.go | 44 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b3f557..4553634 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/props/unit.go b/props/unit.go index 45f5b0d..08e46dc 100644 --- a/props/unit.go +++ b/props/unit.go @@ -17,6 +17,8 @@ const ( UnitTypePercent UnitTypeRem UnitTypeEm + UnitTypeVh + UnitTypeVw UnitTypeAuto UnitTypeInherit UnitTypeInitial @@ -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" @@ -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 "" } @@ -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} } diff --git a/style.go b/style.go index 2a2fc64..8687596 100644 --- a/style.go +++ b/style.go @@ -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"` diff --git a/style_test.go b/style_test.go index 8d52e6b..3074799 100644 --- a/style_test.go +++ b/style_test.go @@ -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 { @@ -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 {