Describe the bug
Problem
AnimatedThemeToggler manages the theme by directly mutating the DOM and localStorage:
document.documentElement.classList.toggle("dark")
localStorage.setItem("theme", newTheme ? "dark" : "light")
This works in plain Vite/React setups, but breaks in Next.js projects using
next-themes (or similar theme context libraries). Because the toggle bypasses
the framework's theme context, useTheme() subscribers never receive the update —
so resolvedTheme stays stale until a full page refresh.
Expected Behavior
Any component consuming useTheme() should reactively update when the toggle fires.
Suggested Fix
Add an optional onToggle callback prop. No breaking changes — existing users
ignore it, framework users wire it up themselves:
interface AnimatedThemeTogglerProps {
// ...existing props
onToggle?: (isDark: boolean) => void
}
const applyTheme = () => {
const newTheme = !isDark
setIsDark(newTheme)
document.documentElement.classList.toggle("dark")
localStorage.setItem("theme", newTheme ? "dark" : "light")
onToggle?.(newTheme) // notify framework context
}
Usage in Next.js:
const { setTheme } = useTheme()
<AnimatedThemeToggler
onToggle={(isDark) => setTheme(isDark ? "dark" : "light")}
/>
Environment
- Next.js (App Router) with
next-themes
AnimatedThemeToggler from Magic UI
Impact
useTheme().resolvedTheme never updates on toggle — any component depending
on it (colors, WebGL, canvas, etc.) requires a full page refresh to reflect
the new theme.
Affected component/components
animated-theme-toggler
How to reproduce
- Create a Next.js app with
next-themes configured
- Add
AnimatedThemeToggler from Magic UI to your navbar
- In any other component, subscribe to the theme via
useTheme():
const { resolvedTheme } = useTheme()
console.log(resolvedTheme)
- Click the
AnimatedThemeToggler button to switch themes
- Observe that
resolvedTheme does not update — the console still shows the old value
- Refresh the page — now
resolvedTheme reflects the correct theme
Expected: resolvedTheme updates instantly on toggle
Actual: resolvedTheme only updates after a full page refresh
Codesandbox/StackBlitz link
No response
Logs
System Info
- OS: macOS
- Browser: Chrome 136
- Next.js: 15.3.2 (App Router)
- next-themes: 0.4.6
- Node.js: v22.x
- Package Manager: npm
Before submitting
Describe the bug
Problem
AnimatedThemeTogglermanages the theme by directly mutating the DOM and localStorage:This works in plain Vite/React setups, but breaks in Next.js projects using
next-themes(or similar theme context libraries). Because the toggle bypassesthe framework's theme context,
useTheme()subscribers never receive the update —so
resolvedThemestays stale until a full page refresh.Expected Behavior
Any component consuming
useTheme()should reactively update when the toggle fires.Suggested Fix
Add an optional
onTogglecallback prop. No breaking changes — existing usersignore it, framework users wire it up themselves:
Usage in Next.js:
Environment
next-themesAnimatedThemeTogglerfrom Magic UIImpact
useTheme().resolvedThemenever updates on toggle — any component dependingon it (colors, WebGL, canvas, etc.) requires a full page refresh to reflect
the new theme.
Affected component/components
animated-theme-toggler
How to reproduce
next-themesconfiguredAnimatedThemeTogglerfrom Magic UI to your navbaruseTheme():AnimatedThemeTogglerbutton to switch themesresolvedThemedoes not update — the console still shows the old valueresolvedThemereflects the correct themeExpected:
resolvedThemeupdates instantly on toggleActual:
resolvedThemeonly updates after a full page refreshCodesandbox/StackBlitz link
No response
Logs
System Info
Before submitting