Next.js Discord

Discord Forum

Dark/Light simple animation toggle issue

Answered
RxvxntxN posted this in #help-forum
Open in Discord
Hello

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>
  );
}
Answered by Nuitari
View full answer

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> ?
I want to show a record , with both simple and original one to show
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>
I shall try to be consistent.
Ooof , i did mistake , for Simple one i did use button instead of shadcn Button
yeah thats why im confused :(
so we now know, <button> works but not <Button> :ChocoHmm:
what is inside <Button> ?
@ᴉuɐpɹɐɐ so we now know, <button> works but not <Button> <:ChocoHmm:932251629212794950>
Button also works on simple one , just tested
nice observation!
I am trying hard to find what am i doing wrong , i am thinking to wrap it with more div
maybe 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
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
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
yeah , i will give there one navigation page
including simple one that works
I made it minimal as possible
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);
      });
    });
  };
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
im using your repo
@Nuitari im using your repo
this one is testing , it will work as i mentioned.
i am talking about this one
ok didnt notice gonna check
@RxvxntxN this one is testing , it will work as i mentioned.
Answer
you notice that?
@Nuitari Click to see attachment
Oh right , I didn't find that lmfao , congrates , you solved it officially
i didnt notice too so dont blame on yourself
I marked in solved , thanks buddy
glad its solved still check that transition is not that hard gl