From 8b5f6eb38afdb5db9ddfc32fa11c2f7bffff686f Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:36:01 +0200 Subject: [PATCH] updates on generated view transition names --- src/components/TestPage.astro | 76 ++++++++++++--------------- src/content/docs/basics/levels.mdx | 2 +- src/content/docs/basics/pseudos.mdx | 65 +++++++++++++++++------ src/content/docs/basics/styling.mdx | 5 ++ src/content/docs/basics/test-page.mdx | 17 +++--- src/content/docs/fwvt/playing.mdx | 2 +- src/content/docs/tips/auto-names.mdx | 40 +++++++------- src/styles/sidebar.css | 8 ++- 8 files changed, 123 insertions(+), 92 deletions(-) diff --git a/src/components/TestPage.astro b/src/components/TestPage.astro index d744823..820d6ae 100644 --- a/src/components/TestPage.astro +++ b/src/components/TestPage.astro @@ -32,24 +32,7 @@ return { name: ua, version: "" }; } - const activation = (act) => { - copy = {}; - copy.from = {}; - copy.from.url = act?.from?.url; - copy.from.index = act?.from?.index; - copy.entry = {}; - copy.entry.url = act?.entry?.url; - copy.entry.index = act?.entry?.index; - copy.navigationType = act?.navigationType; - return copy; - }; addEventListener("pagereveal", (e) => { - const d = {}; - d.viewTransition = e?.viewTransition; - d.activation = activation( - "navigation" in window ? window.navigation?.activation : undefined - ); - sessionStorage.setItem("vtbag-pagereveal", JSON.stringify(d)); e.viewTransition?.ready.then(() => { const as = document.getAnimations(); as.forEach((a) => { @@ -60,43 +43,42 @@ }); addEventListener("pageswap", (e) => { - const d = {}; - d.viewTransition = e?.viewTransition; - d.activation = activation(e?.activation); - sessionStorage.setItem("vtbag-pageswap", JSON.stringify(d)); + sessionStorage.removeItem("vtbag-pageswap-idx"); + (e.activation?.entry?.index ?? -1 !== -1) && + sessionStorage.setItem("vtbag-pageswap-idx", "x"); }); addEventListener("load", () => { - setTimeout(() => { - if (!new URL(location, location.href).searchParams.has("pagereveal")) { - location.href = "/basics/test-page/?pagereveal"; + setTimeout(async () => { + if (!sessionStorage.getItem("vtbag-test-page")) { + sessionStorage.setItem("vtbag-test-page", "x"); + location.href = "/basics/test-page/"; } else { - const reveal = JSON.parse(sessionStorage.getItem("vtbag-pagereveal")); - const swap = JSON.parse(sessionStorage.getItem("vtbag-pageswap")); - reveal && document.documentElement.setAttribute("cdvt", ""); - reveal?.viewTransition && - document.documentElement.setAttribute("prvt", ""); - reveal?.activation && - document.documentElement.setAttribute("ract", ""); - swap?.viewTransition && - document.documentElement.setAttribute("psvt", ""); - swap?.activation && document.documentElement.setAttribute("sact", ""); - ((swap?.activation?.entry?.index ?? -1) !== -1 || - (swap?.activation?.from?.index ?? -1) !== -1) && - document.documentElement.setAttribute("idx", ""); - "startViewTransition" in document && + sessionStorage.removeItem("vtbag-test-page"); + if (document.startViewTransition) { + let matchElement = true; + await document.startViewTransition().ready; + document.getAnimations().forEach((a) => { + if (a.animationName === "no-match-element") matchElement = false; + }); document.documentElement.setAttribute("sdvt", ""); - "navigation" in window && - document.documentElement.setAttribute("nav", ""); + matchElement && document.documentElement.setAttribute("vtnme", ""); + } + "onpageswap" in window && + document.documentElement.setAttribute("cdvt", ""); + sessionStorage.getItem("vtbag-pageswap-idx") && + document.documentElement.setAttribute("idx", ""); CSS.supports("view-transition-class", "mine") && document.documentElement.setAttribute("vtc", ""); - CSS.supports("view-transition-name", "auto") && - document.documentElement.setAttribute("vtna", ""); CSS.supports("view-transition-group", "g") && document.documentElement.setAttribute("vtg", ""); document.documentElement.getAttribute("_vtgc") && document.documentElement.setAttribute("vtgc", ""); + document.documentElement.getAttribute("_vtnme") && + document.documentElement.setAttribute("vtnme", ""); document.body.startViewTransition && document.documentElement.setAttribute("svt", ""); + "navigation" in window && + document.documentElement.setAttribute("nav", ""); "prerendering" in document && document.documentElement.setAttribute("spec", ""); @@ -114,12 +96,20 @@ diff --git a/src/content/docs/basics/levels.mdx b/src/content/docs/basics/levels.mdx index 70f0eee..d8c1143 100644 --- a/src/content/docs/basics/levels.mdx +++ b/src/content/docs/basics/levels.mdx @@ -44,7 +44,7 @@ Part two indeed adds extensions for cross-document view transitions, but also fe ### Extensions for Both, Same- and Cross-Document * The [`view-transition-class`](/basics/styling/#with-classes) property and selector syntax to address multiple pseudo-elements with a single selector. * [View transition types](/basics/styling/#with-types) being part of the ViewTransition object, the `startViewTransition()` signature, and the `@view-transition{ }` at-rule; as well as matching pseudo-class selector `:active-view-transition-type()` and the type-less `:active-view-transition`. -* [Generated view transition names](/basics/pseudos/#auto-generated-names-auto-and-match-element) with `view-transition-name: auto` and `view-transition-name: match-element`. +* [Generated view transition names](/basics/pseudos/#auto-generated-names-match-element) with `view-transition-name: auto` and `view-transition-name: match-element`. * [Nested view transition groups](/basics/pseudos/#nested-view-transition-groups) with the `::view-transition-group-children()` pseudo element. ## Future extensions diff --git a/src/content/docs/basics/pseudos.mdx b/src/content/docs/basics/pseudos.mdx index 43e9b10..3529027 100644 --- a/src/content/docs/basics/pseudos.mdx +++ b/src/content/docs/basics/pseudos.mdx @@ -167,7 +167,7 @@ At the start of the view transition, the **view transition names have to be uniq There are two exceptions: - The API ignores the `view-transition-name` of a fragmented element, i.e. an element that consists of two or more boxes, like an inline element that spans two lines. -- The API ignores the `view-transition-name` of an element if that element is not rendered, like the ``, or if it is skipped. For instance, you can use the same `view-transition-name` multiple times within the same DOM, provided that all but one instance have their `display` CSS property set to `none`. Looking for an example? This technique is utilized in the intra-document [image morph examples](/basics/examples/#image-morph-examples) on this site. This trick also works with an ancestor with `content-visibility: hidden` but not with a simple `visibility: hidden` directly on the element. +- The API ignores the `view-transition-name` of an element if that element is not rendered, like the ``, or if it is skipped. For instance, you can use the same `view-transition-name` multiple times within the same DOM, provided that all but one instance have their `display` CSS property set to `none`. This can also be done by setting an HTML element's hidden attribute or property. Looking for an example? This technique is utilized in the intra-document [image morph examples](/basics/examples/#image-morph-examples) on this site. This trick also works with an ancestor with `content-visibility: hidden` but not with a simple `visibility: hidden` directly on the element. The static user agent stylesheet provides one default name for the document, called `root`. @@ -183,6 +183,9 @@ This default defines an animation for the current viewport. If you want to anima view-transition-name: none; } ``` +The special value `none` removes a potentially set view transition name. + +Other special names are [`match-element`](#auto-generated-names-match-element) and `auto`, and all names beginning with `-ua-`. ### Escaping Do not quote the names. They are not strings but [custom-ident](https://developer.mozilla.org/de/docs/Web/CSS/custom-ident) values. Ensure proper escaping of special characters. The simplest, most portable approach is to just use upper and lower case letters from A to Z, digits from 0 to 9, an underscore `_` or a hyphen `-` character. But don't start the name with a digit. While all other characters with a code < 128 need to be escaped, codes > 127 like 😄 seem to work just fine in view transition names. In browsers, you can use `CSS.escape()` to properly escape strings as custom identifiers. @@ -190,30 +193,63 @@ Do not quote the names. They are not strings but [custom-ident](https://develope It is not important to have much imagination when choosing names. They can be the similar to the `id` of an element or name of a unique tag like `body` or `footer`. -### Auto-Generated Names: auto and match-element -The opposite problem to view transition names not being unique is that elements need to have _any_ `view-transition-name` assigned in order to trigger a group or morph animation. Often, the actual value doesn't matter. What's important is simply having a value defined. For large numbers of elements, doing this manually can be cumbersome. You can automate it with JavaScript, as with the headings on this site or in [this demo](/tips/auto-names/). However, it is also possible to do this with only CSS by using the `auto` value, no JavaScript needed. +### Auto-Generated Names: match-element +The opposite problem to view transition names not being unique is that elements need to have _any_ `view-transition-name` assigned in order to trigger a group or morph animation. Often, the actual value doesn't matter. What's important is simply having a value defined as it acts as a marker for elements for which the API should generate view transition groups. For large numbers of elements, doing this manually can be cumbersome. You can automate it with JavaScript, as with the headings on this site or in [this demo](/tips/auto-names/). + +However, it is also possible to do this with only CSS by using the `match-element` value, no JavaScript needed. ```css -.tile { - view-transition-name: auto; -} .card { view-transition-name: match-element; } ``` -[Check if your current browser already supports it](/basics/test-page/). - -Both, `auto` and `match-element`, assign a generated name that is bound to the DOM element. -Those names typically look like `-ua-auto-....`. +Using `match-element` assigns a generated name that is bound to the DOM element. +Those names typically look like `-ua-....` with some random suffix. Two different elements are always assigned different values. When you set the view transition name multiple times for the same element that way, it will always get the same name. -Safari, first checks for an existing `id` attribute, and instead copies its value if it exists. Chromium browsers don't. +How could you style your pseudo-elements, if they have an unknown random name? The general approach here is to also assign a view transition class. +```css +.card { + view-transition-name: match-element; + view-transition-class: card; +} +``` +The class name does not need to be unique across elements. Then [use the view transition class name](/basics/styling/#with-classes) in your style rules. + +This works fine for **same-document** view transitions on supporting browsers. +For **cross-document** view transitions, generated names for the two document should never be the same. Therefore groups created from `match-element` in cross-document view transitions wi ll always have image-pairs with a single old or new image, only. + +:::caution +That is, if they are created. Safari seems to handle `match-element` like `none` in cross-document view transitions for elements that do not define an `id`. +::: + +Until it gets broader, convergent support in browsers, **I would not recommend `match-element`** for cross-document view transitions. There are [alternatives](/tips/auto-names/#how-to-assign-names-automatically) that work cross-browser and also for cross-document navigation. + +### Auto-Generated Names: auto + +_`view-transition-name: auto` is a bit of a mess right now. Use with caution._ + +Browser also defined `auto` as a special value for `view-transition-name`, but its semantics are not consistent across browsers and its specification is work in progress. Do not use it (yet). + +```css +selector { + view-transition-name: auto; +} +``` +This is similar to `match-element` but it also takes the `id` property of an HTML element into account: If the `id` property is set, `auto` generates the same view transition name for all elements with that `id`, independent of the document that element belongs to. +Different ids generate different view transition names. The generated name has to start with `-ua-`. + +Different browsers and browser versions show divergent behavior. Safari used to directly use the `id` value as view transition name. Current versions use `-ua-id-`, where `` is the element's `id` value. Chrome uses random values similar to those generated for `match-element`. + +:::caution +Like with `match-element`, Safari seem to not generate random names for cross-document view transitions enabled with `@view-transition{navigation: auto}`. Only for view transitions started by `startViewTransition()`. +::: -With cross-document view transitions, auto-generated names of the two documents will never be the same, [not even when `id` attributes would match](https://drafts.csswg.org/css-view-transitions-2/#additions-to-vt-name). +### Using id as View Transition Name -`match-element` will always assign a random name, no matter wether the `id` attribute is set or not. On Chromium browsers, `auto` and `match-element` seem to be the same thing. +_Not available across browsers, yet._ On Chromium browsers you would use the following incantation to use the id attribute as an view transition name: ```css @@ -224,10 +260,9 @@ Here `type()` is necessary to make the id a custom-ident, and `non Fun fact: The following works in Chrome like the original Safari version of `auto`: Use the `id` attribute if defined, otherwise make up some name. ```css -view-transition-name: attr(id type(), auto); +view-transition-name: attr(id type(), match-element); ``` -I would not recommend `auto` for cross-document view transitions. There are [alternatives](/tips/auto-names/#how-to-assign-names-automatically) that work cross-browser and also for cross-document navigation. diff --git a/src/content/docs/basics/styling.mdx b/src/content/docs/basics/styling.mdx index 03dd5c3..aade2db 100644 --- a/src/content/docs/basics/styling.mdx +++ b/src/content/docs/basics/styling.mdx @@ -139,6 +139,11 @@ The typical cross-fade effect of view transitions is implemented by animations d animation-duration: inherit; animation-delay: inherit; animation-fill-mode: inherit; + /* newly added */ + animation-timing-function: inherit; + animation-iteration-count: inherit; + animation-direction: inherit; + animation-play-state: inherit; } ::view-transition-old(x) { animation-name: -ua-view-transition-fade-out, -ua-mix-blend-mode-plus-lighter diff --git a/src/content/docs/basics/test-page.mdx b/src/content/docs/basics/test-page.mdx index d1a6be0..d15d34b 100644 --- a/src/content/docs/basics/test-page.mdx +++ b/src/content/docs/basics/test-page.mdx @@ -18,17 +18,18 @@ This page shows whether your current browser has nativ | ------------------------------------------------------------------------------------------------------------ | ----------------- | | [Same-document view transitions](https://drafts.csswg.org/css-view-transitions-1) | | | [Cross-document view transitions](https://drafts.csswg.org/css-view-transitions-2) | | -| [Navigation API](https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API) | | | [PageSwapEvent.activation.\*.index](https://drafts.csswg.org/css-view-transitions-2/#old-doc-event) | | -| [view-transition-class](https://drafts.csswg.org/css-view-transitions-2/#view-transition-class-prop) | | -| [view-transition-name: auto](https://drafts.csswg.org/css-view-transitions-2/#additions-to-vt-name) | | +| [view-transition-class](https://drafts.csswg.org/css-view-transitions-2/#view-transition-class-prop) | | +| [view-transition-name: match-element](https://drafts.csswg.org/css-view-transitions-2/#additions-to-vt-name) | | | [Nested view transition groups](https://drafts.csswg.org/css-view-transitions-2/#view-transition-group-prop) | | | [View transition group children](https://drafts.csswg.org/css-view-transitions-2/#view-transition-group-children-pseudo) | | | [Scoped view transitions](https://github.com/WICG/view-transitions/blob/main/scoped-transitions.md) | | +| [Navigation API](https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API) | | | [Speculation Rules](https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API) | | -
+
+
In current Chrome versions (>= 134), `nested view transition groups` and `view-transition-name: auto` are available when you enable "Experimental Web Platform features" on `chrome://flags`. `scoped view transitions` are supported in versions >= 136 (canary). @@ -40,9 +41,7 @@ For browser compatibility data regarding the View Transition API see [the mdn pa #sdvt, #cdvt, #nav, - #psvt, - #prvt, - #vtna, + #vtnme, #vtc, #vtg, #vtgc, @@ -57,12 +56,10 @@ For browser compatibility data regarding the View Transition API see [the mdn pa :root[sdvt] #sdvt, :root[cdvt] #cdvt, :root[nav] #nav, - :root[svt] #psvt, - :root[rvt] #prvt, :root[vtc] #vtc, :root[vtg] #vtg, :root[vtgc] #vtgc, - :root[vtna] #vtna, + :root[vtnme] #vtnme, :root[idx] #idx, :root[svt] #svt, :root[spec] #spec diff --git a/src/content/docs/fwvt/playing.mdx b/src/content/docs/fwvt/playing.mdx index 7749175..0a27e78 100644 --- a/src/content/docs/fwvt/playing.mdx +++ b/src/content/docs/fwvt/playing.mdx @@ -571,7 +571,7 @@ If the active view transition type caught your interest, this way of guarding us If you wondered why this demo doesn't use `view-transition-name: auto` or `view-transition-name: match-element`, here is the answer: -[Auto-generated](/basics/pseudos/#auto-generated-names-auto-and-match-element) view transition names are fine if you do not need to address individual elements. You can still address _all_ elements using view transition classes. But because the auto-assigned name is the APIs little secret, you do not know it and thus can't use it to address a particular element. +[Auto-generated](/basics/pseudos/#auto-generated-names-match-element) view transition names are fine if you do not need to address individual elements. You can still address _all_ elements using view transition classes. But because the auto-assigned name is the APIs little secret, you do not know it and thus can't use it to address a particular element. For staggered animations we assign individual delays to the disks and for this we need to know the name of the pseudo-element. If you were counting on `sibling-index()` to make things easier in combination with the `.disk` view transition class, sorry to disappoint: tree-counting functions only work on real DOM elements, not on pseudo-elements. diff --git a/src/content/docs/tips/auto-names.mdx b/src/content/docs/tips/auto-names.mdx index 744aa79..2fa3650 100644 --- a/src/content/docs/tips/auto-names.mdx +++ b/src/content/docs/tips/auto-names.mdx @@ -41,47 +41,47 @@ In reality, you don't want to select each element individually. Instead, you wan ## How to Assign Names Automatically? Here are your options right now: -* For same-document view transitions, only: generate [dynamic identifiers](/basics/pseudos/#auto-generated-names-auto-and-match-element) with `match-element` (or `auto`): \ -`selector { view-transition-name: match-element; } /* or auto */` -* _Not yet baseline: Copy an existing unique attribute like `id`: \ -`selector { view-transition-name: attr(id type()); }` +* Currently only feasible for same-document view transitions: generate dynamic identifiers with [match-element](/basics/pseudos/#auto-generated-names-match-element) + +* In rare cases also for cross-document view transitions: generate dynamic identifiers with [auto](/basics/pseudos/#auto-generated-names-auto) for elements with `id` properties. + +* [Copy an existing unique attribute](/basics/pseudos/#auto-generated-names-match-element) like `id` * In JavaScript, do something like \ `querySelectorAll("selector").forEach(e => e.style.viewTransitionName = ...)` -* Use a script like [`declarative-names`](/tools/utensil-drawer/#declarative-names) script \ +* Use a script like the [`declarative-names`](/tools/utensil-drawer/#declarative-names) script \ `