next-themes icon indicating copy to clipboard operation
next-themes copied to clipboard

attribute="class" causing the following error with Tailwindcss and nextjs

Open avastamin opened this issue 2 years ago • 12 comments

It's working only with ThemeProvider but not working with attribute="class"

  <ThemeProvider attribute="class">
      <Component {...pageProps} />
    </ThemeProvider>

InvalidCharacterError: Failed to execute 'add' on 'DOMTokenList': The token provided ('[object Object]') contains HTML space characters, which are not valid in tokens.

avastamin avatar Dec 04 '22 03:12 avastamin

I have had this happen too, but only when the class I specify for the theme has spaces in it. To be specific that means I had a value of {light: 'classA classB'}

I don't know of a way to specify two classes for a theme 😕. But hopefully this helps you debug.

jylertones avatar Dec 14 '22 16:12 jylertones

I only noticed this happening when providing a function to the setTheme function.

e.g

setTheme((prev) => prev === 'dark' ? 'light' : 'dark')

This sets the function as a string to LocalStorage. Not sure if you're seeing the issue for this reason.

martingoodloans avatar Dec 22 '22 11:12 martingoodloans

I got this while working in NextJs 13

684efs3 avatar Jan 12 '23 19:01 684efs3

@684efs3 How were you able to get this working in Nextjs version 13? Are you using the App directory?

brentrobbins avatar Jan 14 '23 00:01 brentrobbins

same problem here using appDir

EtanHey avatar Feb 14 '23 03:02 EtanHey

Please, I will like to know if someone found a work around on this issue.

HenryNwankwo avatar Jul 14 '23 16:07 HenryNwankwo

I only noticed this happening when providing a function to the setTheme function.

e.g

setTheme((prev) => prev === 'dark' ? 'light' : 'dark')

This sets the function as a string to LocalStorage. Not sure if you're seeing the issue for this reason.

I have removed the setTheme, updated next to @latest, and deleted the node modules and reinstall but still the same issue

HenryNwankwo avatar Jul 14 '23 16:07 HenryNwankwo

Hey, has anyone here got an update about this issue? I am using Next.js 13 with appdir, thanks in advance!

marcobusico avatar Oct 21 '23 10:10 marcobusico

I fixed this on Next.js 14 by doing the following

  1. In Chrome Developer Tools, go to Application > Local Storage > delete the theme variable
  2. Change
<button onClick={ () => { setTheme((prev) => prev === 'dark' ? 'light' : 'dark') } }>
change mode
</button>

to

<button onClick={ theme === "light" ? () => { setTheme("dark") } : () => { setTheme("light") } }>
change mode
</button>

(Not 100% sure if step 2 is necessary, but it worked for me.)

ben519 avatar Nov 30 '23 01:11 ben519

This issue still persists for both nextjs13 and 14 . anyone got it working yet ?

boldrack avatar Dec 03 '23 20:12 boldrack

  1. In Chrome Developer Tools, go to Application > Local Storage > delete the theme variable

This worked for me, step 2 wasn't necessary.

steezplusplus avatar Dec 19 '23 00:12 steezplusplus

  1. In Chrome Developer Tools, go to Application > Local Storage > delete the theme variable

This worked for me, step 2 wasn't necessary.

I was working in other project using chakra ui theme, so i think this is causing a conflict.

martins4duarte avatar Feb 21 '24 13:02 martins4duarte