jmnda

jmnda

Tailwind Javascripts stop working after live redirects between liveviews

Am using tailwind CSS and Flowbite component library. However what’s happening is that when loading a LiveView page and interact with a Flowbite component like a Modal, it’s working fine, but if I redirect to another LiveView and back to the same page, the Modal stops working. This applies to all elements that require Flowbite javascript for interaction like Accordions, Modals, Sidebars e.t.c. I tried to use CDN’s since configuring tailwind in my phoenix project was not working as well. Even after adding CDN’s, the problem still persists. Here is what I added to root.html.heex

<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://unpkg.com/flowbite@1.5.3/dist/flowbite.min.css" />

<body>
. . . 

<script src="https://unpkg.com/flowbite@1.5.3/dist/flowbite.js"></script>
</body>

But when I use Bootstrap CSS and JS CDN’s, all the bootstrap components that use javascript work fine even if I navigate between LiveViews. Not sure what’s going on but need help ASAP.

Marked As Solved

egze

egze

Flowbite inits the objects when the DOM loads (DOMContentLoaded event). With live navigation, this event doesn’t fire anymore, that’s why on next LiveView it stops working.

What you can do as a quick fix, is listen to LiveView events and dispatch the DOMContentLoaded event yourself, to trick Flowbite.

Put this in your app.js

window.addEventListener('phx:page-loading-stop', (event) => {
  // trigger flowbite events
  window.document.dispatchEvent(new Event("DOMContentLoaded", {
    bubbles: true,
    cancelable: true
  }));
});

Also Liked

jmnda

jmnda

Actually, it’s working. I didn’t realize I was testing it in the actual project and was not working, but it’s working it the repo I shared. I have also discovered that it’s only working when I add Flowbite via a CDN script tag, but not when I install it as a node_module and import it in app.js and add it to tailwind.config.js plugins. and content. Thanks very much for the help. For now, I will just work with the CDN and figure out the rest later

selimyoussry

selimyoussry

Hey all, the solution will be soon merged into Flowbite JS, see Support phoenix liveview by jmnda-dev · Pull Request #487 · themesberg/flowbite (github.com).

I’ve pasted my patch as a PR comment if you want to get going right now.

Cheers

egze

egze

Can you create a demo repo to reproduce? Otherwise, it’s hard to help.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement