tom
March 20, 2023, 7:59am
1
Hi guys,
Something awesome dropped in Chrome 111 this month – View Transitions !
View transitions lets us create an animations, where an element on one page morphs into an element on another page at the click of a link.
Currently it only works in SPAs, but if you are using Turbo you are in luck!
To get started with this, I recommend you go read How to use View Transitions in Hotwire Turbo by Matouš Borák .
Personally I ended up using this JavaScript code:
addEventListener("turbo:before-render", (event: TurboBeforeRenderEvent) => {
if (document.startViewTransition) {
event.preventDefault();
document.startViewTransition(() => {
event.detail.resume();
});
}
});
And then just tagging elements that are the same on multiple pages using css :
#an-element {
viewTransitionName: header-image-element;
}
The bug
When navigating by history, Turbo uses the cache. For some reason JavaScript is re-applied when loading a page from cache (and using JavaScript-snippet above).
This goes for both my Stimulus-controllers and other JavaScript.
The temporary solution for the bug
The hack to fix the bug for now was to turn off the page cache on all pages.
<meta name="turbo-cache-control" content="no-cache">
2 Likes
I haven’t played with the page transitions features yet; but, I’ve read that Turbo Drive adds [data-turbo-preview]
to the document when a Preview is being rendered. Perhaps you could use that to disable the CSS selector, like:
html[ data-turbo-preview ] * {
view-transition-name: none ! important ;
}
That way, you could still have the caching in place, but disable the transitions… maybe.
tom
March 20, 2023, 9:46am
3
I don’t think it’s the css that is the problem. This issue is JavaScript-related.
I’m wondering of startViewTransition()
does something to the scope so that Turbo loses it’s context in some way.
Ahh, very possibly. I just thought maybe if the browser couldn’t find any elements to actually transition then maybe you’d be back in the clear.
tom
March 21, 2023, 9:17am
5
Here is an issue around this on Github:
opened 06:56PM - 12 May 22 UTC
With the advent of GoogleIO the [Shared Element Transition API](https://github.c… om/WICG/shared-element-transitions) has been exposed in Chrome Canary behind a couple flags. The power of this API, even in its infancy, can be seen [in this presentation](https://www.youtube.com/watch?v=JCJUPJ_zDQ4) and within [this demo](https://http203-playlist.netlify.app/videos/generating-your-color-palette-in-css/). TLDR: it allows for transitions between content within SPAs with a couple lines of JS and some CSS. Similar to the way that content transitions on native iOS and Android apps.
Figured I'd toss the idea out there of the SET API being something Turbo leverages to allow for transitions with frames and streams. Not sure on how that implementation would look or any of the specifics, but I wanted to get the ball rolling on this discussion, since the API seems super sweet, even in the infantile state it's in right now.
Not even sure if transitions between states of the DOM is something that has been on anyone's radar, it certainly wasn't on mine until after I saw this. Curious to see other people's thoughts on if this is something that could even work with Turbo in any capacity.