-
-
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
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
42dd59b
draft new view transition events
martrapp 28c6286
initial state for PR
martrapp 15596ff
Merge branch 'main' into mt/events
martrapp 700333c
remove intraPageTransitions flag based on review comments
martrapp a70f8f9
add createAnimationScope after review comments
martrapp ca803dd
remove style elements from styles after review comments
martrapp a7c8704
remove quotes from animation css to enable set:text
martrapp e431453
Merge branch 'main' into mt/events
martrapp 8617900
added changeset
martrapp 93eb6ea
Merge branch 'main' into mt/events
martrapp 769f78a
move scrollRestoration call from popstate handler to scroll update
martrapp 3ef0f89
Update .changeset/few-keys-heal.md
martrapp effacaf
Less confusing after following review comments
martrapp 10234e2
Less confusing after following review comments
martrapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
Take full control over the behavior of view transitions! | ||
|
||
Three new events now complement the existing `astro:after-swap` and `astro:page-load` events: | ||
|
||
``` javascript | ||
astro:before-preparation // Control how the DOM and other resources of the target page are loaded | ||
astro:after-preparation // Last changes before taking off? Remove that loading indicator? Here you go! | ||
astro:before-swap // Control how the DOM is updated to match the new page | ||
``` | ||
|
||
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. | ||
Head over to [View Transition docs](https://docs.astro.build/en/guides/view-transitions/) to find out more about the events! | ||
martrapp marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { updateScrollPosition } from './router.js'; | ||
import type { Direction, NavigationTypeString } from './types.js'; | ||
|
||
export const TRANSITION_BEFORE_PREPARATION = 'astro:before-preparation'; | ||
export const TRANSITION_AFTER_PREPARATION = 'astro:after-preparation'; | ||
export const TRANSITION_BEFORE_SWAP = 'astro:before-swap'; | ||
export const TRANSITION_AFTER_SWAP = 'astro:after-swap'; | ||
export const TRANSITION_PAGE_LOAD = 'astro:page-load'; | ||
|
||
type Events = | ||
| typeof TRANSITION_AFTER_PREPARATION | ||
| typeof TRANSITION_AFTER_SWAP | ||
| typeof TRANSITION_PAGE_LOAD; | ||
export const triggerEvent = (name: Events) => document.dispatchEvent(new Event(name)); | ||
export const onPageLoad = () => triggerEvent(TRANSITION_PAGE_LOAD); | ||
|
||
/* | ||
* Common stuff | ||
*/ | ||
class BeforeEvent extends Event { | ||
readonly from: URL; | ||
to: URL; | ||
direction: Direction | string; | ||
readonly navigationType: NavigationTypeString; | ||
readonly sourceElement: Element | undefined; | ||
readonly info: any; | ||
newDocument: Document; | ||
|
||
constructor( | ||
type: string, | ||
eventInitDict: EventInit | undefined, | ||
from: URL, | ||
to: URL, | ||
direction: Direction | string, | ||
navigationType: NavigationTypeString, | ||
sourceElement: Element | undefined, | ||
info: any, | ||
newDocument: Document | ||
) { | ||
super(type, eventInitDict); | ||
this.from = from; | ||
this.to = to; | ||
this.direction = direction; | ||
this.navigationType = navigationType; | ||
this.sourceElement = sourceElement; | ||
this.info = info; | ||
this.newDocument = newDocument; | ||
|
||
Object.defineProperties(this, { | ||
from: { enumerable: true }, | ||
to: { enumerable: true, writable: true }, | ||
direction: { enumerable: true, writable: true }, | ||
navigationType: { enumerable: true }, | ||
sourceElement: { enumerable: true }, | ||
info: { enumerable: true }, | ||
newDocument: { enumerable: true, writable: true }, | ||
}); | ||
} | ||
} | ||
|
||
/* | ||
* TransitionBeforePreparationEvent | ||
|
||
*/ | ||
export const isTransitionBeforePreparationEvent = ( | ||
value: any | ||
): value is TransitionBeforePreparationEvent => value.type === TRANSITION_BEFORE_PREPARATION; | ||
export class TransitionBeforePreparationEvent extends BeforeEvent { | ||
formData: FormData | undefined; | ||
loader: () => Promise<void>; | ||
constructor( | ||
from: URL, | ||
to: URL, | ||
direction: Direction | string, | ||
navigationType: NavigationTypeString, | ||
sourceElement: Element | undefined, | ||
info: any, | ||
newDocument: Document, | ||
formData: FormData | undefined, | ||
loader: (event: TransitionBeforePreparationEvent) => Promise<void> | ||
) { | ||
super( | ||
TRANSITION_BEFORE_PREPARATION, | ||
{ cancelable: true }, | ||
from, | ||
to, | ||
direction, | ||
navigationType, | ||
sourceElement, | ||
info, | ||
newDocument | ||
); | ||
this.formData = formData; | ||
this.loader = loader.bind(this, this); | ||
Object.defineProperties(this, { | ||
formData: { enumerable: true }, | ||
loader: { enumerable: true, writable: true }, | ||
}); | ||
} | ||
} | ||
|
||
/* | ||
* TransitionBeforeSwapEvent | ||
*/ | ||
|
||
export const isTransitionBeforeSwapEvent = (value: any): value is TransitionBeforeSwapEvent => | ||
value.type === TRANSITION_BEFORE_SWAP; | ||
export class TransitionBeforeSwapEvent extends BeforeEvent { | ||
readonly direction: Direction | string; | ||
readonly viewTransition: ViewTransition; | ||
swap: () => void; | ||
|
||
constructor( | ||
afterPreparation: BeforeEvent, | ||
viewTransition: ViewTransition, | ||
swap: (event: TransitionBeforeSwapEvent) => void | ||
) { | ||
super( | ||
TRANSITION_BEFORE_SWAP, | ||
undefined, | ||
afterPreparation.from, | ||
afterPreparation.to, | ||
afterPreparation.direction, | ||
afterPreparation.navigationType, | ||
afterPreparation.sourceElement, | ||
afterPreparation.info, | ||
afterPreparation.newDocument | ||
); | ||
this.direction = afterPreparation.direction; | ||
this.viewTransition = viewTransition; | ||
this.swap = swap.bind(this, this); | ||
|
||
Object.defineProperties(this, { | ||
direction: { enumerable: true }, | ||
viewTransition: { enumerable: true }, | ||
swap: { enumerable: true, writable: true }, | ||
}); | ||
} | ||
} | ||
|
||
export async function doPreparation( | ||
from: URL, | ||
to: URL, | ||
direction: Direction | string, | ||
navigationType: NavigationTypeString, | ||
sourceElement: Element | undefined, | ||
info: any, | ||
formData: FormData | undefined, | ||
defaultLoader: (event: TransitionBeforePreparationEvent) => Promise<void> | ||
) { | ||
const event = new TransitionBeforePreparationEvent( | ||
from, | ||
to, | ||
direction, | ||
navigationType, | ||
sourceElement, | ||
info, | ||
window.document, | ||
formData, | ||
defaultLoader | ||
); | ||
if (document.dispatchEvent(event)) { | ||
await event.loader(); | ||
if (!event.defaultPrevented) { | ||
triggerEvent(TRANSITION_AFTER_PREPARATION); | ||
if (event.navigationType !== 'traverse') { | ||
// save the current scroll position before we change the DOM and transition to the new page | ||
updateScrollPosition({ scrollX, scrollY }); | ||
} | ||
} | ||
} | ||
return event; | ||
} | ||
|
||
export async function doSwap( | ||
afterPreparation: BeforeEvent, | ||
viewTransition: ViewTransition, | ||
defaultSwap: (event: TransitionBeforeSwapEvent) => void | ||
) { | ||
const event = new TransitionBeforeSwapEvent(afterPreparation, viewTransition, defaultSwap); | ||
document.dispatchEvent(event); | ||
event.swap(); | ||
return event; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 fromafter-
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.