-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
New events for Astro's view transition API #9090
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/: |
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.
This PR is blocked because it contains a minor
changeset. A reviewer will merge this at the next release if approved.
@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. |
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.
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!
.changeset/few-keys-heal.md
Outdated
``` | ||
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
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.
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:
history
functionsback()
,forward()
, orgo()
navigate()
, which is provided byastro:transitions/client
Processing 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).
In 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-preparation
andastro:after-preparation
at the beginning and end of the preparation phase.astro:before-swap
andastro:after-swap
at the beginning and end of the swap phase.astro:page-load
during the closing phase.The
astro:after-
events and theastro:page-load
event are standardEvent
objects. Their main purpose is to allow the user code to react to navigation behavior at different points of processing.The
astro:before
events 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-preparation
EventWhile the event listener code must run synchronously, the loader function will perform asynchronous actions.
The
loader
property initially holds Astro's built-in implementation for loading the contents ofevent.to
intoevent.newDocument
. An event listener might override the value of theloader
property to define a completely independent implementation. But more often than not, a listener will keep the original value ofloader
and 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-preparation
TransitionBeforePreparationEvent
The
astro:before-swap
EventMost of the properties of the
astro:before-swap
event are identical to those of theastro:before-preparation
event. The differences are:direction
isreadonly
inastro:before-swap
loader
is not defined inastro:before-swap
The
astro:before-swap
event offeres two additional properties:The
swap()
method of theastro:before-swap
event is used in a very similar way asloader()
ofastro:before-preparation
event. It is possible to override the built-in implementation by assigning to theswap
property and it is possible to build chains with several wrappers. Other tanloader
swap 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-swap
TransitionBeforeSwapEvent
Data flow between
astro:before-*
events:The initial state of the
astro:before-swap
event is derived from the final state of theastro:before-preparation
event of the same transition. Data set by a listener can be seen by subsequent listeners. Data entered during theastro:before-preparation
dispatch can be seen during theastro:before-swap
dispatch. This also applies in particular to the (user) info object that exists in every event.