Skip to content
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

Rails 8 + Importmaps - missing backdrop #998

Open
alec-c4 opened this issue Nov 18, 2024 · 13 comments
Open

Rails 8 + Importmaps - missing backdrop #998

alec-c4 opened this issue Nov 18, 2024 · 13 comments

Comments

@alec-c4
Copy link

alec-c4 commented Nov 18, 2024

Describe the bug
There are no backdrop in rails8 app using importmaps

To Reproduce

SCR-20241118-oknd
@alec-c4
Copy link
Author

alec-c4 commented Nov 18, 2024

I've created an app with esbuild and backdrop works fine

@alec-c4
Copy link
Author

alec-c4 commented Nov 29, 2024

@jbwl May I ask you to explain your point of view?

@bitshaker
Copy link

@alec-c4 you didn't add the css into your project, that's likely why it's not picking up the backdrop

@alec-c4
Copy link
Author

alec-c4 commented Dec 1, 2024

@bitshaker and https://github.com/alec-c4/flowbite_issue/blob/fd8c7059d4b767fefd804bfe02881c3015d4b14f/app/javascript/application.js#L5

As it described in docs - I need only to pin flowbite to importmap.rb and to add it to application.js

@bitshaker
Copy link

The Flowbite specific css file is not in your repo. It needs to be there. Here’s a simple way to do it.

https://youtu.be/zDhM5HQ10aA

tl;dw

Download the latest CDN versions of the flowbite.min.css and flowbite.turbo.min.js files.

Put the flowbite.turbo.min.js file in vendor/javascript
Put the flowbite.min.css in vendor/assets/stylesheets

Add flowbite to the importmap.rb file like:

pin "flowbite", to: "flowbite.turbo.min.js"

Add flowbite to the application.js file like:

import "flowbite"

In the tailwind.config.js file, add a darkMode toggle class preference (default is media if you want to leave it alone) like:

module.exports = {
  darkMode: "class", //add this line
  content: [ 
  //all the default stuff here
]
}

Add the stylesheet to application.html.erb like:

<%= stylesheet_link_tag "flowbite", "data-turbo-track": "reload" %>

Bonus: download the markdown files from the flowbite repo where it describes each component. That way, you have context for AI coding help or just your own documentation in your IDE

@alec-c4
Copy link
Author

alec-c4 commented Dec 1, 2024

@bitshaker I've checked this video and, you know, it goes against almost all rails principles. But, anyway, let's check docs, I've mentioned earlier. If you clone my repo and check it - you'll see that everything done following this guide. Also, you can add any code sample from flowbite docs and everything work fine, except backdrop.

But, ok, forget my repo, let's check rails guides - https://guides.rubyonrails.org/asset_pipeline.html Everytime you pin any package - it is downloaded to vendor/javascript folder for some reason (offline access, prevent from yanking from package hosting, etc.). So, we need to create a separate package for flowbite.turbo. Also, I've found following issue rails/importmap-rails#80 - with importmaps it is impossible to pin CSS. That's why the author of your video have downloaded flowbite.turbo.js and css to vendor folder. As I said - it isn't a rails way, but a workaround.

I have an idea - it is possible to publish ruby gem, which will make all required js/css available without importmaps. @zoltanszogyenyi what do you think? Dow you need my help with rubygem?

@alec-c4
Copy link
Author

alec-c4 commented Dec 1, 2024

Also, I've found the following gem https://github.com/iwdt/flowbite-rails but it looks abandoned

@alec-c4
Copy link
Author

alec-c4 commented Dec 1, 2024

I've checked again this video and there is another problem - author have imported tailwind 2 times - from app/assets/stylesheets/application.tailwind.css and from vendor/assets/stylesheets/flowbite.css

I strongly recommend avoiding following such scheme. There are 2 working schemes:

  1. rubygem with js and css
  2. create separate flowbite.turbo package + add ability to download and put to app/assets/stylesheets or vendor/assets/stylesheets clean css file without tailwind and other stuff, only flowbite code.

I think that rubygem way is more accurate, because developers, who use flowbite, will receive updated assets without necessarily to update local files

@bitshaker
Copy link

Agreed. If you know how to put a gem together, I’ll happily use it and help if I can.

@alec-c4
Copy link
Author

alec-c4 commented Dec 2, 2024

@bitshaker I'll try to do something this weekend

@jaspervanbrian
Copy link

I can confirm that this problem exists.
Also datepicker looks wonky without the minified css.

@alec-c4
Copy link
Author

alec-c4 commented Dec 12, 2024

Hey! I spent some time and I think I know what's wrong with backdrop. The problem occurs is because of tailwind do purgecss for unused classes and all those classes are listed in flowbite/plugin.js. There is a problem with flowbite plugin, it cannot be loaded with importmap. I've created a gem (but didn't publish yet because of plugin.js issue) which pins flowbite.turbo.js avoiding necessarily to pin flowbite to importmap.rb, but I haven't fixed an issue when I do put plugin.js locally and link it in tailwind.config.js :( But if you're strong in JavaScript I'll be appreciated for your help.

telegram-cloud-photo-size-2-5389019294949566147-y
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants