Skip to content

Commit 89ac8d5

Browse files
committed
Add demo for hidden view transition names and clarify usage in CSS
1 parent b64e42d commit 89ac8d5

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/content/docs/basics/pseudos.mdx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,14 @@ On the HTML side, you can hide elements by adding the `hidden` attribute or by p
184184
|closed `<details>` element|Ignores view transition names inside the `<details>` element as long as they are not part of the first `<summary>` child|
185185

186186
<style>{`
187-
td:nth-child(2) {
187+
#hiding-propertiestd td:nth-child(2) {
188188
white-space: nowrap;
189189
}
190190
`}</style>
191191

192192
You can also use CSS properties to hide view transition names:
193193

194+
<div id="hiding-properties">
194195
|CSS Property|Value|Effect|
195196
|-|-|-|
196197
|`display`|`none`|Ignores view transition names of the subtree, including its root|
@@ -199,15 +200,30 @@ You can also use CSS properties to hide view transition names:
199200
|`view-transition-scope`|`all`|Hides view transition names of the subtree from the element's ancestors|
200201
|`visibility`|`hidden`|<span style="color: red;">Has no effect on hiding view-transition names</span>|
201202
|`visibility`|`collapse`|<span style="color: orange;">Hides names where it completely removes elements, but shows inconsistent behaviour between browsers</span>|
203+
</div>
202204

203205

204206
Here, `view-transition-scope` is a relatively new CSS property that is currently supported only in Chrome. With value `all`, it hides the view transition names of its subtree without changing how the elements are rendered.
205207
It hides the names from view transitions rooted at an ancestor of the scoped element, but not from a view transition rooted at the scoped element itself or its descendants.
206208

207209
With these mechanisms for hiding view transition names, you can use the same `view-transition-name` multiple times within the same DOM, provided that all but one instance are hidden.
208210

209-
Looking for an example? This technique is used in the same-document [image morph examples](/demo/BasicS/) on this site, where one element has `display: none` and the other has `display: block`.
210211

212+
#### A Demo with Hidden Names
213+
214+
The following demo shows how hidden view transition names work in practice. Click on the map to switch between two view states.
215+
216+
We want the small map to morph into the larger version. So we assign both `<img>` elements a view transition name of "map". Normally, having two elements with the same view transition name on a page would throw an error and prevent the view transition from working. Here the trick is that the maps of the two view states are disjoint. The first state only has the small map, while the second state only has the large map. We call `startViewTransition()` to switch between the states.
217+
218+
<iframe width="100%" style="border: none; height: 500px;" src="/basics/pseudos-hidden-names-example/"
219+
></iframe>
220+
221+
Whenever the View Transition API captures images, there is only one map rendered, either the small or the big one. And as the API ignores view transition names of elements not being rendered, there is always only one definition of the "map" view transition name, and there are no naming conflicts. The code for this demo can be found [here](https://github.com/vtbag/website/blob/main/src/pages/basics/pseudos-hidden-names-example.astro).
222+
223+
Setting view transition names statically works well for many use cases.
224+
Knowing that you can indeed use the same name multiple times on the same page, as long as only one of those elements is rendered at a time, greatly simplifies shared image view transitions because you can control them with CSS alone, without needing JavaScript to assign unique names to elements.
225+
226+
On a side note: For more complex scenarios, it is good to know that you can also utilize _view transition types_ to [assign view transition names dynamically](/tips/css/#scoping-with-types).
211227

212228
### Special Names
213229
The static user agent stylesheet provides one default name for the document, called `root`.

0 commit comments

Comments
 (0)