Skip to content

Commit

Permalink
startViewTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
Weedshaker committed Dec 20, 2023
1 parent 6b5648d commit 8e87083
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

/** @typedef {{
info: Promise<{ route: Route, location: string, rendered: boolean } | TypeError>
info: Promise<{ route: Route, location: string, rendered: boolean, transition: any } | TypeError>
}} RouteEventDetail
*/

Expand Down Expand Up @@ -221,9 +221,18 @@ export default class Router extends HTMLElement {
return route.component // eslint-disable-line
})).then(component => {
let rendered = false
if ((rendered = this.shouldComponentRender(route.name, isUrlEqual))) this.render(component)
let transition = null
if ((rendered = this.shouldComponentRender(route.name, isUrlEqual))) {
// @ts-ignore
if (document.startViewTransition) {
// @ts-ignore
transition = document.startViewTransition(() => Promise.resolve(this.render(component))) // https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
} else {
this.render(component)
}
}
if (route.scrollIntoView) component.scrollIntoView()
return { route, location: hash, rendered }
return { route, location: hash, rendered, transition }
// @ts-ignore
}).catch(error => {
// force re-fetching at browser level incase it was offline at time of fetch
Expand Down

0 comments on commit 8e87083

Please sign in to comment.