Flowbite 3 + Rails 8:Modal与Drawer背景层不可见问题求助
问题:Rails 8 + Tailwind 4.0 + Flowbite 3.0 中 Modal/Drawer 背景层不可见
从Flowbite文档复制Modal示例代码到Rails 8项目后,Modal和Drawer的背景层尺寸为0,完全不可见。即使手动添加h-full和w-full类让背景层尺寸变为全屏,依然看不到背景层,但Flowbite官网的Modal背景层显示正常。
环境信息
- Rails 8
- Tailwind 4.0
- Flowbite 3.0
相关配置文件
tailwind.config.js
const defaultTheme = require('tailwindcss/defaultTheme') module.exports = { darkMode: 'class', content: [ './public/*.html', './app/helpers/**/*.rb', './app/javascript/**/*.js', './app/views/**/*.{erb,haml,html,slim}', './node_modules/flowbite/**/*.js' ], theme: { extend: { fontFamily: { sans: ['Inter var', ...defaultTheme.fontFamily.sans], }, }, }, plugins: [ require('flowbite/plugin') // require('@tailwindcss/forms'), // require('@tailwindcss/typography'), // require('@tailwindcss/container-queries'), ] }
application.js
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails import "@hotwired/turbo-rails" import "controllers" import 'flowbite'
importmap.rb
# Pin npm packages by running ./bin/importmap pin "application" pin "@hotwired/turbo-rails", to: "turbo.min.js" pin "@hotwired/stimulus", to: "stimulus.min.js" pin "@hotwired/stimulus-loading", to: "stimulus-loading.js" pin_all_from "app/javascript/controllers", under: "controllers" pin "flowbite", to: "https://cdn.jsdelivr.net/npm/flowbite@2.5.2/dist/flowbite.turbo.min.js"
application.html.erb
<!DOCTYPE html> <html class=""> <head> <title><%= content_for(:title) %></title> <meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="mobile-web-app-capable" content="yes"> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= yield :head %> <%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %> <%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %> <link rel="icon" href="/icon.png" type="image/png"> <link rel="icon" href="/icon.svg" type="image/svg+xml"> <link rel="apple-touch-icon" href="/icon.png"> <%# Includes all stylesheet files in app/assets/stylesheets %> <%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %> <%= stylesheet_link_tag :app, "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> </head> <body> <!-- Modal toggle --> <button data-modal-target="default-modal" data-modal-toggle="default-modal" class="block text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" type="button"> Toggle modal </button> <!-- Main modal --> <div id="default-modal" tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full"> <div class="relative p-4 w-full max-w-2xl max-h-full"> <!-- Modal content --> <div class="relative bg-white rounded-lg shadow-sm dark:bg-gray-700"> <!-- Modal header --> <div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600 border-gray-200"> <h3 class="text-xl font-semibold text-gray-900 dark:text-white"> Terms of Service </h3> <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="default-modal"> <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14"> <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/> </svg> <span class="sr-only">Close modal</span> </button> </div> <!-- Modal body --> <div class="p-4 md:p-5 space-y-4"> <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400"> With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply. </p> <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400"> The European Union’s General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them. </p> </div> <!-- Modal footer --> <div class="flex items-center p-4 md:p-5 border-t border-gray-200 rounded-b dark:border-gray-600"> <button data-modal-hide="default-modal" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button> <button data-modal-hide="default-modal" type="button" class="py-2.5 px-5 ms-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700">Decline</button> </div> </div> </div> </div> </body> </html>
解决方案
1. 对齐Flowbite版本
你的importmap.rb中引用的Flowbite是2.5.2版本,但环境标注的是Flowbite 3.0,版本不匹配会导致样式与JS逻辑冲突。更新importmap.rb里的Flowbite链接为3.x版本的Turbo兼容包:
pin "flowbite", to: "https://cdn.jsdelivr.net/npm/flowbite@3.0.0/dist/flowbite.turbo.min.js"
2. 修复Tailwind配置的Content路径
Tailwind 4.0对Content路径的处理更严格,需确保Flowbite的样式能被正确扫描到。更新tailwind.config.js的Content数组,添加Flowbite的HTML文件路径:
content: [ './public/*.html', './app/helpers/**/*.rb', './app/javascript/**/*.js', './app/views/**/*.{erb,haml,html,slim}', './node_modules/flowbite/**/*.{js,html}' // 添加该行,包含Flowbite的HTML文件 ],
3. 确保背景层样式未被Purge
Flowbite的Modal背景层是JS动态添加的modal-backdrop元素,默认带有fixed inset-0 bg-gray-900 bg-opacity-50 transition-opacity类。如果这些样式被Tailwind Purge移除,手动添加到Safelist:
module.exports = { // ...其他配置 safelist: [ 'bg-gray-900', 'bg-opacity-50', 'fixed', 'inset-0', 'transition-opacity' ] }
4. 适配Turbo页面加载机制
由于使用Turbo,需确保Flowbite在Turbo页面加载完成后重新初始化组件。修改application.js:
import "@hotwired/turbo-rails" import "controllers" import flowbite from 'flowbite' // Turbo加载完成后重新初始化Flowbite document.addEventListener('turbo:load', () => { flowbite.initFlowbite() })
内容的提问来源于stack exchange,提问作者Nikolay Lipovtsev
相关产品推荐
相关产品推荐

