Dark/Light simple animation toggle issue
Answered
RxvxntxN posted this in #help-forum
RxvxntxNOP
Hello
Greetings ,
I am facing a simple animation toggle issue with
Issue is that, it is instant but not animated with duration. When i took certain code from original to simple one to test if it works and it is working.
Greetings ,
I am facing a simple animation toggle issue with
rotation issue with icon sun and moon from Lucide React,Issue is that, it is instant but not animated with duration. When i took certain code from original to simple one to test if it works and it is working.
"use client";
import { useState } from "react";
import { Moon, Sun } from "lucide-react";
export default function ButtonRotationTest() {
const [isDark, setIsDark] = useState(false);
return (
<div className="p-8">
<button
onClick={() => setIsDark(!isDark)}
className="mb-8 px-4 py-2 bg-blue-500 text-white rounded"
>
Toggle Dark Mode
</button>
<div className={`relative p-4 rounded-lg ${isDark ? 'dark' : ''}`}>
<button
className="inline-flex items-center justify-center w-10 h-10 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800"
title={isDark ? "Switch to light mode" : "Switch to dark mode"}
>
<Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0 duration-1000" />
<Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100 duration-1000" />
</button>
</div>
<p className="mt-4">{isDark.toString()}</p>
</div>
);
}47 Replies
can you post the video of what it looks like?
have you tried to reproduce the issue using simple elements instead of using <Sun> ?
this is simple one
have you tried to reproduce the issue using simple elements instead of using <Sun> ?
change <Sun className="..."> with <p className="...">T</p>
can you reproduce the issue on a minimal reproducible repository?
@ᴉuɐpɹɐɐ did you use <Button> or <button>? please be consistent. its hard to tell what your code is like
RxvxntxNOP
i am using Button from shadcn
I shall try to be consistent.
Ooof , i did mistake , for Simple one i did use
button instead of shadcn Buttonyeah thats why im confused :(
so we now know, <button> works but not <Button> 

what is inside <Button> ?
nice observation!
RxvxntxNOP
I am trying hard to find what am i doing wrong , i am thinking to wrap it with more
divmaybe its the parent/wrapper component thats causing the issue
you can also try to reproduce the bug in another repo and share it so people can debug it
RxvxntxNOP
where can i upload ? in my git ?
@ᴉuɐpɹɐɐ you can also try to reproduce the bug in another repo and share it so people can debug it
RxvxntxNOP
Sorry , forgive me , i don't understand. I am happy to share.
i will create git then
@RxvxntxN where can i upload ? in my git ?
github repo yeah
minimum reproducible repository means keeping the bug but remove unecessary code that doesn't cause the bug
RxvxntxNOP
yeah , i will give there one navigation page
including simple one that works
I made it minimal as possible
RxvxntxNOP
bump
also if you want view transition the theme not only icon check this https://theme-toggle.rdsx.dev/ but for react you gonna need to flushsync the update before start transtion let you here the relevant code but it use two icons is not a switcher
const { theme, setTheme } = useTheme();
const iconSize = 14;
const handleToggle = (newTheme: string) => {
if (newTheme === theme) return; //
if (!document.startViewTransition) {
setTheme(newTheme);
return;
}
document.startViewTransition(() => {
flushSync(() => {
setTheme(newTheme);
});
});
};@Nuitari also if you want view transition the theme not only icon check this https://theme-toggle.rdsx.dev/ but for react you gonna need to flushsync the update before start transtion let you here the relevant code but it use two icons is not a switcherjs
const { theme, setTheme } = useTheme();
const iconSize = 14;
const handleToggle = (newTheme: string) => {
if (newTheme === theme) return; //
if (!document.startViewTransition) {
setTheme(newTheme);
return;
}
document.startViewTransition(() => {
flushSync(() => {
setTheme(newTheme);
});
});
};
RxvxntxNOP
I didn't ask these , I simply asked that when i click toggle , it is not animated. i have even added separate file which was worked but that same toggle does't work on navigation bar.
Don't think anything fancy , it is simple toggle , that transition doesn't work.
Try it though , i gave you git repo.
it worked to me button is transitioning
also add transition to background or will switch instant
i am talking about this one
ok didnt notice gonna check
you notice that?
@Nuitari Click to see attachment
RxvxntxNOP
Oh right , I didn't find that lmfao , congrates , you solved it officially
i didnt notice too so dont blame on yourself
RxvxntxNOP
I marked in solved , thanks buddy
glad its solved still check that transition is not that hard gl