Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass transition directives onto components #838

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-keys-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Pass transition directives onto components
13 changes: 2 additions & 11 deletions internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

. "github.com/withastro/compiler/internal"
astro "github.com/withastro/compiler/internal"
"github.com/withastro/compiler/internal/handler"
"github.com/withastro/compiler/internal/js_scanner"
"github.com/withastro/compiler/internal/loc"
Expand Down Expand Up @@ -404,6 +403,7 @@ func render1(p *printer, n *Node, opts RenderOptions) {
if isImplicit {
// do nothing
} else if isComponent {
maybeConvertTransition(n)
p.print(",")
p.printAttributesToObject(n)
} else if isSlot {
Expand Down Expand Up @@ -434,16 +434,7 @@ func render1(p *printer, n *Node, opts RenderOptions) {
}
p.print(`]`)
} else {
if transform.HasAttr(n, transform.TRANSITION_ANIMATE) || transform.HasAttr(n, transform.TRANSITION_NAME) {
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, animationExpr, transitionExpr),
Type: astro.ExpressionAttribute,
})
}
maybeConvertTransition(n)

for _, a := range n.Attr {
if transform.IsImplicitNodeMarker(a) || a.Key == "is:inline" {
Expand Down
16 changes: 15 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ func (p *printer) printAttributesToObject(n *astro.Node) {
if i != 0 && !lastAttributeSkipped {
p.print(",")
}
if a.Key == "set:text" || a.Key == "set:html" || a.Key == "is:raw" {
if a.Key == "set:text" || a.Key == "set:html" || a.Key == "is:raw" || a.Key == "transition:animate" || a.Key == "transition:name" {
lastAttributeSkipped = true
continue
}
if a.Namespace != "" {
Expand Down Expand Up @@ -446,6 +447,19 @@ func remove(slice []*astro.Node, node *astro.Node) []*astro.Node {
return append(slice[:s], slice[s+1:]...)
}

func maybeConvertTransition(n *astro.Node) {
if transform.HasAttr(n, transform.TRANSITION_ANIMATE) || transform.HasAttr(n, transform.TRANSITION_NAME) {
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, animationExpr, transitionExpr),
Type: astro.ExpressionAttribute,
})
}
}

func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.TransformOptions, source []byte) {
var specs []string
var asrts []string
Expand Down
9 changes: 9 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,15 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<div${$$addAttribute($$renderTransition($$result, "", (slide({duration:15})), ""), "data-astro-transition-scope")}></div>`,
},
},
{
name: "transition:animate on Component",
only: true,
source: `<Component class="bar" transition:animate="morph"></Component>`,
filename: "/projects/app/src/pages/page.astro",
want: want{
code: `${$$renderComponent($$result,'Component',Component,{"class":"bar","data-astro-transition-scope":($$renderTransition($$result, "", "morph", ""))})}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tough to understand the expected result. Are the parentheses needed in ($$renderTransition($$result, "", "morph", ""))})?

- ($$renderTransition($$result, "", "morph", ""))
+ $$renderTransition($$result, "", "morph", "")

It should yield the same result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our attribution expression code always wraps expressions with parens. Maybe we can change it? I don't know. I assume it's to protect against something... but what alludes me. Perhaps in case someone puts a statement inside of an expression, the parens make it into an expression? idk.

},
},
}

for _, tt := range tests {
Expand Down
Loading