Debian Codegraph @

www/src/content/essays/view-transitions.md

raw symbols SCIP
1 ---
2 title: "View Transitions"
3 description: "Carson Gross explores the evolution of web applications and the significance of view transitions in improving user experience. He discusses the limitations of traditional web 01-frontend, where full-page refreshes create an unpleasant experience, and how modern technologies like CSS transitions and the View Transition API aim to enhance aesthetic smoothness. Carson explains how htmx leverages the View Transition API to bring seamless transitions to hypermedia-driven applications, offering an alternative to single-page applications (SPAs) and highlighting its potential once widely available in HTML."
4 created: 2023-04-11
5 authors: [ "Carson Gross" ]
6 tags: ["guides"]
7 includeMockServer: true
8 ---
9
10 We have asserted, for a while now, that a major reason that many people have adopted the SPA architecture for web
11 applications
12 is due to aesthetic considerations.
13
14 As we mention in our book [Hypermedia Systems](https://hypermedia.systems), when
15 discussing the Web 1.0-style contact management application we begin with, there are serious _aesthetic_ issues with
16 the application, even if it has feature-parity with an SPA version:
17
18 > From a user experience perspective: there is a noticeable refresh when you move between pages of the application, or
19 > when you create, update or
20 > delete a contact. This is because every user interaction (link click or form submission) requires a full page
21 > refresh, with a whole new HTML document to process after each action.
22 >
23 > *–Hypermedia Systems - [Chapter 4](https://hypermedia.systems/extending-html-as-hypermedia/)*
24
25 This jarring "ka-chunk" between webpages, often with
26 a [Flash of Unstyled Content](https://webkit.org/blog/66/the-fouc-problem/)
27 has been with us forever and, while modern browsers have improved the situation somewhat (while, unfortunately, also
28 making
29 it less obvious that a request is in flight) the situation is still bad, particularly when compared with what a
30 well-crafted
31 SPA can achieve.
32
33 Now, early on in the life of the web, this wasn't such a big deal. We had stars flying around dinosaurs _in the
34 browser's toolbar_,
35 flaming text, table-based layouts, dancing babies and so forth, and we were largely comparing the web with things like
36 ftp clients.
37
38 The bar was _low_ and the times were _good_.
39
40 Alas, the web has since put away such childish things, and now we are expected to present polished, attractive
41 interfaces
42 to our users, _including_ smooth transitions from one view state to another.
43
44 Again, we feel this is why many teams default to the SPA approach: the old way just seems... clunky.
45
46 ## CSS Transitions
47
48 The early web engineers realized that web developers would like to provide smooth transitions between different view
49 states
50 and have offered various technologies for achieving this. A major one
51 is [CSS Transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/transition),
52 which allow you to specify a mathematical _transition_ from one state to another.
53
54 Unfortunately for HTML, CSS transitions are only available if you use JavaScript: you have to change elements
55 dynamically
56 in order to trigger the transitions, which "vanilla" HTML can't do. In practice, this meant that only the cool kids
57 using JavaScript to build SPAs got to use these tools, further cementing the _aesthetic superiority_ of SPAs.
58
59 htmx, as you probably know, makes CSS Transitions [available in plain HTML](https://htmx.org/patterns/display/animations/) via
60 a somewhat elaborate [swapping model](https://htmx.org/docs/#request-operations) where we take elements that are in both
61 the old and new content and "settle" attributes on them. It's a neat trick and can be used to make hypermedia-driven
62 application feel as buttery-smooth as well done SPA.
63
64 However, there is a new kid on the
65 block: [The View Transition API](https://developer.chrome.com/docs/web-platform/view-transitions/)
66
67 ## The View Transition API
68
69 The View Transition API is much more ambitious than CSS transitions in that it is attempting to provide a simple,
70 intuitive
71 API for transitioning an _entire DOM_ from one state to another in a way that mere mortals can take advantage of.
72
73 Furthermore, this API is supposed to be available not only in JavaScript, but also for plain old links and forms in HTML
74 as well,
75 making it possible to build _much nicer_ user interfaces using the Web 1.0 approach.
76
77 It will be fun to revisit the Contact application in "Hypermedia Systems" when this functionality is available!
78
79 As of this writing, however, the API is, like CSS Transitions, only available in JavaScript, and its only been just
80 released in Chrome 111+.
81
82 In JavaScript, The API could not be more simple:
83
84 ```js
85
86 // this is all it takes to get a smooth transition from one
87 // state to another!
88 document.startViewTransition(() => updateTheDOMSomehow(data));
89
90 ```
91
92 Now, that's my kind of API.
93
94 As luck would have it, it's trivial to wrap this API around the regular htmx swapping model, which allows us to
95 start exploring View Transitions in htmx, even before it's generally available in HTML!
96
97 And, as of [htmx 1.9.0](https://cdn.jsdelivr.net/npm/[email protected]), you can start experimenting with the API by adding
98 the
99 `transition:true` attribute to an [`hx-swap`](/reference/attributes/hx-swap) attribute.
100
101 ## A Practical Example
102
103 So let's look at a simple example of this new shiny toy coupled with htmx.
104
105 Doing so will involve two parts:
106
107 * Defining our View Transition animation via CSS
108 * Adding a small annotation to an htmx-powered button
109
110 ### The CSS
111
112 The first thing that we need to do is define the View Transition animation that we want.
113
114 * Define some animations using @keyframes to slide and fade content
115 * Define a view transition with the name `slide-it` using the `:view-transition-old()` and `:view-transition-new()`
116 pseudo-selectors
117 * Tie the `.sample-transition` class to the `slide-it` view transition that we just defined, so we can bind it to
118 elements via a that CSS class name
119
120 (Fuller details on the View Transition API can be found on
121 the [Chrome Developer Page](https://developer.chrome.com/docs/web-platform/view-transitions/)
122 documenting them.)
123
124 ```html
125
126 <style>
127 @keyframes fade-in {
128 from { opacity: 0; }
129 }
130
131 @keyframes fade-out {
132 to { opacity: 0; }
133 }
134
135 @keyframes slide-from-right {
136 from { transform: translateX(90px); }
137 }
138
139 @keyframes slide-to-left {
140 to { transform: translateX(-90px); }
141 }
142
143 /* define animations for the old and new content */
144 ::view-transition-old(slide-it) {
145 animation: 180ms cubic-bezier(0.4, 0, 1, 1) both fade-out,
146 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;
147 }
148 ::view-transition-new(slide-it) {
149 animation: 420ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in,
150 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;
151 }
152
153 /* tie the view transition to a given CSS class */
154 .sample-transition {
155 view-transition-name: slide-it;
156 }
157
158 </style>
159
160 ```
161
162 This CSS sets it up such that content with the `.sample-transition` class on it will fade out and slide to the left when
163 it is removed, and new content will fade in and slide in from the right.
164
165 ### The HTML
166
167 With our View Transition defined via CSS, the next thing to do is to tie this View Transition to an actual element that
168 htmx will mutate, and to specify that htmx should take advantage of the View Transition API:
169
170 ```html
171
172 <div class="sample-transition">
173 <h1>Initial Content</h1>
174 <button hx-get="/new-content"
175 hx-swap="innerHTML transition:true"
176 hx-target="closest div">
177 Swap It!
178 </button>
179 </div>
180
181 ```
182
183 Here we have a button that issues an `GET` to get some new content, and that replaces the closest div's inner HTML
184 with the response.
185
186 That div has the `sample-transition` class on it, so the View Transition defined above will apply to it.
187
188 Finally, the `hx-swap` attribute includes the option, `transition:true`, which is what tells htmx to use the
189 internal View Transition JavaScript API when swapping.
190
191 ## Demo
192
193 With all that tied together, we are ready to start using the View Transition API with htmx. Here's a demo, which
194 should work in Chrome 111+ (other browsers will work fine, but won't get the nice animation):
195
196 <style>
197 @keyframes slide-from-right {
198 from { transform: translateX(100%); }
199 }
200
201 @keyframes slide-to-left {
202 to { transform: translateX(-100%); }
203 }
204
205 ::view-transition-group(slide-it) {
206 animation: none;
207 overflow: hidden;
208 }
209
210 ::view-transition-old(slide-it) {
211 animation: 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;
212 }
213 ::view-transition-new(slide-it) {
214 animation: 300ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;
215 }
216
217 /* Reverse direction on restore. */
218 :root.vt-reverse::view-transition-old(slide-it) {
219 animation: 300ms cubic-bezier(0.4, 0, 0.2, 1) both reverse slide-from-right;
220 }
221 :root.vt-reverse::view-transition-new(slide-it) {
222 animation: 300ms cubic-bezier(0.4, 0, 0.2, 1) both reverse slide-to-left;
223 }
224
225 .sample-transition {
226 view-transition-name: slide-it;
227 }
228
229 </style>
230
231
232 <script>
233 const primaryBtn = "cursor-pointer inline-flex items-center justify-center h-10 px-5 rounded-lg bg-linear-to-b from-blue-600 to-blue-700 dark:from-blue-500 dark:to-blue-600 interact:from-blue-500 interact:to-blue-600 dark:interact:from-blue-400 dark:interact:to-blue-500 text-white font-chicago text-sm border border-blue-500 dark:border-blue-500 dark:interact:border-blue-400 transition";
234 const secondaryBtn = "cursor-pointer inline-flex items-center justify-center h-10 px-5 rounded-lg bg-neutral-100 dark:bg-neutral-850 text-neutral-900 dark:text-neutral-100 font-chicago text-sm border border-neutral-300 dark:border-neutral-700 interact:bg-neutral-200 dark:interact:bg-neutral-800 transition";
235 const heading = '<h1 class="font-chicago text-2xl tracking-tightest mb-4">';
236
237 server.get("/new-content", () =>
238 `${heading}New Content</h1>
239 <button class="${secondaryBtn}"
240 hx-get="/original-content"
241 hx-swap="innerHTML transition:true"
242 hx-target="closest .sample-transition"
243 hx-on::config:request="document.documentElement.classList.add('vt-reverse')">
244 Restore It!
245 </button>`);
246
247 server.get("/original-content", () =>
248 `${heading}Initial Content</h1>
249 <button class="${primaryBtn}"
250 hx-get="/new-content"
251 hx-swap="innerHTML transition:true"
252 hx-target="closest .sample-transition"
253 hx-on::config:request="document.documentElement.classList.remove('vt-reverse')">
254 Swap It!
255 </button>`);
256 </script>
257
258 <div class="not-prose demo-container flex items-center justify-center min-h-[180px]">
259 <div class="sample-transition w-full text-center">
260 <h1 class="font-chicago text-2xl tracking-tightest mb-4">Initial Content</h1>
261 <button class="cursor-pointer inline-flex items-center justify-center h-10 px-5 rounded-lg bg-linear-to-b from-blue-600 to-blue-700 dark:from-blue-500 dark:to-blue-600 interact:from-blue-500 interact:to-blue-600 dark:interact:from-blue-400 dark:interact:to-blue-500 text-white font-chicago text-sm border border-blue-500 dark:border-blue-500 dark:interact:border-blue-400 transition"
262 hx-get="/new-content"
263 hx-swap="innerHTML transition:true"
264 hx-target="closest .sample-transition"
265 hx-on::config:request="document.documentElement.classList.remove('vt-reverse')">
266 Swap It!
267 </button>
268 </div>
269 </div>
270
271 Assuming you are looking at this page in Chrome 111+, you should see the content above slide gracefully out to the
272 left and be replaced by new content sliding in from the right. Nice!
273
274 ## Conclusion
275
276 Hey now, that's pretty neat, and, once you get your head around the concept, not all that much work! This new API
277 shows a lot of promise.
278
279 View Transitions are an exciting new technology that we feel can dramatically level the playing field between
280 [Hypermedia Driven Applications](https://htmx.org/essays/hypermedia-driven-applications/) and the more prevalent SPA
281 architecture used today.
282
283 By doing away with the ugly "ka-chunk" of Web 1.0 applications, the aesthetic advantages of the
284 SPA approach will be diminished, and we can make decisions less around "sizzle" and focus more on the actual [technical
285 tradeoffs](https://htmx.org/essays/when-to-use-hypermedia/) associated with various architectures.
286
287 We are looking forward to when View Transitions are available in vanilla HTML, but, until then, you can start playing
288 with them in htmx, today!
289