themesberg / flowbite

Open-source UI component library and front-end development framework based on Tailwind CSS
https://flowbite.com
MIT License
7.35k stars 711 forks source link

Fix Rails Turbo support #700

Closed WozniakMac closed 5 months ago

WozniakMac commented 8 months ago

Fixes issues: https://github.com/themesberg/flowbite/issues/697 https://github.com/themesberg/flowbite/issues/698

BRAKING CHANGES:

data-modal-toggle will not work without data-modal-target anymore.

zoltanszogyenyi commented 8 months ago

Hey @WozniakMac,

Thanks for the PR! Couple of problems from my side:

Cheers, Zoltan

WozniakMac commented 8 months ago

@zoltanszogyenyi Thanks for review!

  1. data-drawer-toggle stays but will work exactly this same as data-drawer-show and data-drawer-hide. It was working also without data-modal-target what was inconsistent. I did it just to simplify things. From now all modals will need to have data-modal-target because it will be tag that initialise modal.
  2. The reason for local instance manager is to make sure it adds hide/show event listeners only to those modals that was initialise in current initModals run. If you run initModals again it will it will create modals and add event listeners only to new html tags that were added to the page. If you have better ideas how to make sure we don't create modal objects and event listeners on every run I'll be happy to change it. For event listeners we always check if it exists in Instance Manager. That was the issue because Old and New modals will be there.
  3. It shouldn't impact vanilla because if you run it once it works as before. Other frameworks may benefit from this if they update html on fly.

Basically I'm trying to solve 3 things here:

  1. Make sure we initialise modals and event listeners only once if you run initModals multiple times.
  2. Make sure we initialise modals and event listeners for added html if you run initModals again.
  3. Reset all instances on turbo:load because it swaps the whole body with new one.

I can revert changes to data-drawer-toggle if you want to maintain backwards compatibility with 1.6