shadcn-ui / ui

Beautifully designed components that you can copy and paste into your apps. Accessible. Customizable. Open Source.
https://ui.shadcn.com
MIT License
62.96k stars 3.54k forks source link

Icon does not change to reflect theme setting #26

Open kariemoorman opened 1 year ago

kariemoorman commented 1 year ago

When transitioning from light to dark mode the associated icon stays fixed as sun, instead of switching to moon icon (to reflect the current theme setting).

Zebeqo commented 1 year ago

https://github.com/shadcn/ui/pull/4

kuldeepswnkumar commented 1 year ago

Hello @kariemoorman. I would like to work on this.

agmmnn commented 1 year ago

This is my implementation in tauri-ui-boilerplate.

export function ThemeToggle() {
  const { setTheme, theme, systemTheme } = useTheme()
  const [currentTheme, setCurrentTheme] = useState("")

  useEffect(() => {
    setCurrentTheme(
      theme === "system"
        ? systemTheme === "light"
          ? "light"
          : "dark"
        : theme === "light"
        ? "light"
        : "dark"
    )
  }, [theme, systemTheme])

  const ThemeIcon = Icons[currentTheme === "light" ? "sun" : "moon"]

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="ghost" size="sm">
          <ThemeIcon className="hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-100" />
...

https://github.com/agmmnn/tauri-ui-boilerplate/blob/master/src/components/theme-toggle.tsx

ahosny752 commented 2 months ago

I found a solution to this. Its due to hydration mismatch for. Refer to this doc https://github.com/pacocoursey/next-themes#avoid-hydration-mismatch