-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Integration with Turbo #63
Comments
The only thing I can think of would be adding and removing a Probably all of the components would need this, since you probably want to reset all of them before-cache. |
Found this on Turbolinks: turbolinks/turbolinks#390 I would assume Turbo is supposed to work similarly. Maybe something is wrong with this functionality. |
Looks like it's an open issue with Turbo: hotwired/turbo#117 PR with a fix is in |
Thank you for the information. I’ll test against Turbo’s main branch
tomorrow and I’ll post an update back here.
|
I've tried both hotwired/turbo#169 and hotwired/turbo#162, neither seem to fix the issue described above. The page seems to be cached prior to the For now, I'll stick with the
|
It's probably a combination of the Turbo disconnect at the right time, and we need to make sure we close dropdowns, etc on disconnect (which I don't believe we do right now). |
From looking at turbolinks/turbolinks#390 which you linked to above, it looks like Turbolinks was updated to defer the snapshot caching until after the stimulus disconnect. Turbo does not seem to be working in the same way.
I have made this change locally, but as mentioned above, it's not quite working as expected with Turbo. I'll switch back to a Turbolinks setup and see what happens there, and depending on the outcome, I'll open a PR with the changes. |
Having thought about this some more, I think the issue can be resolved in Turbo is doing the correct thing by snapshot'ing the state of the page upon departure, so we need to get the page into the correct state when a user is leaving the page via clicking a link in the dropdown menu, e.g. upon selecting a link in the dropdown, the dropdown should be closed. This appears to happen as a new page is rendered, however, the dropdown is actually still open. Solution 1: The user is responsible for adding Stimulus markup on the dropdown items: e.g: <div data-controller="dropdown" aria-haspopup="true" aria-expanded="false">
<div aria-haspopup="true" aria-expanded="false">
<button data-action="click->dropdown#toggle click@window->dropdown#hide">
<span>Button label</span>
</button>
</div>
<div data-dropdown-target="menu">
<div>
<div role="menu" aria-orientation="vertical">
<a role="menuitem" data-action="click->dropdown#toggle" href="#">Option 1</a>
<a role="menuitem" data-action="click->dropdown#toggle" href="#">Option 2</a>
<a role="menuitem" data-action="click->dropdown#toggle" href="#">Option 3</a>
</div>
</div>
</div>
</div> Solution 2: The Stimulus if (this.hasMenuTarget) {
$(this.menuTarget).on("turbo:click", (event) => {
this.hide()
});
} Both of these solutions work as expected when using Turbo. If you let me know your thoughts I can look to get a PR opened. |
Updating the README to outline how the dropdown can be closed upon clicking on a item in the dropdown menu. Required for Turbo previews to displayed as expected as outlined in excid3#63 (comment)
Updating the README to outline how the dropdown can be closed upon clicking on an item in the dropdown menu. Required for Turbo previews to displayed as expected as outlined in excid3#63 (comment)
Hi @davegudge i was about to close the issue on Turbo's repository but i found out about your issues. I have some questions/notes:
This is actually very spot on. My concern is that while using Turbolinks disconnect method was used in order to have a single and final spot where your controller would shape page's state, before it was cached. Before Turbo-v.7.0.0.beta4 it was definitely not the case for me.. But after this release my experience has improved vastly regarding this issue. From your answers i have come to conclusion that the issue still persists even while using the main branch of Turbo. If my assumption is correct, i should keep my issue open at Turbo's repository.. if not i am gonna close it. Thanks in advance. |
Hi @thanosbellos, For the issue I have outlined in this ticket, I came to the conclusion that the fix should be part of Another way to think about this without involving Turbo. If one of the links in the dropdown was used to call a JavaScript function (e.g toggle light/dark mode) without navigating the user away from the page, upon click, I would expect the dropdown menu to close. This would not happen at the moment. As a consequence of the dropdown being left open, this is the state which is captured by Turbo upon snapshoting as outlined in the issue above. By However, all that said, I was expecting to be able to add So I'm pretty certain that Turbo is working slightly different to Turbolinks. If you look add the diagrams added on turbolinks/turbolinks#390 Turbolinks was updated to ensure the snapshot was saved to the cache after the controller's disconnect method has been called (I haven't tested the issue outlined on this thread using Turbolinks). I found that this was not happening in Turbo(v.7.0.0.beta4) or in the pull requests mentioned in (hotwired/turbo#117). However, it's worth noting that my dropdown was using the animations as outlined at https://github.com/excid3/tailwindcss-stimulus-components#dropdowns e.g:
So perhaps Turbo is calling |
Thank you very much for your time and for your detailed response.
Definitely on board with this approach.. It's really logical to handle both the cases you described at will. i.e someone may want to find the dropdown open after pressing back button.
This is what caught my attention. That, regardless the desired intention and how it could be achieved by the library:
If you have the time i would be really grateful if you tried that scenario(no animations). I will proceed and test it using turbolinks. |
Adding the following to connect() {
console.log("DropdownController#connect");
...
disconnect() {
console.log("DropdownController#disconnect"); and the following to my own project: $(document).on('turbo:click', function() {
return console.log('turbo:click');
});
$(document).on('turbo:before-visit', function() {
return console.log('turbo:before-visit');
});
$(document).on('turbo:visit', function() {
return console.log('turbo:visit');
});
$(document).on('turbo:submit-start', function() {
return console.log('turbo:submit-start');
});
$(document).on('turbo:before-fetch-request', function() {
return console.log('turbo:before-fetch-request');
});
$(document).on('turbo:before-fetch-response', function() {
return console.log('turbo:before-fetch-response');
});
$(document).on('turbo:submit-end', function() {
return console.log('turbo:submit-end');
});
$(document).on('turbo:before-cache', function() {
return console.log('turbo:before-cache');
});
$(document).on('turbo:before-render', function() {
return console.log('turbo:before-render');
});
$(document).on('turbo:before-stream-render', function() {
return console.log('turbo:before-stream-render');
});
$(document).on('turbo:render', function() {
return console.log('turbo:render');
});
$(document).on('turbo:load', function() {
return console.log('turbo:load');
}); Using Initial page load:
Click a link in the dropdown (or simply navigate to another page)
The |
Yes, my expectation was that any manipulation via the controller disconnect method would be applied before the page was cached. That expectation was based upon turbolinks/turbolinks#390 (comment) |
Would you like me to test against Turbo's main branch? Did you find an easy way to do this? I have both |
Yeah i use turbo-rails too. i didn't dig up more about a better way to test it out.. that's how i test it too. i just created a new project to test it out with turbolinks. If you have the time and you are in the mood you can proceed with your tests 😄 But you have helped a lot so don't feel obliged to do so :) I will report my finding back soon (i guess) |
That said, in the 'after' diagram on turbolinks/turbolinks#390,
So the log output above is as expected I guess! |
Exactly. I guess that their goal is to maintain the same behaviour in Turbo too.. It makes perfect sense to have your controller's disconnect method, to reset external libraries(i.e uppy file uploader, lightbox, flatpickr). What seems to be happening is that sometimes, if you manipulate the dom inside the disconnect method, the snapshot cache does not contain the changes. All of my custom controllers suffered from the issue before v.7.0.0.beta4. I only have one controller now that still has the same issue.. although it didn't work well with turbolinks neither.. that's why i was about to close the issue and then i saw your use case. |
I've removed Turbo (and Turbo-rails) and switched the project back to In summary, it seems like Turbo is working the same as Turbolinks, but it is not how I was expecting it to work based upon turbolinks/turbolinks#390
For completeness, here's the output using Turbolinks: Initial page load:
Click a link in the dropdown (or simply navigate to another page)
|
Yeap tested it on new project and it seems to behave like what you have described on both occasions. Even without animations i see the menu closing rapidly after restoring from the cache. But it does close something that is crucial in general. As long as it has the same behaviour, i am gonna close it for now and maybe we start a new issue about whether the "expected" functionality could be achieved. Thanks a lot for your time and contribution. |
Just to confirm, this doesn't happen when using Once
|
Exactly. those were my findings using the modified version that calls hide when the controller disconnects and for either library(turbolinks and turbo) |
I will close this issue as I have opened #65 which resolves the problem outlined above. |
Updating the README to outline how the dropdown can be closed upon clicking on an item in the dropdown menu. Required for Turbo previews to displayed as expected as outlined in #63 (comment)
Hi Chris,
First of all, great work on this project 👍🏼
I am looking into fixing an issue I've found whist using the dropdown component in a Turbo/Rails setup.
The dropdown is working as expected, the issue occurs when navigating back to the screen where the dropdown link was clicked (via the back button), the dropdown remains open:
dropdown.mov
Adding
data-turbo="false"
to the dropdown options solves the issue, so it's definitely a turbo related problem.I was hoping that by adding a
this.hide()
call, as part of thedisconnect()
method, would achieve the desired solution:However, the hide does not occur until the page is redisplayed:
dropdown2.mov
As a workaround, I've added a
turbo:before-cache
to hide the dropdown e.g:If you have any ideas on how tailwindcss-stimulus-components could be modified to accommodate this issue, I'm happy to implement the change and open a PR. Perhaps it falls outside the scope of tailwindcss-stimulus-components, and using the
turbo:before-cache
is the best solution?Thanks
The text was updated successfully, but these errors were encountered: