You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/tips/view-transition-fails-and-fixes.mdx
+44-2Lines changed: 44 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -241,9 +241,12 @@ You have no chance to lift a normal DOM element high enough to obscure one of th
241
241
242
242
<details name="fail" class="c s">
243
243
<summary>Build tools rewrite CSS and break selectors</summary>
244
-
You can spend hours staring at your CSS and questioning your own sanity. If you’ve been squinting at your IDE long enough, take a look at the page source as the browser actually sees it. Sometimes all the helpful UX tricks can get in your way. Here’s an example that often trips me up when working with Astro, even though I should really know better by now.
244
+
TL;DR: The CSS that reaches the browser might not be the same as what you see in your IDE.
245
245
246
-
Even though this example is about CSS rewriting in **Astro**, the same thing can happen with **Vue**, **Svelte**, or other frontend frameworks or even with some overzealous PostCSS plugin.
246
+
247
+
You can spend hours staring at your CSS and questioning your own sanity. After you’ve been squinting at your IDE long enough, take a look at the page source as the browser actually sees it. Sometimes all the helpful UX tricks can get in your way. Here’s an example that often trips me up when working with Astro, even though I should really know better by now.
248
+
249
+
Even though the first example is about CSS rewriting in **Astro**, the same thing can happen with **Vue**, **Svelte**, or other frontend frameworks or even with some overzealous PostCSS plugin or plain CSS Modules, as shown in the second example.
247
250
248
251
249
252
Look at this example of astyleelement in a `Layout.astro` file. This shows the recommended way to consistently slow down all your view transition animations to 2 seconds.
@@ -285,6 +288,45 @@ Or move your pseudo-element styles into a global style element using `<style is:
285
288
}
286
289
</style>
287
290
```
291
+
### CSS Modules
292
+
293
+
Here is another example from this category, kindly shared by [@larsejaas.bsky.social](https://bsky.app/profile/larsejaas.bsky.social/post/3ly4llcqf522e): When you currently use [CSS modules](https://github.com/css-modules/css-modules) with Vite, the content of your module file…
294
+
```css title="style.module.css"
295
+
.box {
296
+
view-transition-class: my-box;
297
+
}
298
+
::view-transition-group(.my-box) {
299
+
animation-duration: 0.5s;
300
+
}
301
+
```
302
+
…might be sent to the browser as…
303
+
```css title="What the browser sees"
304
+
._box_1p56a_15 {
305
+
view-transition-class: my-box;
306
+
}
307
+
::view-transition-group(._my-box_1p56a_15) {
308
+
animation-duration: 0.5s;
309
+
}
310
+
```
311
+
Note how scoping changed the view transition class name in the second selector. We now have an unused view transition class named `my-box` and an undefine view transition class named `_my-box_1p56a_15`.
312
+
313
+
Even when your CSS Module implementation might transform the original CSS into…
314
+
```css title="What the browser sees"
315
+
._box_1p56a_15 {
316
+
view-transition-class: _my-box_1p56a_15;
317
+
}
318
+
::view-transition-group(._my-box_1p56a_15) {
319
+
animation-duration: 0.5s;
320
+
}
321
+
```
322
+
…this is not much better, as it would not allow you to use the same view transition class in different modules. And it will no work at all if you have the definition of the view transition class and its use in different CSS modules.
323
+
324
+
The best way here is to stop scoping with the `:global()` pseudo-class like this
325
+
```css title="style.module.css"
326
+
::view-transition-group(:global(.my-box)) {...}
327
+
```
328
+
After all, view-transition-names are global, aren't they? <small>_(…skimming through the spec…)_</small>
0 commit comments