pacocoursey / next-themes

Perfect Next.js dark mode in 2 lines of code. Support System preference and any other theme with no flashing

Home Page:https://next-themes-example.vercel.app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useTheme doesn't work in monorepo ui library.

4Dmu opened this issue · comments

commented

It appears that the useTheme hook doesn't work when used in a monorepo ui library and then imported in a nextjs project.

Here is a shadcn ui component that uses setTheme:

"use client";
import { Moon, Sun } from "lucide-react";
import { useTheme } from "next-themes";
import { Button } from "./button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "./dropdown-menu";

export function ThemeToggle({
  setTheme: st,
}: {
  setTheme?: (theme: string) => void;
}) {
  const { setTheme } = useTheme();

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="outline" size="icon">
          <Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
          <Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
          <span className="sr-only">Toggle theme</span>
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end" className="">
        <DropdownMenuItem onClick={() => st?.("light")}>Light</DropdownMenuItem>
        <DropdownMenuItem onClick={() => st?.("dark")}>Dark</DropdownMenuItem>
        <DropdownMenuItem onClick={() => st?.("system")}>
          System
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}

When I use setTheme from the hook nothing happens, but If I pass a setTheme function as props and then call the useTheme hook inside my next-app and pass in setTheme everything works as expected. In case it's relevant I'm using turborepo and pnpm.

Hey @4Dmu, here's an example of useTheme working in a monorepo (specifically Turborepo). Hope this is helpful.