-
-
Notifications
You must be signed in to change notification settings - Fork 26
Refactor: Replace dropdown theme menu with single-click toggle button… #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yogeshkumawat2027
wants to merge
1
commit into
StabilityNexus:main
Choose a base branch
from
yogeshkumawat2027:feature/enhanced-theme-toggle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,103 @@ | ||
| "use client"; | ||
|
|
||
| import { Moon, Sun } from "lucide-react"; | ||
| import { Moon, Sun, Monitor } from "lucide-react"; | ||
| import { useTheme } from "next-themes"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| DropdownMenu, | ||
| DropdownMenuContent, | ||
| DropdownMenuItem, | ||
| DropdownMenuTrigger, | ||
| } from "@/components/ui/dropdown-menu"; | ||
| Tooltip, | ||
| TooltipContent, | ||
| TooltipProvider, | ||
| TooltipTrigger, | ||
| } from "@/components/ui/tooltip"; | ||
|
|
||
| export function ModeToggle() { | ||
| const { setTheme } = useTheme(); | ||
| const { theme, setTheme, resolvedTheme } = useTheme(); | ||
| const [mounted, setMounted] = useState(false); | ||
|
|
||
| // Prevent hydration mismatch | ||
| useEffect(() => { | ||
| setMounted(true); | ||
| }, []); | ||
|
|
||
| if (!mounted) { | ||
| return ( | ||
| <Button | ||
| variant="ghost" | ||
| size="icon" | ||
| className="h-9 w-9" | ||
| disabled | ||
| > | ||
| <Sun className="h-[1.2rem] w-[1.2rem]" /> | ||
| </Button> | ||
| ); | ||
| } | ||
|
|
||
| // Determine the next theme to cycle through | ||
| const nextTheme = () => { | ||
| if (theme === "light") { | ||
| setTheme("dark"); | ||
| } else if (theme === "dark") { | ||
| setTheme("system"); | ||
| } else { | ||
| setTheme("light"); | ||
| } | ||
| }; | ||
|
|
||
| // Determine which icon to display based on resolved theme | ||
| const renderIcon = () => { | ||
| switch (resolvedTheme) { | ||
| case "dark": | ||
| return ( | ||
| <Moon className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> | ||
| ); | ||
| case "light": | ||
| return ( | ||
| <Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> | ||
| ); | ||
| default: | ||
| return ( | ||
| <Monitor className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| // Determine tooltip text | ||
| const getTooltipText = () => { | ||
| switch (theme) { | ||
| case "light": | ||
| return "Light mode (click to switch to Dark)"; | ||
| case "dark": | ||
| return "Dark mode (click to switch to System)"; | ||
| case "system": | ||
| return `System theme - ${resolvedTheme === "dark" ? "Dark" : "Light"} (click to switch to Light)`; | ||
| default: | ||
| return "Toggle theme"; | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <DropdownMenu> | ||
| <DropdownMenuTrigger asChild> | ||
| <Button variant="ghost" size="icon" className="h-9 w-9"> | ||
| <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"> | ||
| <DropdownMenuItem onClick={() => setTheme("light")}> | ||
| Light | ||
| </DropdownMenuItem> | ||
| <DropdownMenuItem onClick={() => setTheme("dark")}> | ||
| Dark | ||
| </DropdownMenuItem> | ||
| <DropdownMenuItem onClick={() => setTheme("system")}> | ||
| System | ||
| </DropdownMenuItem> | ||
| </DropdownMenuContent> | ||
| </DropdownMenu> | ||
| <TooltipProvider> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant="ghost" | ||
| size="icon" | ||
| className="h-9 w-9 relative transition-all duration-200 hover:bg-accent" | ||
| onClick={nextTheme} | ||
| aria-label={`Current theme: ${theme}. Click to cycle through themes.`} | ||
| > | ||
| {renderIcon()} | ||
| <span className="sr-only"> | ||
| {theme === "light" ? "Light" : theme === "dark" ? "Dark" : "System"} theme | ||
| </span> | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent side="bottom" className="text-xs"> | ||
| {getTooltipText()} | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| </TooltipProvider> | ||
| ); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Monitor icon will never display -
resolvedThemeis never "system".The
resolvedThemefromnext-themesalways resolves to the actual applied theme ("light"or"dark"), never"system". When the user selects system mode,resolvedThemereflects the OS preference. This means thedefaultcase never executes and the Monitor icon is unreachable.To show Monitor when the user has selected system theme, use
themeinstead:// Determine which icon to display based on resolved theme const renderIcon = () => { - switch (resolvedTheme) { + switch (theme) { + case "system": + return ( + <Monitor className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> + ); case "dark": return ( <Moon className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> ); - case "light": - return ( - <Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> - ); default: return ( - <Monitor className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> + <Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all duration-300" /> ); } };Alternatively, if you want the icon to reflect the actual appearance when in system mode (Sun/Moon based on OS) while showing Monitor only as a deliberate design choice when
theme === "system", adjust the logic accordingly.📝 Committable suggestion
🤖 Prompt for AI Agents