Skip to content

Commit f6b09de

Browse files
committed
add custom property support
1 parent 846b059 commit f6b09de

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44

55
### v0.0.4
66

7+
Added support for custom props.
8+
79
Added new props:
810

911
- cursor

style.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type (
11+
// Props represents the standard CSS properties that can be applied to a CSS rule.
1112
Props struct {
1213
AlignItems props.AlignItems `css:"align-items"`
1314
Appearance props.Appearance `css:"appearance"`
@@ -74,9 +75,20 @@ type (
7475
WhiteSpace props.WhiteSpace `css:"white-space"`
7576
Width props.Unit `css:"width"`
7677
}
78+
// Style represents a CSS style rule.
7779
Style struct {
80+
// Selector is the CSS selector to which the properties will be applied.
81+
// It can be any valid CSS selector like class, id, element type, etc.
7882
Selector string
79-
Props Props
83+
84+
// Props contains the standard CSS properties that will be applied to the selector.
85+
// These properties are represented by the Props struct and are validated.
86+
Props Props
87+
88+
// CustomProps contains any additional CSS properties that are not covered by the Props struct.
89+
// These properties are not validated and are directly added to the CSS rule.
90+
// The keys of the map are the CSS property names and the values are the CSS property values.
91+
CustomProps map[string]string
8092
}
8193
)
8294

@@ -89,6 +101,7 @@ func (s *Style) CSS(w io.Writer) error {
89101
return err
90102
}
91103

104+
// Iterate over the fields of the Props struct and write the CSS properties to the writer.
92105
for i := 0; i < propsValue.NumField(); i++ {
93106
fieldValue := propsValue.Field(i)
94107
fieldType := propsType.Field(i)
@@ -106,6 +119,13 @@ func (s *Style) CSS(w io.Writer) error {
106119
}
107120
}
108121

122+
// Write the custom properties to the writer.
123+
for prop, value := range s.CustomProps {
124+
if _, err := fmt.Fprintf(w, "%s:%s;", prop, value); err != nil {
125+
return err
126+
}
127+
}
128+
109129
if _, err := fmt.Fprint(w, "}"); err != nil {
110130
return err
111131
}

style_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ func TestStyle_MultipleProps(t *testing.T) {
3838
}
3939
}
4040

41+
func TestStyle_CustomProps(t *testing.T) {
42+
st := &Style{
43+
Selector: ".test",
44+
CustomProps: map[string]string{
45+
"--color": "red",
46+
"background-color": "var(--color)",
47+
},
48+
}
49+
var buf bytes.Buffer
50+
err := st.CSS(&buf)
51+
if err != nil {
52+
t.Errorf("unexpected error: %v", err)
53+
}
54+
css := ".test{--color:red;background-color:var(--color);}"
55+
if buf.String() != css {
56+
t.Errorf("expected %q, got %q", css, buf.String())
57+
}
58+
}
59+
4160
func TestStyle_AlignItems(t *testing.T) {
4261
testCases := map[props.AlignItems]string{
4362
props.AlignItemsStart: "flex-start",

0 commit comments

Comments
 (0)