flowbite not working with NextJS
Issue
- We have installed flowbite to our react project by following recommended steps via npm. We added it to tailwind config etc
- The code is just not working properly. The effects (e.g. tooltip) do not show on hover. If we try to import it directly (via
import '@themesberg/flowbite';we get an errordocument not found. - Is it correct to inject the script just with
<script src="../path/to/@themesberg/flowbite/dist/flowbite.bundle.js"></script>? Is thepath/tojust a placeholder for another custom local path? - We are using NextJS. I think the issue could be related to SSR but I am not sure. Any pointers?
I got the same problem right now! I'll try to fix it.
.
@volp99 feel free to also fork the project as you may need to update the JS from Flowbite.
The main issue is with the event listeners not being registered as React uses a virtual DOM and the Flowbite script looks for those data attributes.
The other non-interactive elements should work regardless.
Same issue here followed all steps mentioned on their site. Using it via npm and nextjs/tailwind. Sometimes it works and sometimes when page reloads it start giving document not found error. I am using tooltip from flowbite. And importing import '@themesberg/flowbite' in _app.js. Is it the right place to import?
@volp99 feel free to also fork the project as you may need to update the JS from Flowbite.
The main issue is with the event listeners not being registered as React uses a virtual DOM and the Flowbite script looks for those data attributes.
The other non-interactive elements should work regardless.
@zoltanszogyenyi thanks for your reply. Is there a way to fix this without a (rather cumbersome) fork?
Hey @franzbewerunge,
One way to work with Flowbite with React/Vue would be to include only the Tailwind CSS plugin and code the interactive behaviour yourself or with the help of an external library.
We will create a project on Github for React and Vue and we'll start building the components with the community. ETA is probably a few months.
Hey everyone,
Please update Flowbite to v1.3.0 and check out whether it works for Next JS or not.
We've tested it for React and Vue.js and it works (it didn't work before).
Thanks!
Hello @zoltanszogyenyi ! I update Flowbite to v1.3.0 right now but unfortunately still not works for Next JS (same error: document is not defined)
if it can help, I'm using:
"next": "12.0.7",
"react": "17.0.2",
"typescript": "4.5.4",
"tailwindcss": "^3.0.7"
Hello,
Try importing the JS as described here:
https://www.kindacode.com/article/how-to-correctly-use-bootstrap-5-in-next-js/
Let me know if it helps!
Thanks for your reply,
I follow the steps as described above but I always get the same error. I try also to install again all the dependencies but nothing.
I'm going to find another way!
@volp99 that is odd. I'll check it out with a fresh NextJS install the next few days. Please let me know if you've made progress.
@zoltanszogyenyi of course and thank you!
@zoltanszogyenyi I can confirm that it does not work for following config:
"next": "^12.0.3"
"react": "^17.0.2"
"tailwindcss": "^3.0.11",
"typescript": "^4.1.2"
Thanks for looking into this! I updated flowbite to the latest version (1.3.0)
@volp99 Would be amazing if you could let me know in case you find a solution or other workaround :-)
Looks like this is a NextJS specific problem. I'll look into it better next week.
If you anyone finds a workaround, or more info on this, let me know.
I'll just give this a +1. Utilizing NextJS 12 and receiving this exact error when attempting to utilize.
same here with next 12
Hey I don't know if it helps you people, but I actually got a workaround on this... I'm still working on it but I got some javascript actions working with those lines.
on your _app.js add:
useEffect(() => {
import('@themesberg/flowbite')
}, [])

Another (hideous) way to make this work would be adding the js directly on the an Script (https://nextjs.org/docs/basic-features/script#afterinteractive) tag.
so, for example, we would add this on the _app.js
import Script from 'next/script'
.
.
.
<Script
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
const isSidebarExpanded = toggleSidebarEl => {
return toggleSidebarEl.getAttribute('aria-expanded') === 'true' ? true : false;
}
const toggleSidebar = (sidebarEl, expand, setExpanded = false) => {
const bottomMenuEl = document.querySelector('[sidebar-bottom-menu]');
const mainContentEl = document.getElementById('main-content');
if (expand) {
sidebarEl.classList.add('lg:w-64');
sidebarEl.classList.remove('lg:w-16');
mainContentEl.classList.add('lg:ml-64');
mai......|
Hey I don't know if it helps you people, but I actually got a workaround on this... I'm still working on it but I got some javascript actions working with those lines.
on your
_app.jsadd:useEffect(() => { import('@themesberg/flowbite') }, [])
@bragamat Thanks for looking into this! This solution still doesnt work for me (e.g. the tooltip just does not appear on hover).
Now, it's been a month but the issue has not been solved yet.