The View Transitions API: Enhancing the feel of the web

By Dave Bitter

4 min read

The View Transitions API has landed in Chrome. Let’s have a look at how the API works and why it will change the feel of the web.

The _View Transitions API_: Enhancing the feel of the web
Authors

As a developer, I am always on the lookout for new tools and technologies to make my work easier and more efficient, but simultaneously increase the user experience of it. That's why I was excited to explore the View Transitions API, which is now stable in Chrome and see how it could simplify the process of creating view transitions on the web.

What is the View Transitions API?

The View Transitions API is a powerful tool that simplifies the process of creating view transitions on the web. It provides a set of built-in transition effects that can be easily customized and combined to create more complex transitions to transition from one page to another.

I love how this API can easily improve the user experience by adding a small layer to a website. It's a great way to create dynamic and interactive web experiences that will take the websites you build to the next level.

A practical use case

For my demo of the View Transitions API, I created a fictitious sneaker company in the future called Neon Sole Society. All content, including the company name, product brands, product pricing, product descriptions, and even product images, are generated with AI. This is a fun way to make your demo’s more realistic.

Screen recording of the demo showing an overview with sneakers and a transition to a detailed view of a pair of sneakers

I created a page that displays different types of futuristic sneakers, and when a user clicks on a pair , the page seamlessly transitions to a detailed view of that shoe. I used the built-in transition effects provided by the View Transitions API without any tweaking to see how well it does. Throughout this article, I show simplified code examples that are focused solely on the View Transitions API, making it easy for you to understand how to implement these transitions in their own projects. If you’d like to see all of the code, you can view the project on GitHub. If you’d like to see the demo in action, head over here. Keep in mind that at the time of writing, only Chrome has support for this.

Creating the view transition

To create a view transition, I first added a unique value for the CSS property view-transition-name for every product on the overview page. Here's an example of how this might look in the HTML for the demo’s sake:

<li
  class="product"
  style="view-transition-name: waverider-pro-flytech-details"
  data-product="waverider-pro-flytech"
>
  <div class="product__details" data-product-details>
    <p class="product__name" data-product-name>WaveRider Pro</p>
    <p class="product__brand" data-product-brand>FlyTech</p>
    <p class="product__price" data-product-price>$170</p>
    <a class="product__link" data-product-link>View</a>
    <p class="product__description" data-product-description>
      Introducing FlyTech's WaveRider Pro - the ultimate sneaker for runners and athletes. With a
      sleek and aerodynamic design, it offers unparalleled speed and comfort. Made with the latest
      FlyTech materials for durability and performance, it features a wave-shaped sole for
      unbeatable traction and advanced sensors for real-time feedback on performance. With built-in
      GPS and Bluetooth connectivity, it's perfect for achieving your personal best. Get yours today
      and experience the ultimate in futuristic footwear!
    </p>
  </div>
  <div class="product__image-wrapper">
    <img
      src="./images/products/waverider-pro-flytech.png"
      class="product__image"
      data-product-image
    />
  </div>
</li>

Next, I added an event listener to each product’s link to the detail page. I can then call preventDefault and implement the logic for the transition. If the View Transitions API is not supported, I call the handleViewProduct function directly, which takes care of updating the route and DOM in a single-page application (SPA) way. If the API is supported, I call the function in the callback function provided to document.startViewTransition. Here's an example:

const addEventListenersForProduct = (productElement, product) => {
  const productLinkElement = productElement.querySelector('[data-product-link]')

  productLinkElement.addEventListener('click', (e) => {
    e.preventDefault()

    if (!document.startViewTransition) {
      handleViewProduct(product)
    } else {
      document.startViewTransition(() => {
        handleViewProduct(product)
      })
    }
  })
}

Finally, in the handleViewProduct function, you can set the view-transition-name of the clicked product as the view-transition-name of the detail page's product element and render the page. Here's an example:

const handleViewProduct = (product) => {
  renderProductDetail()

  window.history.pushState(
    product.slug,
    `${product.name} - ${product.brand}`,
    `/product/${product.slug}`
  )

  const productDetailElement = document.querySelector('[data-product-detail]')
  const { name, brand, price, description, image, slug } = product

  productDetailElement.querySelector('[data-product-name]').innerHTML = name
  productDetailElement.querySelector('[data-product-brand]').innerHTML = brand
  productDetailElement.querySelector('[data-product-price]').innerHTML = price
  productDetailElement.querySelector('[data-product-description]').innerHTML = description
  productDetailElement.querySelector('[data-product-image]').src = image
  productDetailElement.style.viewTransitionName = `${slug}-details`
}

And that’s all! The element will now use sensible defaults to animate between the two views. From here on out, you can tweak the animations to your liking with CSS.

What will this mean for the web?

Before the View Transitions API, creating smooth and visually appealing view transitions on the web was a difficult and complex process. You had to rely on custom code or third-party libraries to achieve the desired effect, which often led to bloated code and slower page load times. Additionally, the process of creating these transitions was often time-consuming and required a deep understanding of complex CSS animations and JavaScript.

With the View Transitions API, creating seamless view transitions is now easier and more accessible than ever before. By providing a set of built-in transition effects and a simple API for applying these effects to elements on the page, you can create dynamic and engaging user interfaces without the need for custom code or third-party libraries. This not only simplifies the development process but also leads to faster page load times and a more seamless user experience overall.

As more developers begin to incorporate the View Transitions API into their projects, we can expect to see a shift towards more engaging and interactive web experiences, ultimately changing the way we navigate and interact with websites and web applications.


Upcoming events

  • The 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? 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
  • Coven of Wisdom - Herentals - Winter `24 edition

    Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiënte 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 – 📢 Talk 1 20:00 – 🍹 Kleine pauze 20:15 – 📢 Talk 2 21:00 – 🙋‍♀️ Drinks 22:00 – 🍻 Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. 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 edition
  • Mastering Event-Driven Design

    PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋‍♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.

    | Coven of Wisdom - Utrecht

    Go to page for Mastering Event-Driven Design

Share