ui icon indicating copy to clipboard operation
ui copied to clipboard

Applying transition-colors to light / dark theme

Open vitorCamarotto opened this issue 9 months ago • 7 comments

Description

Hi, I'm having a hard time applying transition to dark / light background.

My main.css file is as follows.

Changing the background color from slate to stone, for example, works fine.

The problem is just for the light / dark theme transition.

:root {
  --ui-bg: var(--ui-color-neutral-100);
}

.dark {
  --ui-bg: var(--ui-color-neutral-900);
}

@layer base {
  body {
    @apply transition-colors duration-1000;
  }
}

Here is the function that changes the theme:

const isDark = computed({
  get() {
    return colorMode.value === 'dark';
  },
  set() {
    colorMode.preference = colorMode.value === 'dark' ? 'light' : 'dark';
  },
});

Any help would be appreciated, thanks!

vitorCamarotto avatar Mar 21 '25 13:03 vitorCamarotto

This might happen because @nuxt/ui disabled @nuxtjs/color-mode transitions: https://github.com/nuxt/ui/blob/v3/src/module.ts#L97

You should be able to re-enable it in your nuxt.config.ts.

benjamincanac avatar Mar 21 '25 15:03 benjamincanac

@benjamincanac Thanks for the response!

I tried enabling it, didn't work:

export default defineNuxtConfig({
  colorMode: {
    preference: 'dark',
    disableTransition: false
  },
})

Maybe it's because --ui-bg variable is being redefined when changing the theme from light to dark on :root and .dark... Since the selector doesn't remain the same, CSS can't really trigger an animation?

Not sure if that would make sense

And just trying to make sure it's not a mistake by my side: the following computed works as expected on the body background (triggers animation).

const neutral = computed({
  get() {
    return appConfig.ui.colors.neutral;
  },
  set(option) {
    appConfig.ui.colors.neutral = option;
    window.localStorage.setItem('nuxt-ui-neutral', appConfig.ui.colors.neutral);
  },
});

vitorCamarotto avatar Mar 21 '25 16:03 vitorCamarotto

Same here, cant make this work. It's minor but would be nice to have.

IliasPavlakos avatar Apr 10 '25 19:04 IliasPavlakos

Does it work when using bg-light dark:bg-black on your body? 🤔

benjamincanac avatar Apr 11 '25 12:04 benjamincanac

@benjamincanac Tried it but still doent work, the transition is not applied when changing from light to dark and back (setting the theme preference with @nuxtjs/color-mode.)

From what i tested, changing light/dark mode doesnt apply the transition but changing just the bg color while in the same mode the transition works.

IliasPavlakos avatar Apr 11 '25 18:04 IliasPavlakos

I tried what you suggested, it did not work:

// no transition

@layer base {
  body {
    @apply bg-white dark:bg-black transition-all duration-1000;
  }
}

Note that changing the secondary color while on the same mode does work, as @IliasPavlakos stated.

// transition works fine i.e.
// changing secondary color from Stone to Slate

@layer base {
  body {
    @apply transition-all duration-1000;
  }
}

vitorCamarotto avatar Apr 11 '25 18:04 vitorCamarotto

Same here. No transition of colors when switching modes. It's unpleasant to switch from dark to light without any transition. It's just a switch. BAM! And your eyes receive full-blown white BG in the face ;-)

It would be nice to have some degree of softening the blow ;-)

KonradHEXANT avatar Apr 23 '25 19:04 KonradHEXANT

@HugoRCD Can your work on the portfolio template solve this? 🤔

benjamincanac avatar May 02 '25 14:05 benjamincanac

@HugoRCD Any updates on this?

abhishekvash avatar May 20 '25 08:05 abhishekvash

@abhishekvash On the portfolio template I used the view-transition so it's not really a color transition, that's why it works, but I'll have to look at it because I don't see why the "basic" colors transition doesn't work

HugoRCD avatar May 20 '25 08:05 HugoRCD

First of all, same issue here.

This might happen because @nuxt/ui disabled @nuxtjs/color-mode transitions: https://github.com/nuxt/ui/blob/v3/src/module.ts#L107

You should be able to re-enable it in your nuxt.config.ts.

I tested this by changing that line to disableTransition: false, and the color transition worked smoothly again.

However, this setting doesn't seem to be configurable via nuxt.config.ts. It would be very helpful if disableTransition could be made configurable so that transitions can be enabled without modifying the package code.

Also worth noting: once transitions are enabled, they work perfectly with Nuxt UI color like bg-(--ui-bg).

Uniixon avatar Jun 08 '25 09:06 Uniixon

@HugoRCD any updates on this ?

jeanprbt avatar Jul 04 '25 11:07 jeanprbt

@jeanprbt I've tried a few local tests and so far I've got some elements that manage to transition by reactivating the disableTransition of colorMode but I still don't understand why they don't all do it and why it renders so badly 🤔 https://github.com/user-attachments/assets/f5aaed85-85d0-421c-a006-6ce8af742626

HugoRCD avatar Jul 04 '25 12:07 HugoRCD

@benjamincanac @HugoRCD What was the initial reason why disableTransition is set to false by @nuxt/ui, and would it be possible to add this as a setting in nuxt.config.ts?

For now, as a fix, I don't use @nuxtjs/color-mode anymore to manage light/dark toggling but I rely on tailwind manual toggling and I use the following trick to toggle dark mode manually (of course, can be extended to follow system preferences using prefers-color-scheme). Transitions work again and I can use the wonderful components from @nuxt/ui library!

const isDark = ref(false);
const toggleDarkMode = () => isDark.value = !isDark.value;
watch(isDark, (newValue) => {
    document.documentElement.classList.toggle('dark', newValue)
});

jeanprbt avatar Jul 09 '25 08:07 jeanprbt

@jeanprbt This is set by default to avoid weird transitions when switching between light and dark mode, like: https://github.com/user-attachments/assets/4e8d2cb1-d673-4ed0-9f73-275f9a10d593

I thought it was possible to re-enable this option in your nuxt.config.ts but realized there was a bug in options merging, pushed this to fix it: https://github.com/nuxt/ui/commit/78f92a24f8ac5cd0cf1e1c5b55dfc8476612bf93

export default defineNuxtConfig({
  modules: ['@nuxt/ui'],
  css: ['~/assets/css/main.css'],
  colorMode: {
    disableTransition: false
  },
})

benjamincanac avatar Jul 09 '25 10:07 benjamincanac

I believe we can close this since https://github.com/nuxt/ui/commit/78f92a24f8ac5cd0cf1e1c5b55dfc8476612bf93 fixes the issue. Sorry for not finding this earlier, I was convinced the installModule was merging with user's config.

benjamincanac avatar Jul 09 '25 10:07 benjamincanac