flowbite icon indicating copy to clipboard operation
flowbite copied to clipboard

flowbite not working with NextJS

Open fxmb opened this issue 4 years ago • 79 comments

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 error document not found.
  • Is it correct to inject the script just with <script src="../path/to/@themesberg/flowbite/dist/flowbite.bundle.js"></script>? Is the path/to just 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?

fxmb avatar Dec 22 '21 16:12 fxmb

I got the same problem right now! I'll try to fix it.

Schermata 2021-12-23 alle 13 49 16 .

stefanovolpatti avatar Dec 23 '21 12:12 stefanovolpatti

@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 avatar Dec 23 '21 13:12 zoltanszogyenyi

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?

ozamamurzleen avatar Dec 27 '21 10:12 ozamamurzleen

@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?

fxmb avatar Dec 27 '21 10:12 fxmb

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.

zoltanszogyenyi avatar Jan 03 '22 10:01 zoltanszogyenyi

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!

zoltanszogyenyi avatar Jan 06 '22 19:01 zoltanszogyenyi

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"

stefanovolpatti avatar Jan 06 '22 20:01 stefanovolpatti

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!

zoltanszogyenyi avatar Jan 06 '22 20:01 zoltanszogyenyi

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!

stefanovolpatti avatar Jan 06 '22 20:01 stefanovolpatti

@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 avatar Jan 06 '22 20:01 zoltanszogyenyi

@zoltanszogyenyi of course and thank you!

stefanovolpatti avatar Jan 06 '22 20:01 stefanovolpatti

@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)

fxmb avatar Jan 07 '22 14:01 fxmb

@volp99 Would be amazing if you could let me know in case you find a solution or other workaround :-)

fxmb avatar Jan 07 '22 14:01 fxmb

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.

zoltanszogyenyi avatar Jan 07 '22 14:01 zoltanszogyenyi

I'll just give this a +1. Utilizing NextJS 12 and receiving this exact error when attempting to utilize.

Rykuno avatar Jan 10 '22 22:01 Rykuno

same here with next 12

bonesoul avatar Jan 11 '22 10:01 bonesoul

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')
  }, [])

flowbite-nextjs

bragamat avatar Jan 15 '22 04:01 bragamat

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......|

bragamat avatar Jan 15 '22 04:01 bragamat

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')
  }, [])

flowbite-nextjs

@bragamat Thanks for looking into this! This solution still doesnt work for me (e.g. the tooltip just does not appear on hover).

fxmb avatar Jan 15 '22 08:01 fxmb

Now, it's been a month but the issue has not been solved yet.

iamhimanshusrivastava avatar Jan 24 '22 10:01 iamhimanshusrivastava