From 93c512d0204877472382d9f88b03a80996b2c427 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Sat, 11 Nov 2023 21:10:20 -0800 Subject: [PATCH] Add utilities for percent, px, vw, vh, em, rem --- styles/utils.go | 34 ++++++++++++++++++++++++++++++++++ styles/utils_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/styles/utils.go b/styles/utils.go index 21cc81d..6890539 100644 --- a/styles/utils.go +++ b/styles/utils.go @@ -1,5 +1,9 @@ package styles +import ( + "fmt" +) + // Merge combines multiple styles.Props maps into one, with later styles overriding earlier ones. func Merge(styleMaps ...Props) Props { mergedStyles := Props{} @@ -10,3 +14,33 @@ func Merge(styleMaps ...Props) Props { } return mergedStyles } + +// Percent returns a string representation of the given integer as a percentage. +func Percent(value int) string { + return fmt.Sprintf("%d%%", value) +} + +// Pixels returns a string representation of the given integer as a pixel value. +func Pixels(value int) string { + return fmt.Sprintf("%dpx", value) +} + +// ViewportHeight returns a string representation of the given integer as a viewport height value. +func ViewportHeight(value int) string { + return fmt.Sprintf("%dvh", value) +} + +// ViewportWidth returns a string representation of the given integer as a viewport width value. +func ViewportWidth(value int) string { + return fmt.Sprintf("%dvw", value) +} + +// Em returns a string representation of the given float as an em value. +func Em(value float64) string { + return fmt.Sprintf("%.2fem", value) +} + +// Rem returns a string representation of the given float as a rem value. +func Rem(value float64) string { + return fmt.Sprintf("%.2frem", value) +} diff --git a/styles/utils_test.go b/styles/utils_test.go index fda399f..f19ec35 100644 --- a/styles/utils_test.go +++ b/styles/utils_test.go @@ -54,3 +54,45 @@ func TestMergeThreeStyles(t *testing.T) { assert.Equal(t, expectedStyle, mergedStyle) } + +func TestPercent(t *testing.T) { + assert.Equal(t, "100%", Percent(100)) + assert.Equal(t, "0%", Percent(0)) + assert.Equal(t, "50%", Percent(50)) + assert.Equal(t, "25%", Percent(25)) +} + +func TestPixels(t *testing.T) { + assert.Equal(t, "100px", Pixels(100)) + assert.Equal(t, "0px", Pixels(0)) + assert.Equal(t, "50px", Pixels(50)) + assert.Equal(t, "25px", Pixels(25)) +} + +func TestViewportHeight(t *testing.T) { + assert.Equal(t, "100vh", ViewportHeight(100)) + assert.Equal(t, "0vh", ViewportHeight(0)) + assert.Equal(t, "50vh", ViewportHeight(50)) + assert.Equal(t, "25vh", ViewportHeight(25)) +} + +func TestViewportWidth(t *testing.T) { + assert.Equal(t, "100vw", ViewportWidth(100)) + assert.Equal(t, "0vw", ViewportWidth(0)) + assert.Equal(t, "50vw", ViewportWidth(50)) + assert.Equal(t, "25vw", ViewportWidth(25)) +} + +func TestEm(t *testing.T) { + assert.Equal(t, "100.00em", Em(100)) + assert.Equal(t, "0.00em", Em(0)) + assert.Equal(t, "50.00em", Em(50)) + assert.Equal(t, "25.00em", Em(25)) +} + +func TestRem(t *testing.T) { + assert.Equal(t, "100.00rem", Rem(100)) + assert.Equal(t, "0.00rem", Rem(0)) + assert.Equal(t, "50.00rem", Rem(50)) + assert.Equal(t, "25.00rem", Rem(25)) +}