Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utilities for percent, px, vw, vh, em, rem #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions styles/utils.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand All @@ -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)
}
42 changes: 42 additions & 0 deletions styles/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}