Skip to content

Commit

Permalink
create separate Props.CSS func
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartaccent committed Jun 6, 2024
1 parent 9126f49 commit 1657129
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

### v0.1.2

- Move the `Props` CSS writing into a `Props.CSS` func, so it can be used in isolation of the `Style` struct.
- This allows for the `Props` CSS to be used in other contexts, such as in a `style` attribute in HTML.

### v0.1.1

Added new props:
Expand Down
32 changes: 21 additions & 11 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,15 @@ type (
}
)

// CSS writes the CSS representation of the style to the writer.
func (s *Style) CSS(w io.Writer) error {
propsValue := reflect.ValueOf(s.Props)
propsType := reflect.TypeOf(s.Props)

if _, err := fmt.Fprintf(w, "%s{", s.Selector); err != nil {
return err
}
// CSS writes the CSS representation of the props to the writer.
func (p *Props) CSS(w io.Writer) error {
value := reflect.ValueOf(*p)
typ := reflect.TypeOf(*p)

// Iterate over the fields of the Props struct and write the CSS properties to the writer.
for i := 0; i < propsValue.NumField(); i++ {
fieldValue := propsValue.Field(i)
fieldType := propsType.Field(i)
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
fieldType := typ.Field(i)

if fieldValue.IsZero() {
continue
Expand All @@ -145,6 +141,20 @@ func (s *Style) CSS(w io.Writer) error {
}
}

return nil
}

// CSS writes the CSS representation of the style to the writer.
func (s *Style) CSS(w io.Writer) error {
if _, err := fmt.Fprintf(w, "%s{", s.Selector); err != nil {
return err
}

// Write the standard properties to the writer.
if err := s.Props.CSS(w); err != nil {
return err
}

// Write the custom properties to the writer.
for _, prop := range s.CustomProps {
if _, err := fmt.Fprintf(w, "%s:%s;", prop.Attr, prop.Value); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ func runTest(t *testing.T, st *Style, expected string) {
}
}

func TestProps_CSS(t *testing.T) {
testCases := map[Props]string{
{}: "",
{BackgroundColor: props.ColorRGBA(0, 0, 0, 255)}: "background-color:rgba(0,0,0,1.00);",
{Margin: props.UnitPx(10), Padding: props.UnitPx(10)}: "margin:10px;padding:10px;",
}
for prop, expected := range testCases {
var buf bytes.Buffer
err := prop.CSS(&buf)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

if buf.String() != expected {
t.Errorf("expected %q, got %q", expected, buf.String())
}
}
}

func TestStyle_Empty(t *testing.T) {
st := &Style{Selector: ".test", Props: Props{}}
css := ".test{}"
Expand Down

0 comments on commit 1657129

Please sign in to comment.