Conversation
🦋 Changeset detectedLatest commit: 10234e2 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
I can work on the docs for this one. |
I would be very happy if you would do that! It's always so grueling to write good documentation in a foreign language! |
|
Hey @martrapp and @matthewp ! I'm starting to dig into this to be ready for docs, and just a heads up that in addition to this now-complete picture of things as a whole, some things that will help docs fall into place:
The docs site itself will really just document how things work as we don't describe changes, or past version there. But for updating existing docs (esp. if recommendations are different now), the changeset, blog post, and for helping our support squad understand what's different so they can answer people's questions, paying some attention now to the differences is helpful. |
|
@sarah11918 Yeah there's one case where you can do darkmode with one of the new events that might be cleaner than the existing |
|
Hi @sarah11918 , nice of you to come on board! Even if it is partially outdated, the information in withastro/roadmap#740 can help to understand how things came to be. I've changed the color coding in the diagram at the top of the page to better represent what's old and what's new. This is exactly as you have already guessed. As announced above, a few paragraphs from the user perspective that might also help define good documents: There are now 5 events? How do I choose the right one?The events are assigned to five positions in the transition process: The beginning and the end of the preparation phase, the beginning and the end of the swap phase and during the closing phase. Three of them ( While some things can be done in any event, some tasks can only be performed in a specific event, see details below. I want to have different behaviors for different links on the page.You can use the I would like to display some "loading..." indicatorUse the event listener of the I want to add my own custom animationWait for the I want to prefetch images on the target pageView transitions from thumbnails to large images in target files look very ugly if the target image is not available when the transition starts. For better results, preload the images before the view transition begins: Define a new function I want to optimize the number of objects participating in a view transitionIn the listener for the I want to use a different loader that can output percentage events during loading.Yes, you can do that by overriding the The default swapping of View Transitions resets iframes and animations.Define your own swap algorithm: Override the Do all event listeners and callbacks have to be synchronous?All event listeners should only execute synchronous code. The reason for this is that You can use asynchronous code in the event listeners and callbacks, but the processing would not wait for this code to complete and it would actually be executed in parallel with the view transition. So this is clearly not recommended. I need to run asynchronous code during the transition and waitThe only way to execute asynchronous code during the transition and |
|
I just added a draft PR of the updated docs and @martrapp's explanations above blows away mine :D withastro/docs#5425 |
|
Two new tech demos on https://events-3bg.pages.dev/: |
|
@matthewp we haven't talked explicitly on the current state of the |
|
Do you mean like this? navigate("/url", { _formData: formData })If so, no I don't think we should do that. The alignment with |
OK, fine with me! |
|
I only skimmed through the code, and I think Matthew has the best knowledge to review this, but I'm slightly concerned of the bundle size increase this feature brings. The code to create events are taking a lot for something that might not be used. Testing locally with It's around 20% size increase. I don't think this is a blocker for the PR, but something I thought to bring up. |
sarah11918
left a comment
There was a problem hiding this comment.
Amazing work @martrapp ! The care and detail into this feature is truly outstanding!
Just a question to verify re: connecting this to what's in Docs proper, and suggested updating to a more direct link for the lifecycle events!
| ``` | ||
|
|
||
| The `astro:before-*` events allow you to change properties and strategies of the view transition implementation. | ||
| The `astro:after-*` events allow you to make changes to the current DOM before and after the transition. |
There was a problem hiding this comment.
I'll note that saying it's for "before and after" is maybe confusing here, since we're trying to distinguish before- events from after- events?
The explanation on the docs PR refers to after events firing "when a particular phase has finished".
The distinction between these two here makes it sound like "before" is used for changing how a process occurs, but "after" sounds like it only changes WHAT gets written to the DOM. Is that correct? Are both of these correct? If so, maybe check that "tip" in the docs section for updating/nuance!
There was a problem hiding this comment.
Argh, 😄 that was really confusing, thanks for pointing it out! I like the wording in the "tip" much better. The after event is not about what is written to the DOM. Making changes to the DOM was just one example of what you can do when you know a phase is over.
|
@bluwy Yeah I have similar concerns but I don't think that means we shouldn't release this. This is probably the biggest increase we've had to the router, but it's probably the last big one we'll ever do. The initial version of the router was not configurable, but now with the addition of the events and There might be some opportunities to trim down the size in the future as well. |
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
sarah11918
left a comment
There was a problem hiding this comment.
Just a formality, but Docs is happy! 🥳
* draft new view transition events * initial state for PR * remove intraPageTransitions flag based on review comments * add createAnimationScope after review comments * remove style elements from styles after review comments * remove quotes from animation css to enable set:text * added changeset * move scrollRestoration call from popstate handler to scroll update * Update .changeset/few-keys-heal.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Less confusing after following review comments * Less confusing after following review comments --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Changes
Implementation for withastro/roadmap#740.
The earlier sections of the discussion document are out of date and merely describe the discussions we have had and the options we have considered. The content from 11/5 onwards is what will be implemented in this PR.
It might be beneficial to look at the larger diff of router.ts with the "hide whitespace options".
No changeset right now. Is this a minor?
Testing
The current tests still run without failures
Docs
How I would like to write again that these are just bug fixes, but I suspect that this time there is really something to do with the documentation. Sarah, do you have a week or two please?
Here are some paragraphs (from a technical point of view) describing the structure of the events.
Coming soon: I'm also planning some paragraphs explaining what cool things you can do with these events. (User perspective)
View Transition Events (Technical View)
View Transition Processing (bigger picture)
There are currently four types of actions that trigger Astro's View Transitions:
historyfunctionsback(),forward(), orgo()navigate(), which is provided byastro:transitions/clientProcessing begins with a preparation phase that loads the DOM of the target page. Then the actual view transition begins, which takes a screenshot of the current view (green line), swaps the current DOM with the contents of the loaded DOM and then starts the visual transition from the old to the new view (red line).
graph subgraph nav["Client-Side Navigation"] subgraph vtp["View Transition Promises"] direction LR g>updateCallbackDone]~~~h>ready]~~~i>finished] end subgraph p["Preparation Phase"] a{{astro:before-preparation}}--> b[["Loader()"<br>load DOM of target page]]--> c{{astro:after-preparation}} end subgraph vt["View Transition"] d([startViewTransition]) subgraph s["Swap DOM Phase"] t{{astro:before-swap}}--> u[["Swap()"<br>update current DOM<br>with loaded DOM]]--> v([update history state<br>and scroll position])--> w{{astro:after-swap}} end subgraph f["Completion Phase"] x([Run scripts])--> y{{astro:page-load}}--> z([Announce Route])--> zz([await finished])--> zzz([Cleanup]) end end p-->d==>s==>f d-.->vtp end linkStyle 13 stroke:#faa linkStyle 12 stroke:#afa style b stroke:#585,stroke-width:1px,color:#fff style nav fill:#888,stroke:#555,stroke-width:4px,color:#fff style p fill:#564,stroke:#555,stroke-width:4px,color:#fff style vt fill:#456,stroke:#555,stroke-width:4px,color:#fff style s fill:#457,stroke:#777,stroke-width:4px,color:#fff style vtp fill:#467,stroke:#777,stroke-width:4px,color:#fff style f fill:#654,stroke:#777,stroke-width:4px,color:#fff style x stroke:#855,stroke-width:1px,color:#fff style z stroke:#855,stroke-width:1px,color:#fff style a fill:#fea,stroke:#ddd,stroke-width:2px,color:#000 style c fill:#fea,stroke:#ddd,stroke-width:2px,color:#000 style t fill:#fea,stroke:#ddd,stroke-width:2px,color:#000 style w fill:#fea,stroke:#ddd,stroke-width:2px,color:#000 style y fill:#fea,stroke:#ddd,stroke-width:2px,color:#000In the completion phase, the newly added scripts are executed, the page title of the new route is announced for users of assistive technologies and some clean-up work is carried out at the end.
During the view transition, a view transition object exists with three promises that are resolved or rejected at different points during the visual transition. Users can use these to trigger custom code. For details see the View Transition documentation on MDN.
Events
The yellow blocks in the diagram above mark the positions in Astro's navigation processing at which events are triggered. There are five events.
astro:before-preparationandastro:after-preparationat the beginning and end of the preparation phase.astro:before-swapandastro:after-swapat the beginning and end of the swap phase.astro:page-loadduring the closing phase.The
astro:after-events and theastro:page-loadevent are standardEventobjects. Their main purpose is to allow the user code to react to navigation behavior at different points of processing.The
astro:beforeevents provide navigation-specific propertiesthat show details of the processing. Some of these properties are even writable to give users control over the behavior of navigation processing.
All five events fire on
window.document.The
astro:before-preparationEventWhile the event listener code must run synchronously, the loader function will perform asynchronous actions.
The
loaderproperty initially holds Astro's built-in implementation for loading the contents ofevent.tointoevent.newDocument. An event listener might override the value of theloaderproperty to define a completely independent implementation. But more often than not, a listener will keep the original value ofloaderand use it to define its own version.Several listener might cooperate and build a Chain of Responsibility by wrapping the loader in several layers like an onion.
astro:before-preparationTransitionBeforePreparationEventThe
astro:before-swapEventMost of the properties of the
astro:before-swapevent are identical to those of theastro:before-preparationevent. The differences are:directionisreadonlyinastro:before-swaploaderis not defined inastro:before-swapThe
astro:before-swapevent offeres two additional properties:The
swap()method of theastro:before-swapevent is used in a very similar way asloader()ofastro:before-preparationevent. It is possible to override the built-in implementation by assigning to theswapproperty and it is possible to build chains with several wrappers. Other tanloaderswap does not support asynchronous actions. Browser implementations of view transition put a rather rigid timout on the code that can run during swap. Long running code should be moved to the loader callback.astro:before-swapTransitionBeforeSwapEventData flow between
astro:before-*events:The initial state of the
astro:before-swapevent is derived from the final state of theastro:before-preparationevent of the same transition. Data set by a listener can be seen by subsequent listeners. Data entered during theastro:before-preparationdispatch can be seen during theastro:before-swapdispatch. This also applies in particular to the (user) info object that exists in every event.