The future of page transitions with Shared Element Transitions
The future of page transitions with Shared Element Transitions
By Milan Vogels
4 min read
The Shared Element Transitions API allows creating page transitions using a browser API that can provide users with a better visual connection between page-a and page-b by transitioning shared elements on both pages.
- Authors
- Name
- Milan Vogels
- linkedinMilan Vogels
- twitter@milanvogels
In native mobile app development transitioning from one page/state to another is something that has been supported for a long time. For example, you can do this using Activity transitions for Android. For the web, however, things aren't all that easy.
Yes, we can create transitions between pages using libraries like BarbaJs. But these don't provide any visual connection between page-a and page-b to the user. So to close the gap, a new browser API is in the works: Shared Element Transition API.
The Shared Element Transition is a proposal for a new Web API. It allows for creating transition animations between page navigation and transitioning elements that are present on both pages.
Getting started
To get started with the Shared Element Transition API the following flags have to be enabled in Chrome:
chrome://flags/#document-transition
chrome://flags/#enable-experimental-web-platform-features
First of all, we need to check if the browser supports the API, and add a fallback if they don't.
async function navigate() {
if (!document.createDocumentTransition) {
updateDOM(); // update the DOM
return;
}
}
If it does support the new API, we can use the createDocumentTransition
function to create a new transition.
async function navigate() {
if (!document.createDocumentTransition) {
updateDOM(); // update the DOM
return;
}
const transition = document.createDocumentTransition();
await transition.start(() => updateDOM());
}
The default transition the API creates is a simple fade. I created a small CodePen example that just toggles between a card view and a detail view by toggling the display style.
Changing the animation
The animation is using CSS. The default browser UA styles will animate ::page-transition-incoming-image
from opacity: 0;
and ::page-transition-outgoing-image
to opacity: 0;
.
We can add our own animations by using the following pseudo-element selectors
::page-transition
::page-transition-container(root)
::page-transition-image-wrapper(root)
::page-transition-outgoing-image(root)
::page-transition-incoming-image(root)
For example, we can change the animation to a slide out:
@keyframes slide-to-top {
to {
transform: translateY(-100%);
}
}
::page-transition-outgoing-image(root) {
animation: 400ms ease-out both slide-to-top;
}
::page-transition-incoming-image(root) {
animation: none; /* disable default animation */
}
To animate separate elements, instead of the whole page, we can replace root
with a page-transition-tag
property value you can add to another css selector. The default animation transitions the width
and height
attributes from the 'before' state to the 'after' state. Also, it animates the transform
to update to the position of the targeted element.
In the following example, we add a page-transition-tag: article-img
to an image tag.
.foo img {
page-transition-tag: article-img;
}
::page-transition-outgoing-image(article-image) {}
::page-transition-incoming-image(article-image) {}
This gives us an already pretty cool effect.
If we want specific elements of our site to stay in place during the transition, we have to add a page-transition-tag
and contain: paint
. Assuming the element won't change between page-a and page-b, the element will now remain in the same place.
.header {
page-transition-tag: header;
contain: paint;
}
We can't have the same page-transition-tag
being used multiple times on the same page. We'll get an error in the console when starting the transition:
Unexpected duplicate page transition tag: tag-name
So instead of defining it in CSS, we can add it with JavaScript, for example on a click event.
async function navigate() {
if (!document.createDocumentTransition) {
updateDOM(); // update the DOM
return;
}
const foo = GetElementYouClickedOn();
foo.style.pageTransitionTag = 'article-img';
const transition = document.createDocumentTransition();
await transition.start(() => updateDOM());
foo.style.pageTransitionTag = '';
}
Here's an example of a full grid of cards animating each card's image to the full article image. It still shows the singular article detail view we created, hence the detail image doesn't reflect the card's image.
Multi-page applications
The current iteration of the Shared Element Transition API only supports Single-page applications (SPAs). Support for Multi-page applications (MPAs) is on the roadmap to be added in the future. The use of the API for SPAs or MPAs should work similarly.
Start experimenting!
I'm really excited about this API myself, it allows us to create page transitions that can add context to the user in a non-complicated way without the use of any 3rd party libraries. The Shared Element Transition API is still in an early phase and it will take some time until it's ready for shipping and implemented by other browsers, but we can already experiment a lot with it.
I would like to encourage you to do the same and share your opinions in the Github repository. Can't wait to see what others can create with this new API.
Bright times ahead for page transitions!
Upcoming events
Drupal CMS Launch Party
Zoals sommigen misschien weten wordt op 15 Januari een nieuwe distributie van Drupal gelanceerd. Namelijk Drupal CMS (ook wel bekend als Starshot). Om dit te vieren gaan we op onze campus een klein eventje organiseren. We gaan die dag samen de livestream volgen waarbij het product gelanceerd wordt. De agenda is als volgt: 17u β 18u30: Drupal CMS livestream met taart 18u30 β 19u00: Versteld staan van de functionaliteiten 19u β 20u: Pizza eten en verder versteld staan van de functionaliteiten Laat ons zeker weten of je komt of niet door de invite te accepteren! Tot dan!
| Coven of Wisdom Herentals
Go to page for Drupal CMS Launch PartyCoven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met het bouwen van schaalbare digitale ecosystemen of zit je vast in een props hell met React of in een ander framework? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en flexibele applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! π 18:00 β πͺ Deuren open 18:15 β π Food & drinks 19:00 β π’ Building a Mature Digital Ecosystem - Maarten Heip 20:00 β πΉ Kleine pauze 20:15 β π’ Compound Components: A Better Way to Build React Components - Sead Memic 21:00 β πββοΈ Drinks 22:00 β π» Tot de volgende keer? Tijdens deze meetup gaan we dieper in op het bouwen van digitale ecosystemen en het creΓ«ren van herbruikbare React componenten. Maarten deelt zijn expertise over het ontwikkelen van een volwassen digitale infrastructuur, terwijl Sead je laat zien hoe je 'From Props Hell to Component Heaven' kunt gaan door het gebruik van Compound Components. Ze delen praktische inzichten die je direct kunt toepassen in je eigen projecten. π Waar? Je vindt ons bij iO Herentals - Zavelheide 15, Herentals. Volg bij aankomst de borden 'meetup' vanaf de receptie. π« Schrijf je in! De plaatsen zijn beperkt, dus RSVP is noodzakelijk. Dit helpt ons ook om de juiste hoeveelheid eten en drinken te voorzien - we willen natuurlijk niet dat iemand met een lege maag naar huis gaat! π Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creΓ«ert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionThe Test Automation Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Test automation is a cornerstone of effective software development. It's about creating robust, predictable test suites that enhance quality and reliability. By diving into automation, you're architecting systems that ensure consistency and catch issues early. This expertise not only improves the development process but also broadens your skillset, making you a more versatile team member. Whether you're a developer looking to enhance your testing skills or a QA professional aiming to dive deeper into automation, RSVP for an evening of learning, delicious food, and the fusion of coding and quality assurance! ππ 18:00 β πͺ Doors open to the public 18:15 β π Letβs eat 19:00 β π’ First round of Talks 19:45 β πΉ Small break 20:00 β π’ Second round of Talks 20:45 β π» Drinks 21:00 β πββοΈ See you next time? First Round of Talks: The Power of Cross-browser Component Testing - Clarke Verdel, SR. Front-end Developer at iO How can you use Component Testing to ensure consistency cross-browser? Overcoming challenges in Visual Regression Testing - Sander van Surksum, Pagespeed | Web Performance Consultant and Sannie Kwakman, Freelance Full-stack Developer How can you overcome the challenges when setting up Visual Regression Testing? Second Round of Talks: Omg who wrote this **** code!? - Erwin Heitzman, SR. Test Automation Engineer at Rabobank How can tests help you and your team? Beyond the Unit Test - Christian WΓΌrthner, SR. Android Developer at iO How can you do advanced automated testing for, for instance, biometrics? RSVP now to secure your spot, and let's explore the fascinating world of test automation together!
| Coven of Wisdom - Amsterdam
Go to page for The Test Automation Meetup