Skip to content

Commit

Permalink
fix: add missing support for templ.SafeCSS in optional CSS class expr…
Browse files Browse the repository at this point in the history
…essions, fixes #162
  • Loading branch information
a-h committed Sep 20, 2023
1 parent a277685 commit 95be989
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
2 changes: 2 additions & 0 deletions generator/test-css-usage/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
<div class="a c"></div>
<div class="a"></div>
<input type="email" id="email" name="email" class="a b" placeholder="[email protected]" autocomplete="off"/>
<button class="bg-violet-500 hover:bg-violet-600">Save changes</button>

5 changes: 5 additions & 0 deletions generator/test-css-usage/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ templ KVExample() {
<input type="email" id="email" name="email" class={ "a", "b", "c", templ.KV("c", false) } placeholder="[email protected]" autocomplete="off"/>
}

templ PsuedoAttributes() {
<button class={ "bg-violet-500", templ.KV(templ.SafeClass("hover:bg-violet-600"), true) }>Save changes</button>
}

templ ThreeButtons() {
{! Button("A") }
{! Button("B") }
<button class={ templ.Classes(green) } type="button">{ "Green" }</button>
{! MapCSSExample() }
{! KVExample() }
{! PsuedoAttributes() }
}

62 changes: 56 additions & 6 deletions generator/test-css-usage/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ func (cp *cssProcessor) Add(item any) {
}
case KeyValue[string, bool]:
cp.AddUnsanitized(c.Key, c.Value)
case []KeyValue[CSSClass, bool]:
for _, kv := range c {
cp.AddSanitized(kv.Key.ClassName(), kv.Value)
}
case KeyValue[CSSClass, bool]:
cp.AddSanitized(c.Key.ClassName(), c.Value)
case CSSClasses:
for _, item := range c {
cp.Add(item)
Expand Down
4 changes: 3 additions & 1 deletion runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ func TestClassesFunction(t *testing.T) {
templ.KV("b", true),
"c",
templ.KV("c", false),
templ.KV(templ.SafeClass("d"), true),
templ.KV(templ.SafeClass("e"), false),
},
expected: "a b",
expected: "a b d",
},
{
name: "an array of KV types can be used to show or hide classes",
Expand Down

0 comments on commit 95be989

Please sign in to comment.