From 0d13efca49770505cf4c7b5f5476df55502d5008 Mon Sep 17 00:00:00 2001 From: joerdav Date: Thu, 1 Feb 2024 14:07:10 +0000 Subject: [PATCH 1/2] feat: support css args --- .version | 2 +- cmd/templ/generatecmd/testwatch/watch_test.go | 2 +- cmd/templ/migratecmd/main.go | 2 +- generator/generator.go | 10 +-- generator/test-css-usage/expected.html | 13 +++ generator/test-css-usage/template.templ | 14 ++++ generator/test-css-usage/template_templ.go | 75 ++++++++++++++++- parser/v2/cssparser.go | 71 ++++------------ parser/v2/cssparser_test.go | 82 +++++++++++++++---- parser/v2/goexpression/parse.go | 7 +- parser/v2/goparser.go | 21 +++-- parser/v2/templateparser.go | 2 +- parser/v2/types.go | 5 +- 13 files changed, 213 insertions(+), 93 deletions(-) diff --git a/.version b/.version index 7c39898db..1969284cb 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.552 \ No newline at end of file +0.2.557 \ No newline at end of file diff --git a/cmd/templ/generatecmd/testwatch/watch_test.go b/cmd/templ/generatecmd/testwatch/watch_test.go index a202ac617..79f11584f 100644 --- a/cmd/templ/generatecmd/testwatch/watch_test.go +++ b/cmd/templ/generatecmd/testwatch/watch_test.go @@ -334,7 +334,7 @@ func waitForUrl(url string) (err error) { var tries int for { time.Sleep(time.Second) - if tries > 5 { + if tries > 20 { return err } tries++ diff --git a/cmd/templ/migratecmd/main.go b/cmd/templ/migratecmd/main.go index fd3bd686c..bf6e9fbb3 100644 --- a/cmd/templ/migratecmd/main.go +++ b/cmd/templ/migratecmd/main.go @@ -134,7 +134,7 @@ func migrateV1TemplateFileNodeToV2TemplateFileNode(in v1.TemplateFileNode) (out }, nil case v1.CSSTemplate: var t v2.CSSTemplate - t.Name.Value = n.Name.Value + t.Expression.Value = n.Name.Value t.Properties = make([]v2.CSSProperty, len(n.Properties)) for i, p := range n.Properties { t.Properties[i], err = migrateV1CSSPropertyToV2CSSProperty(p) diff --git a/generator/generator.go b/generator/generator.go index fd05d3f31..8573ed20b 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -229,12 +229,12 @@ func (g *generator) writeCSS(n parser.CSSTemplate) error { if _, err = g.w.Write("func "); err != nil { return err } - if r, err = g.w.Write(n.Name.Value); err != nil { + if r, err = g.w.Write(n.Expression.Value); err != nil { return err } - g.sourceMap.Add(n.Name, r) - // () templ.CSSClass { - if _, err = g.w.Write("() templ.CSSClass {\n"); err != nil { + g.sourceMap.Add(n.Expression, r) + // templ.CSSClass { + if _, err = g.w.Write(" templ.CSSClass {\n"); err != nil { return err } { @@ -266,7 +266,7 @@ func (g *generator) writeCSS(n parser.CSSTemplate) error { return fmt.Errorf("unknown CSS property type: %v", reflect.TypeOf(p)) } } - if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_CSSID := templ.CSSID(`%s`, templ_7745c5c3_CSSBuilder.String())\n", n.Name.Value)); err != nil { + if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_CSSID := templ.CSSID(`%s`, templ_7745c5c3_CSSBuilder.String())\n", n.Name)); err != nil { return err } // return templ.CSS { diff --git a/generator/test-css-usage/expected.html b/generator/test-css-usage/expected.html index db8ab3b81..a9f08b00a 100644 --- a/generator/test-css-usage/expected.html +++ b/generator/test-css-usage/expected.html @@ -15,3 +15,16 @@
Class names are HTML escaped.
+ +
+ CSS components can be used with arguments. +
+ +
+ CSS components can be used with arguments. +
+ diff --git a/generator/test-css-usage/template.templ b/generator/test-css-usage/template.templ index 262f27473..3068d6b30 100644 --- a/generator/test-css-usage/template.templ +++ b/generator/test-css-usage/template.templ @@ -1,5 +1,7 @@ package testcssusage +import "fmt" + // Constant class. templ StyleTagsAreSupported() { +
+ +
+``` + ### CSS Middleware The use of CSS templates means that `