-
Notifications
You must be signed in to change notification settings - Fork 750
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
Flowbite with Turbo on rails7 #88
Comments
Thanks for posting this. I tested your solution on a local version of flowbite.js and it indeed fixed the problem I was having with the js not working. |
thanks :) |
works like a charm 👍 |
Worked for me on rails7 project, thanks :) |
In order to make Flowbite work with Turbo, we have to patch it to listen for `turbo:load` rather than `DOMContentLoaded`.[^1] This commit adds the patch-package Node package and its `postinstall` Yarn script, along with a patchfile specific to Flowbite 1.4.3. Developer instructions for why we’re doing this and how to update the patchfile in the future have also been added to `DEVELOPMENT.md`. #182 [^1]: themesberg/flowbite#88
In order to make Flowbite work with Turbo, we have to patch it to listen for `turbo:load` rather than `DOMContentLoaded`.[^1] This commit adds the patch-package Node package and its `postinstall` Yarn script, along with a patchfile specific to Flowbite 1.4.3. Developer instructions for why we’re doing this and how to update the patchfile in the future have also been added to `DEVELOPMENT.md`. #182 [^1]: themesberg/flowbite#88
Wow thanks for posting this. Was just hooking up flowbite with rails 7 and hit this issue. Glad I stumbled on this. Fixed my issues. |
…ite#88 -- git apply flowbite.patch)
Also works with Turbolinks (5.2.0) with |
For those using Webpack, a solution that works is to replace DOMContentLoaded by turbo:load in the ./node_modules/flowbite/dist/flowbite.js file
|
this is old, but it would be easier if inside your own js, you trigger the DOMContentLoaded event inside the turbo:load event
|
Wouldn't it be even better if flowbite.js would have a // import "flowbite";
window.document.addEventListener('turbo:load', (event) => {
Flowbite.connect();
}); So no need to hack the flowbite.js and no need to trigger UPDATE: Or as an alternative: Provide an event that reconnects flowbite when triggered. like "document flowbite:reconnect" or something like that. |
Yeah, that's a nice solution, I was considering something similar, but couldn't get it to not fire "DOMContentLoaded" twice on the initial load of the page (when "DOMContentLoaded" and "turbo:load" both fire). Do you know a way to solve this? Will appreciate it! |
You can handle with Turbo Events with application.js as below.
flowbite.js can export init method for example, initDismiss, initCollapse etc..?
|
On 1.5.2 I've got the following:
I can see the Update I had to modify @TsubasaKawajiri's comment but got it working.
It works but it would be awfully nice to have a generic shim instead of copying init lines for every component. |
Also getting this with Phoenix LiveView, I assume for the same reasons. |
For LiveView the fix is similar: window.addEventListener('phx:page-loading-stop', (event) => {
// trigger flowbite events
window.document.dispatchEvent(new Event("DOMContentLoaded", {
bubbles: true,
cancelable: true
}));
}); |
Please help me, how to use Flowbite and Turbo Rails 7 with import map? I change "DOMContentLoaded" on "turbo:load" in dist/flowbite.js and in vendor/javascript/flowbite.js, but it's not work for me, my scripts work only if reload page |
@varProg I was having the same issue, and tried the solutions above with no success. Nevertheless, after checking a lot of similar problems I found something that might be helpful for us using Flowbite with Rails 7 as a temporary solution. On your application.js add the following lines:
According to the turbo.hotwired.dev handbook:
I also tried just setting the interactive element as The solution mentioned works for me, but it is by no means a permanent solution, because the Rails application has bigger transition times during the navigation after I did this change. However, it can help more experienced developers to identify a better solution (I have been working with Rails for only one month). I'll copy the link of that documentation for further reference. I hope you find this solution useful, in the meantime. |
Hey, IMHO this change introduced the bug 87a0c75 |
I ended up using stimulus to set up Flowbite functions. The example below is for a Dropdown element's functionality. app/views/shared/_nav.html.erb
app/javascripts/controllers/dropdown_controller.js
app/javascripts/application.js
|
This is a really great idea! Thank you @vladiim ! |
@indigodavid Working, not sure thats the correct solution tho. Thanks :D |
Just adding clarification for others trying to fix this, you'll need the turbo dist file instead of the standard flowbite index file (in addition to upgrading to |
Good job guys, but this partials sent over cable are still not fixed by this :( |
what @alhafoudh said. it still doesn't work with partials. would you be able to provide solution / refund or fix it? tbh only subscribed cause seen here that all issues with turbo were resolved, but the one i had - persists. |
Personally I think taking on the responsibility of component initialization has to make too many assumptions about the ways developers are manipulating their DOM. If component initialization wasn't attempted by simply including the script, we would be free to initialize in our own ways. With Rails, Stimulus makes the most sense (IMO) because just placing the component HTML on the page also initializes any associated JS. Is there any way we can opt to not auto-initialize? I noticed this:
Is there a way to not include that or would we have to write our own Flowbite index.ts? I suppose another long term solution would be to init components with MutationObservers, but that's quite a change. |
I'm getting this in my console:
The top of my application.js only has this: import "@hotwired/turbo-rails";
import "flowbite";
import "flowbite-datepicker";
import "flowbite/dist/flowbite.turbo.js";
import "controllers"; Are there any instructions missing? |
Hey everyone, Can you please try to initialise the components when using partials on a hook from Ruby on Rails 7 when the DOM has been rendered or re-rendered? For Nuxt.js this would be the equivalent:
Basically, you would only need to call the Here's some more info on this: https://flowbite.com/docs/getting-started/quickstart/#init-functions Cheers, |
You can check out an example for this in the latest comment for this issue: #499
And then:
|
Please check out the solution above by calling one single function to initialise all UI components that require data attributes. |
Yes, there is a way. You can initialise them yourself using |
@zoltanszogyenyi, thank you I'll try that. what could be the issue here tho: i have this line in my importmap and import it in application.js |
Hey @hs-dliaska, Because you need to make sure that Flowbite has been imported into your source JavaScript file. Here's the source code for what you actually import we using https://github.com/themesberg/flowbite/blob/main/src/index.ts So you should try this instead:
Can you confirm? Cheers, |
yeah here's my flowbite_controller.js :(
and i have this imported on my local: although it seems i miss something but not sure what Screen.Recording.2023-03-27.at.11.56.14.AM.movI also confused how it suppose to import I was trying to resolve it with |
I made it work with this however would be nice to consider some better options. not sure why I couldn't make it work just with initDropdown or initFlowbite method.
|
@hs-dliaska ☝️ you suggestion worked worked for me. // flowbite_controller.js
import { Controller } from "@hotwired/stimulus";
import "flowbite";
export default class FlowbiteController extends Controller {
initialize() {
document
.querySelectorAll("[data-dropdown-toggle]")
.forEach(function ($triggerEl) {
var dropdownId = $triggerEl.getAttribute("data-dropdown-toggle");
var $dropdownEl = document.getElementById(dropdownId);
if ($dropdownEl) {
var placement = $triggerEl.getAttribute("data-dropdown-placement");
var offsetSkidding = $triggerEl.getAttribute(
"data-dropdown-offset-skidding"
);
var offsetDistance = $triggerEl.getAttribute(
"data-dropdown-offset-distance"
);
var triggerType = $triggerEl.getAttribute("data-dropdown-trigger");
var delay = $triggerEl.getAttribute("data-dropdown-delay");
new Dropdown($dropdownEl, $triggerEl, {
placement: placement ? placement : "bottom",
triggerType: triggerType ? triggerType : "click",
offsetSkidding: offsetSkidding ? parseInt(offsetSkidding) : 0,
offsetDistance: offsetDistance ? parseInt(offsetDistance) : 10,
delay: delay ? parseInt(delay) : 300,
});
} else {
console.error(
'The dropdown element with id "'.concat(
dropdownId,
'" does not exist. Please check the data-dropdown-toggle attribute.'
)
);
}
});
}
} And usage <button data-controller="flowbite" id="dropdownMenuIconButton" data-dropdown-toggle="dropdownDots>" class="inline-flex items-center p-2 text-sm font-medium text-center text-gray-900 bg-white rounded-lg hover:bg-gray-100 focus:outline-none " type="button">
<svg class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 6a2 2 0 110-4 2 2 0 010 4zM10 12a2 2 0 110-4 2 2 0 010 4zM10 18a2 2 0 110-4 2 2 0 010 4z"></path></svg>
</button> |
At the moment, Flowbite Turbo initializes on these two events: "turbo:load" and "turbo:frame-load" However, in order to cover all cases (for example a form that returns 422 - Unprocessable Entity), |
I had the same issue with Symfony Ux Turbo and it worked 👍🏼 |
Is there something that I am missing here on how to get this to work? Following the Rails instructions it looks like you I have https://github.com/fzf/flowbite-test that replicates the behavior. |
Honestly, best to just use esbuild and follow the instructions of the original post, @filser89 explained it pretty well. I’ll ask him if there’s any updates on how best to implement. |
Thanks for this! @giugrilli
Using Flowbite with Rails 7 via importmaps and flowbite was not working after form submit with a 422 I'd prefer a more elegant solution, but this works.
Hope this helps you as well @fzf |
@AndrewNgKF this worked! Thank you for the suggestion |
I wanted to add this is still an issue on Rails 7, but @AndrewNgKF's suggestion does get it working again |
Thanks mate |
Hi! 👋
Firstly, thanks for your work on this project! 🙂
I got Flowbite working together with Turbo in my Rails 7 project. The original issue was that the javascript worked until the first turbo navigation was made and stoped working after that.
According to Turbo's documentaction this happened because Flowbite uses
DOMContentLoaded
event to install JS behavior, but this event only fires once, in response to the initial page load.In order to install the JS behavior on every page change I replaced
DOMContentLoaded
toturbo:load
event indist/flowbite.js
.turbo:load
event fires on every page change.I used patch-package to patch
flowbite@1.3.3
for the project I'm working on.Here is the diff that solved my problem:
This issue body was partially generated by patch-package.
The text was updated successfully, but these errors were encountered: