-
Notifications
You must be signed in to change notification settings - Fork 59
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
Support for transition:persist directive #840
Changes from all commits
31e6c26
8c55149
abde938
293db48
233736c
9222b8c
3ffae54
e625f45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/compiler': minor | ||
--- | ||
|
||
Support the transition:persist directive |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ var RENDER_SLOT = "$$renderSlot" | |
var MERGE_SLOTS = "$$mergeSlots" | ||
var ADD_ATTRIBUTE = "$$addAttribute" | ||
var RENDER_TRANSITION = "$$renderTransition" | ||
var CREATE_TRANSITION_SCOPE = "$$createTransitionScope" | ||
var SPREAD_ATTRIBUTES = "$$spreadAttributes" | ||
var DEFINE_STYLE_VARS = "$$defineStyleVars" | ||
var DEFINE_SCRIPT_VARS = "$$defineScriptVars" | ||
|
@@ -122,6 +123,10 @@ func (p *printer) printInternalImports(importSpecifier string, opts *RenderOptio | |
p.addNilSourceMapping() | ||
p.print("renderTransition as " + RENDER_TRANSITION + ",\n ") | ||
} | ||
if opts.opts.ExperimentalPersistence { | ||
p.addNilSourceMapping() | ||
p.print("createTransitionScope as " + CREATE_TRANSITION_SCOPE + ",\n ") | ||
} | ||
|
||
// Only needed if using fallback `resolvePath` as it calls `$$metadata.resolvePath` | ||
if opts.opts.ResolvePath == nil { | ||
|
@@ -276,14 +281,33 @@ func (p *printer) printFuncSuffix(opts transform.TransformOptions, n *astro.Node | |
p.println(fmt.Sprintf("export default %s;", componentName)) | ||
} | ||
|
||
var skippedAttributes = map[string]bool{ | ||
"define:vars": true, | ||
"set:text": true, | ||
"set:html": true, | ||
"is:raw": true, | ||
"transition:animate": true, | ||
"transition:name": true, | ||
"transition:persist": true, | ||
} | ||
|
||
var skippedAttributesToObject = map[string]bool{ | ||
"set:text": true, | ||
"set:html": true, | ||
"is:raw": true, | ||
"transition:animate": true, | ||
"transition:name": true, | ||
"transition:persist": true, | ||
} | ||
|
||
func (p *printer) printAttributesToObject(n *astro.Node) { | ||
lastAttributeSkipped := false | ||
p.print("{") | ||
for i, a := range n.Attr { | ||
if i != 0 && !lastAttributeSkipped { | ||
p.print(",") | ||
} | ||
if a.Key == "set:text" || a.Key == "set:html" || a.Key == "is:raw" || a.Key == "transition:animate" || a.Key == "transition:name" { | ||
if _, ok := skippedAttributesToObject[a.Key]; ok { | ||
lastAttributeSkipped = true | ||
continue | ||
} | ||
|
@@ -338,7 +362,7 @@ func (p *printer) printAttributesToObject(n *astro.Node) { | |
} | ||
|
||
func (p *printer) printAttribute(attr astro.Attribute, n *astro.Node) { | ||
if attr.Key == "define:vars" || attr.Key == "set:text" || attr.Key == "set:html" || attr.Key == "is:raw" || attr.Key == "transition:animate" || attr.Key == "transition:name" { | ||
if _, ok := skippedAttributes[attr.Key]; ok { | ||
return | ||
} | ||
|
||
|
@@ -458,6 +482,26 @@ func maybeConvertTransition(n *astro.Node) { | |
Type: astro.ExpressionAttribute, | ||
}) | ||
} | ||
if transform.HasAttr(n, transform.TRANSITION_PERSIST) { | ||
transitionPersistIndex := transform.AttrIndex(n, transform.TRANSITION_PERSIST) | ||
// If there no value, create a transition scope for this element | ||
if n.Attr[transitionPersistIndex].Val != "" { | ||
// Just rename the attribute | ||
n.Attr[transitionPersistIndex].Key = "data-astro-transition-persist" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
} else if transform.HasAttr(n, transform.TRANSITION_NAME) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is a transition:name on the element, use that as the persist value as well.
|
||
transitionNameAttr := transform.GetAttr(n, transform.TRANSITION_NAME) | ||
n.Attr[transitionPersistIndex].Key = "data-astro-transition-persist" | ||
n.Attr[transitionPersistIndex].Val = transitionNameAttr.Val | ||
n.Attr[transitionPersistIndex].Type = transitionNameAttr.Type | ||
} else { | ||
n.Attr = append(n.Attr, astro.Attribute{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No value provided, so we will assign a value at runtime based on the position of the element within the component and the number of times this component has been used. This is the same algorithm used to auto-name
|
||
Key: "data-astro-transition-persist", | ||
Val: fmt.Sprintf(`%s(%s, "%s")`, CREATE_TRANSITION_SCOPE, RESULT, n.TransitionScope), | ||
Type: astro.ExpressionAttribute, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.TransformOptions, source []byte) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are exactly the same except that the object form doesn't skip
define:vars
.