Skip to content

Commit

Permalink
fix(web): Fix the toggle theme button allowing clicks from anywhere i…
Browse files Browse the repository at this point in the history
…n the dropdown item
  • Loading branch information
MohamedBassem committed Apr 9, 2024
1 parent cae543c commit 5ab6c33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
14 changes: 8 additions & 6 deletions apps/web/components/dashboard/sidebar/SidebarProfileOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { useToggleTheme } from "@/components/theme-provider";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
Expand All @@ -13,28 +14,29 @@ import { signOut } from "next-auth/react";
import { useTheme } from "next-themes";

function DarkModeToggle() {
const { theme, setTheme } = useTheme();
const { theme } = useTheme();

let comp;
if (theme == "dark") {
comp = (
<button onClick={() => setTheme("light")}>
<span>
<Sun className="size-4" />
<p>Light Mode</p>
</button>
</span>
);
} else {
comp = (
<button onClick={() => setTheme("dark")}>
<span>
<Moon className="size-4" />
<p>Dark Mode</p>
</button>
</span>
);
}
return <Slot className="flex flex-row gap-2">{comp}</Slot>;
}

export default function SidebarProfileOptions() {
const toggleTheme = useToggleTheme();
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
Expand All @@ -43,7 +45,7 @@ export default function SidebarProfileOptions() {
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-fit">
<DropdownMenuItem>
<DropdownMenuItem onClick={toggleTheme}>
<DarkModeToggle />
</DropdownMenuItem>
<DropdownMenuItem
Expand Down
11 changes: 10 additions & 1 deletion apps/web/components/theme-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

import type { ThemeProviderProps } from "next-themes/dist/types";
import * as React from "react";
import { ThemeProvider as NextThemesProvider } from "next-themes";
import { ThemeProvider as NextThemesProvider, useTheme } from "next-themes";

export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
}

export function useToggleTheme() {
const { theme, setTheme } = useTheme();
if (theme == "dark") {
return () => setTheme("light");
} else {
return () => setTheme("dark");
}
}

0 comments on commit 5ab6c33

Please sign in to comment.