Skip to content

Commit 6e4f100

Browse files
author
Dave Bitter
committed
feat(article): add view transitions api article
1 parent e733a87 commit 6e4f100

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

content/articles/articles.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,136 @@
11
---
22
items:
3+
- type: articles
4+
body: >-
5+
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.
6+
7+
## What is the View Transitions API?
8+
9+
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.
10+
11+
12+
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.
13+
14+
## A practical use case
15+
16+
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.
17+
18+
<div style={{display: 'flex', justifyContent: 'center'}} data-reveal-in-view>
19+
<img src='/img/articles/view-transitions-api-demo.gif' alt='Screen recording of the demo showing an overview with sneakers and a transition to a detailed view of a pair of sneakers' />
20+
</div>
21+
22+
23+
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](https://github.com/DaveBitter/view-transitions-api-demo). If you’d like to see the demo in action, head over [here](https://view-transitions-api-demo.davebitter.com/). Keep in mind that at the time of writing, only Chrome has support for this.
24+
25+
26+
## Creating the view transition
27+
28+
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:
29+
30+
31+
```html
32+
33+
<li
34+
class="product"
35+
style="view-transition-name: waverider-pro-flytech-details"
36+
data-product="waverider-pro-flytech"
37+
>
38+
<div class="product__details" data-product-details>
39+
<p class="product__name" data-product-name>WaveRider Pro</p>
40+
<p class="product__brand" data-product-brand>FlyTech</p>
41+
<p class="product__price" data-product-price>$170</p>
42+
<a class="product__link" data-product-link>View</a>
43+
<p class="product__description" data-product-description>
44+
Introducing FlyTech's WaveRider Pro - the ultimate sneaker for runners and athletes. With a
45+
sleek and aerodynamic design, it offers unparalleled speed and comfort. Made with the latest
46+
FlyTech materials for durability and performance, it features a wave-shaped sole for
47+
unbeatable traction and advanced sensors for real-time feedback on performance. With built-in
48+
GPS and Bluetooth connectivity, it's perfect for achieving your personal best. Get yours today
49+
and experience the ultimate in futuristic footwear!
50+
</p>
51+
</div>
52+
<div class="product__image-wrapper">
53+
<img
54+
src="./images/products/waverider-pro-flytech.png"
55+
class="product__image"
56+
data-product-image
57+
/>
58+
</div>
59+
</li>
60+
61+
```
62+
63+
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:
64+
65+
```jsx
66+
67+
const addEventListenersForProduct = (productElement, product) => {
68+
const productLinkElement = productElement.querySelector('[data-product-link]')
69+
70+
productLinkElement.addEventListener('click', (e) => {
71+
e.preventDefault()
72+
73+
if (!document.startViewTransition) {
74+
handleViewProduct(product)
75+
} else {
76+
document.startViewTransition(() => {
77+
handleViewProduct(product)
78+
})
79+
}
80+
})
81+
}
82+
83+
```
84+
85+
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:
86+
87+
```jsx {18}
88+
89+
const handleViewProduct = (product) => {
90+
renderProductDetail()
91+
92+
window.history.pushState(
93+
product.slug,
94+
`${product.name} - ${product.brand}`,
95+
`/product/${product.slug}`
96+
)
97+
98+
const productDetailElement = document.querySelector('[data-product-detail]')
99+
const { name, brand, price, description, image, slug } = product
100+
101+
productDetailElement.querySelector('[data-product-name]').innerHTML = name
102+
productDetailElement.querySelector('[data-product-brand]').innerHTML = brand
103+
productDetailElement.querySelector('[data-product-price]').innerHTML = price
104+
productDetailElement.querySelector('[data-product-description]').innerHTML = description
105+
productDetailElement.querySelector('[data-product-image]').src = image
106+
productDetailElement.style.viewTransitionName = `${slug}-details`
107+
}
108+
109+
```
110+
111+
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.
112+
113+
## What will this mean for the web?
114+
115+
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.
116+
117+
118+
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.
119+
120+
121+
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.
122+
123+
date: 2023-04-11T00:00:00.000Z
124+
slug: the-view-transitions-api
125+
tags:
126+
- front-end
127+
intro: >-
128+
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.
129+
teaserCopy: >-
130+
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.
131+
teaserImage: /img/articles/view-transitions-api-demo.webp
132+
title: >-
133+
The File System Access API: Unlocking New Possibilities for Web Developers
3134
- type: articles
4135
body: >-
5136
The web is an incredibly powerful platform that keeps getting better. I’m always on the hunt for new capabilities, like the File System Access API, to create the best user experience on the web. In this article, we’ll have a look at what the File System Access API is, how it works and why this is such a great addition to the web as a platform.
Loading
Binary file not shown.

0 commit comments

Comments
 (0)