Skip to content

Commit

Permalink
Pass attribute expressions for transition:animate (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jul 24, 2023
1 parent 3f3242c commit ce5cf31
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-onions-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Pass transition:animate expressions
20 changes: 3 additions & 17 deletions internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,26 +435,12 @@ func render1(p *printer, n *Node, opts RenderOptions) {
p.print(`]`)
} else {
if transform.HasAttr(n, transform.TRANSITION_ANIMATE) || transform.HasAttr(n, transform.TRANSITION_NAME) {
animationName := ""
if transform.HasAttr(n, transform.TRANSITION_ANIMATE) {
animationName = transform.GetAttr(n, transform.TRANSITION_ANIMATE).Val
}
transitionExpr := ""
if transform.HasAttr(n, transform.TRANSITION_NAME) {
attr := transform.GetAttr(n, transform.TRANSITION_NAME)
switch attr.Type {
case astro.QuotedAttribute:
transitionExpr = fmt.Sprintf(`"%s"`, attr.Val)
case astro.ExpressionAttribute:
transitionExpr = fmt.Sprintf(`(%s)`, attr.Val)
case astro.TemplateLiteralAttribute:
transitionExpr = fmt.Sprintf("`%s`", attr.Val)
}
}
animationExpr := convertAttributeValue(n, transform.TRANSITION_ANIMATE)
transitionExpr := convertAttributeValue(n, transform.TRANSITION_NAME)

n.Attr = append(n.Attr, astro.Attribute{
Key: "data-astro-transition-scope",
Val: fmt.Sprintf(`%s(%s, "%s", "%s", %s)`, RENDER_TRANSITION, RESULT, n.TransitionScope, animationName, transitionExpr),
Val: fmt.Sprintf(`%s(%s, "%s", %s, %s)`, RENDER_TRANSITION, RESULT, n.TransitionScope, animationExpr, transitionExpr),
Type: astro.ExpressionAttribute,
})
}
Expand Down
8 changes: 8 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,14 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<div${$$addAttribute($$renderTransition($$result, "", "", ` + BACKTICK + `${one}-two` + BACKTICK + `), "data-astro-transition-scope")}></div>`,
},
},
{
name: "transition:animate with an expression",
source: "<div transition:animate={slide({duration:15})}></div>",
filename: "/projects/app/src/pages/page.astro",
want: want{
code: `${$$maybeRenderHead($$result)}<div${$$addAttribute($$renderTransition($$result, "", (slide({duration:15})), ""), "data-astro-transition-scope")}></div>`,
},
},
}

for _, tt := range tests {
Expand Down
18 changes: 18 additions & 0 deletions internal/printer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"strings"

"github.com/iancoleman/strcase"
astro "github.com/withastro/compiler/internal"
"github.com/withastro/compiler/internal/js_scanner"
"github.com/withastro/compiler/internal/transform"
)

func escapeText(src string) string {
Expand Down Expand Up @@ -118,3 +120,19 @@ func removeComments(input string) (string, error) {

return strings.TrimSpace(sb.String()), nil
}

func convertAttributeValue(n *astro.Node, attrName string) string {
expr := `""`
if transform.HasAttr(n, attrName) {
attr := transform.GetAttr(n, attrName)
switch attr.Type {
case astro.QuotedAttribute:
expr = fmt.Sprintf(`"%s"`, attr.Val)
case astro.ExpressionAttribute:
expr = fmt.Sprintf(`(%s)`, attr.Val)
case astro.TemplateLiteralAttribute:
expr = fmt.Sprintf("`%s`", attr.Val)
}
}
return expr
}

0 comments on commit ce5cf31

Please sign in to comment.