-
Notifications
You must be signed in to change notification settings - Fork 436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected behavior when morphing a turbo frame src #1158
Comments
@danecando Did you try to try to add the attribute refresh="morph" on your turbo-frame ? I encountered the same issue and this attribute did the trick. It avoids the reset of the src and the request. I don't know if using this attribute has any drawback though... EDIT: it does not do the trick... Modal does not pop up BUT the request is fired... |
@nflorentin I don't think that it what I want. I'm expecting the frame src to be unset during the morph. Here, since the new page is morphed the frame src should be empty (that is its default state) The confusing part is that it applies that operation but something resets it back right after |
@danecando you are right, I misunderstood. Meanwhile, I removed the modal manually with turbo stream :/ |
@danecando we are seeing the same thing with the same modal pattern, and came to the same conclusion as you. |
I'll need to do some debugging, but this might be by intention from reviewing the source. |
I was curious to figure out exactly why this is going on so I ran your app locally and went in with the web tools debugger. It's a little "fun" dance that happens and I'm pretty sure it's a bug. I'll explain in detail how it happens but first a small remark. diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 0cb46c4..511fc52 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -27,7 +27,7 @@ class PostsController < ApplicationController
def update
if @post.update(post_params)
- redirect_back_or_to @post
+ redirect_to posts_path
else
render :edit
end
@@ -42,4 +42,4 @@ class PostsController < ApplicationController
def post_params
params.require(:post).permit(:title, :body)
end
-end
\ No newline at end of file
+end Ok, so with that out of the way, let me explain what's going on:
<turbo-frame data-modal-target="frame" id="turbo-frame-modal" target="_top" src="http://localhost:3000/posts/1/edit" complete="">
Given how it's a bunch of different unrelated mechanisms interacting in a strange way, I am pretty sure it's a bug. Especially considering that the behaviour would be completely different if idiomorph didn't do this small internal optimisation of removing attributes starting from the back. The most likely correct fix seems to be postponing all "attribute changed" callbacks until idiomorph is finished. This would mean that by the time they fire they would see the real final state, instead of some random intermediate state that idiomorph is going through. But that introduces some extra complexity. Maybe it's worth thinking a bit about how to reduce the number of hidden assumptions that the turbo frame lifecycle mechanism (as encoded in FrameController) makes? |
this strangely happens to me too, idk how could this possible, because when I see it on network tab of inspect elements its make a strange request to show the edit page again, maybe there is a turbo_stream response thus the behavior I will check more about it, after my mom finished cleaning my room forcefully, can't help, she have cleaning ocd I think this one is related https://www.reddit.com/r/rails/comments/1as69jv/do_i_need_to_use_turboactionreplace_for_morphing/?rdt=44534 |
@danecando - did you manage to find a workaround (other than using |
This is a good catch! I agree that it seems like a bug, that's triggered by a bunch of unrelated mechanisms and the way Idiomorph does it's thing. A potential workaround for now could be something like: addEventListener("turbo:before-morph-attribute", (event) => {
const { target, detail } = event
if (target.tagName === "TURBO-FRAME" && detail.attributeName === "complete" && target.hasAttribute("src")) {
event.preventDefault()
return false;
}
}) .. which would prevent the removal of the |
Have you guys updated to the latest version? I believe it's fixed now. |
Awesome! I can confirm that it's working with the latest release. |
In my application I have a modal that gets triggered to open when a turbo frame inside of it gets loaded (pretty common pattern). The main use cases for my modal are
new
andedit
pages. Before morphing on anedit
for example, I would do a redirect from theupdate
controller on success and the replace operation would remove the modal since the content from the redirect has it's default state with nothing loaded in the turbo frame.With morphing enabled, the morph operation gets applied to the modals turbo frame to remove the src and everything seems dandy but immediately afterward the src attribute gets reset to the last value and the modal pops back open. I don't see any turbo event correlated with the second mutation of the src attribute. I've spent a bit of time trying to trace it with no luck.
I've included a quick screen recording to highlight the issue and a repo with a minimal reproduction of the issue with a simple stimulus modal.
https://github.com/danecando/turbo-frame-morph
Screen.Recording.2024-02-04.at.9.31.13.AM.mp4
Here's the same thing with a replace operation instead of a morph
Screen.Recording.2024-02-04.at.10.07.09.AM.mp4
The text was updated successfully, but these errors were encountered: