spicychickensauce

spicychickensauce

View Transition API just landed in Firefox, which means it’s soon fully ready to be used.

Unfortunately, I haven’t found a good way yet to make use of it with liveview.

There seems to be a hacky way available:

But this wraps way too much code inside the startViewTransition callback, not just the dom mutation. Because of that, There is a big delay between the navigation click and the end of the transition.

I have found that changing it to the code below is a big improvement in responsiveness, but of course that’s very flaky and doesn’t always work:

// Needs `@view-transition { navigation: auto; }` in CSS to work
window.addEventListener("phx:page-loading-start", (event: CustomEvent) => {
  if (event.detail?.kind === "initial") {
    if (!("startViewTransition" in document)) return;
    document.startViewTransition(() => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(true);
        }, 20);
      });
    });
  }

  topbar.show(500);
});

Has anyone found a more reliable way?
What would it take to get this officially supported in liveview?
How do other frameworks handle this?
Ideally the callback that wraps the DOM update should also provide enough context such that we could pass different types to the view transition to e.g. have different transitions for forward/backwards.

Also, ideally we should be able to trigger a view transition outside of just page navigation events, like in this example

Showing Posts 1 to 10

greven

greven

Hey, I’m the one that has put that piece of code I tried several things till landing on that, but didn’t get any feedback from anyone doing something similar. Anyway you say it wraps too much code?

It literally just returns the promise that resolves into a function, do you mean the Topbar hide?

I also tried using a setTimeout, but I noticed sometimes it did get stuck loading the page. So I removed it.

spicychickensauce

spicychickensauce OP

Yes, as in the amount of code that runs between phx:page-loading-start and phx:page-loading-stop is too much. The callback in startViewTransition should only be wrapped around the code that updates the DOM. With your code it fetches the page, calculates the diff, applies the diff and maybe more, not sure.

This leads to unresponsive navigation for me and adds significant delay.

My version is “worse” in that it wraps an unknown amount of code that might or might not wrap the actual dom update. But for me it is still “better”, since it never slows down navigation, it just doesn’t always actually trigger the transition. If I increase the timeout to 100ms I can already feel the slowdown, so I kept it very low.

greven

greven

Don’t know that much about startViewTransition will explore a bit more and dig into it. :slight_smile:

greven

greven

Yes, that was something I faced when using something similar to what you have. This code I posted isn’t yet pushed to prod, I’m still exploring the best way forward with MPA View Transitions and Phoenix, there isn’t much to lean into, we are the canary in the mine. :smiley:

spicychickensauce

spicychickensauce OP

Yeah, seems like only very few are interested in this. I think view transitions are a huge deal and will make the web platform much closer to native apps.

I found this blog post which has an interesting hack to do it “properly”.
I modified it slightly by adding type == "mount" such that it only transitions when navigating between pages:

  // deps/phoenix_live_view/priv/static/phoenix_live_view.esm.js
  applyDiff(type, rawDiff, callback) {
    this.log(type, () => ["", clone(rawDiff)]);
    const { diff, reply, events, title } = Rendered.extract(rawDiff);
    if (document.startViewTransition && type == "mount") {
      document.startViewTransition(() => callback({diff, reply, events}))
    } else {
      callback({diff, reply, events})
    }
    if (typeof title === "string" || type == "mount") {
      window.requestAnimationFrame(() => dom_default.putTitle(title));
    }
  }

Apart from the js asset patching hackery, this is pretty much perfect! No flakyness, no delays, perfectly smooth transitions!


Obviously this isn’t good enough to get into liveview.
A proper solution would have some way for users to opt in to the transition on changes.

Maybe the API could be something like:

# Calls startViewTransition on mount
<div phx-view-transition >...

# Results in: startViewTransition({ types: ["slide", "backwards"], update: ... })
<div phx-view-transition={["slide", "backwards"]} >...

This API would allow page transitions by using this in our layout component. But it would also allow same document view transitions by using it more granularly.
Only problem with this API would be that If multiple elements with this attribute are mounted in the same frame its not clear which one will “win”.

spicychickensauce

spicychickensauce OP

Actually, I think a better API might be to operate at the socket level, similar to push_navigate.
Because the API I proposed above wouldn’t work for triggering a view transition when deleting something from a list.

So maybe something like: start_view_transition(socket, types \\ [])
And there could be a corresponding JS.start_view_transition(js, types \\ []) which would be used together with JS.toggle() and similar for client side only dom mutations.

spicychickensauce

spicychickensauce OP

I have been playing around with a fork of liveview to implement a proper API for this.

Here’s a small video of a demo app with view transitions implemented with my fork: https://imgur.com/a/tGz06Eb
Still very rough, but it demonstrates that it works.

The API I implemented is: JS.start_view_transition and start_view_transition(socket).
types can be passed as an option.

I also needed to add a temp_name option, which allows implementing this common technique of giving an element a transition name just for the start of the view transition, like they do in this article: Same-document view transitions for single-page applications  |  View Transitions  |  Chrome for Developers.
Doing that looks like this:
JS.start_view_transition(temp_name: "img-full", to: "#element") |> JS.navigate("/card/#{@id}")

Once I’ve cleaned this up I’ll probably create a new post to propose this API and if deemed worthy enough I can open a PR.

rhcarvalho

rhcarvalho

I didn’t use view transitions in production yet.

Reading on MDN and thinking about how it applies to LiveView, I see we have both MPA-type transitions for disconnected mount and regular links / crossing live_session boundaries, and SPA-style transitions for live navigation, live patch, and other DOM updates.

What’s been your experience making transitions working consistently regardless of socket connected/disconnected state?

spicychickensauce

spicychickensauce OP

For dead/static render view transitions between pages can easily be done just with css. You only need:

@view-transition {
  navigation: auto;
}

See Cross-document view transitions for multi-page applications  |  View Transitions  |  Chrome for Developers

But when using live navigation or for same document transitions we need support in live_view, like I’m proposing in the other thread:

rhcarvalho

rhcarvalho

I saw the video of your demo! Do you need to take special care to avoid a double transition between live_session boundaries? Because that would be a full page load, IIUC triggering the MPA page transition, and then… where in the lifetime of the LiveView do you call the new/proposed API? I’ll go have a look at the demo code :slight_smile:

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
515 12931 101
New
wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
AstonJ
A remark @Garrison made… ..has already inspired a thread for the technical side of reclaiming our internet, but there is of course anot...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews