Nuxt UI
Animate Nuxt UI's UColorModeButton, UColorModeSwitch and UColorModeSelect with no wrapper.
Nuxt UI registers @nuxtjs/color-mode itself. With nuxt-penumbra installed, its color mode components animate without any change to your templates.
export default defineNuxtConfig({
modules: ['@nuxt/ui', 'nuxt-penumbra'],
css: ['~/assets/css/main.css'],
penumbra: { transition: 'circle', duration: 600 },
})
<template>
<UApp>
<UColorModeButton />
<NuxtPage />
</UApp>
</template>
The same works with UColorModeSwitch and UColorModeSelect. Clicking one of them shows the configured effect.
How it works
Those components write to useColorMode().preference directly. nuxt-penumbra watches preference with a synchronous watcher. When a write arrives that did not come from the module itself, the watcher puts the old value back and replays the write inside a view transition, so the browser snapshots the old page first. The new value is then applied inside the transition.
Because the module only intercepts preference, it never stores a theme in this mode. Color-mode’s storage, cookie and system handling stay in charge.
nuxt-ui.spec.ts in the Nuxt UI playground runs the three components with no wrapper, in Chromium and WebKit. The reveal starts at the center of the screen, or at the last click if it happened within the previous second.
Setup order
The module configures itself after all modules (modules:done), so it sees the color-mode registration that Nuxt UI makes. Put nuxt-penumbra anywhere in modules.
disableTransition
Nuxt UI registers color-mode with disableTransition: true. That only suppresses CSS transitions for a frame while the class changes. It does not interfere with view transitions, as the same spec shows.
Using the toggle instead
PenumbraToggle also works inside a Nuxt UI app. It is a plain button without Nuxt UI styling. If you want Nuxt UI’s look with the module’s origin handling, use UColorModeButton and let the module intercept it.
Unverified
Nuxt UI versions other than the one in the playground, and combinations with other modules that write preference, have not been tested. See browser support and compatibility.