Skip to content

Commit 55dbc77

Browse files
committed
added CSS Module example
1 parent 61d10db commit 55dbc77

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

astro.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function sidebar() {
260260
{
261261
label: "Why View Transitions Might Fail",
262262
link: "tips/view-transition-fails-and-fixes/",
263-
badge: { text: "New!", variant: "success" } as Badge
263+
badge: { text: "Updated!", variant: "success" } as Badge
264264
},
265265
],
266266
},

src/content/docs/tips/view-transition-fails-and-fixes.mdx

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,12 @@ You have no chance to lift a normal DOM element high enough to obscure one of th
241241

242242
<details name="fail" class="c s">
243243
<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.
245245

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.
247250

248251

249252
Look at this example of a style element 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:
285288
}
286289
</style>
287290
```
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&hellip;
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+
&hellip;might be sent to the browser as&hellip;
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&hellip;
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+
&hellip;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>_(&hellip;skimming through the spec&hellip;)_</small>
329+
288330

289331
</details>
290332

0 commit comments

Comments
 (0)