Skip to content

Commit

Permalink
make custom prop type
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartaccent committed May 21, 2024
1 parent 53692ce commit dcf5839
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

### v0.0.5

Added explicit CustomProp type and make CustomProps a slice to preserve ordering.

### v0.0.4

Added support for custom props.
Expand Down
11 changes: 7 additions & 4 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type (
WhiteSpace props.WhiteSpace `css:"white-space"`
Width props.Unit `css:"width"`
}
// CustomProp represents an additional CSS property that is not covered by the Props struct.
CustomProp struct {
Attr, Value string
}
// Style represents a CSS style rule.
Style struct {
// Selector is the CSS selector to which the properties will be applied.
Expand All @@ -87,8 +91,7 @@ type (

// CustomProps contains any additional CSS properties that are not covered by the Props struct.
// These properties are directly added to the CSS rule as is.
// The keys of the map are the CSS property names and the values are the CSS property values.
CustomProps map[string]string
CustomProps []CustomProp
}
)

Expand Down Expand Up @@ -120,8 +123,8 @@ func (s *Style) CSS(w io.Writer) error {
}

// Write the custom properties to the writer.
for prop, value := range s.CustomProps {
if _, err := fmt.Fprintf(w, "%s:%s;", prop, value); err != nil {
for _, prop := range s.CustomProps {
if _, err := fmt.Fprintf(w, "%s:%s;", prop.Attr, prop.Value); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func TestStyle_MultipleProps(t *testing.T) {
func TestStyle_CustomProps(t *testing.T) {
st := &Style{
Selector: ".test",
CustomProps: map[string]string{
"--color": "red",
"background-color": "var(--color)",
CustomProps: []CustomProp{
{Attr: "--color", Value: "red"},
{Attr: "background-color", Value: "var(--color)"},
},
}
var buf bytes.Buffer
Expand Down

0 comments on commit dcf5839

Please sign in to comment.