11"use client"
22
33import * as React from "react"
4- import { Laptop , Moon , Sun } from "lucide-react"
4+ import { Moon , Sun , SunMoon } from "lucide-react"
55import { useTheme } from "next-themes"
66import { useTranslations } from 'next-intl'
7-
8- import { Button } from "@/components/ui/button"
97import {
108 DropdownMenu ,
119 DropdownMenuContent ,
1210 DropdownMenuItem ,
1311 DropdownMenuTrigger ,
1412} from "@/components/ui/dropdown-menu"
15- import { SidebarMenuButton } from "./ui/sidebar"
13+ import { SidebarMenuButton } from "@/components/ui/sidebar" ;
14+ import { Button } from "@/components/ui/button" ;
1615
17- function Toggle ( ) {
16+ export function ModeToggle ( ) {
1817 const t = useTranslations ( ) ;
1918 const { theme, setTheme } = useTheme ( )
2019 const [ open , setOpen ] = React . useState ( false )
2120
22- // 循环切换主题:light -> dark -> system -> light
23- const cycleTheme = ( ) => {
24- if ( theme === "light" ) setTheme ( "dark" )
25- else if ( theme === "dark" ) setTheme ( "system" )
26- else setTheme ( "light" )
27- }
28-
2921 return (
3022 < DropdownMenu open = { open } onOpenChange = { setOpen } >
3123 < DropdownMenuTrigger asChild >
32- < Button
33- variant = "ghost"
34- size = "sm"
35- onClick = { cycleTheme }
24+ < SidebarMenuButton asChild className = "md:h-8 md:p-0"
25+ tooltip = { {
26+ children : t ( 'common.theme' ) ,
27+ hidden : false ,
28+ } }
3629 >
37- < Sun className = "h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
38- < Moon className = "absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
39- < span className = "sr-only" > { t ( 'common.theme' ) } </ span >
40- </ Button >
30+ < a href = "#" >
31+ < div className = "flex size-8 items-center justify-center rounded-lg" >
32+ < Button
33+ variant = "ghost"
34+ size = "sm"
35+ >
36+ < ThemeIcon theme = { theme } />
37+ </ Button >
38+ </ div >
39+ </ a >
40+ </ SidebarMenuButton >
4141 </ DropdownMenuTrigger >
42- < DropdownMenuContent align = "end" >
42+ < DropdownMenuContent align = "end" side = "right" >
4343 < DropdownMenuItem onClick = { ( ) => setTheme ( "light" ) } >
4444 < Sun className = "mr-2 h-4 w-4" />
45- < span > { t ( 'common.light' ) } </ span >
45+ < span > { t ( 'common.light' ) } { theme === "light" && "✓" } </ span >
4646 </ DropdownMenuItem >
4747 < DropdownMenuItem onClick = { ( ) => setTheme ( "dark" ) } >
4848 < Moon className = "mr-2 h-4 w-4" />
49- < span > { t ( 'common.dark' ) } </ span >
49+ < span > { t ( 'common.dark' ) } { theme === "dark" && "✓" } </ span >
5050 </ DropdownMenuItem >
5151 < DropdownMenuItem onClick = { ( ) => setTheme ( "system" ) } >
52- < Laptop className = "mr-2 h-4 w-4" />
53- < span > { t ( 'common.system' ) } </ span >
52+ < SunMoon className = "mr-2 h-4 w-4" />
53+ < span > { t ( 'common.system' ) } { theme === "system" && "✓" } </ span >
5454 </ DropdownMenuItem >
5555 </ DropdownMenuContent >
5656 </ DropdownMenu >
5757 )
5858}
5959
60- export function ModeToggle ( ) {
61- const t = useTranslations ( ) ;
62- return (
63- < SidebarMenuButton asChild className = "md:h-8 md:p-0"
64- tooltip = { {
65- children : t ( 'common.theme' ) ,
66- hidden : false ,
67- } }
68- >
69- < a href = "#" >
70- < div className = "flex size-8 items-center justify-center rounded-lg" >
71- < Toggle />
72- </ div >
73- </ a >
74- </ SidebarMenuButton >
75- )
76- }
60+ function ThemeIcon ( { theme } : { theme ?: string } ) {
61+ switch ( theme ) {
62+ case "light" :
63+ return < Sun />
64+ case "dark" :
65+ return < Moon />
66+ case "system" :
67+ return < SunMoon />
68+ default :
69+ return < SunMoon />
70+ }
71+ }
0 commit comments