|
2 | 2 |
|
3 | 3 | import { useThemeState, useThemeActions } from "@/lib/theme-context"; |
4 | 4 | import { useCallback } from "react"; |
| 5 | +import { motion, AnimatePresence } from "framer-motion"; |
5 | 6 |
|
6 | 7 | export default function ThemeToggle() { |
7 | 8 | const { theme, resolvedTheme, isMounted, isLoading, error } = useThemeState(); |
@@ -49,74 +50,98 @@ export default function ThemeToggle() { |
49 | 50 | title={getTitle()} |
50 | 51 | disabled={isLoading} |
51 | 52 | > |
52 | | - {error ? ( |
53 | | - <svg |
54 | | - xmlns="http://www.w3.org/2000/svg" |
55 | | - fill="none" |
56 | | - viewBox="0 0 24 24" |
57 | | - strokeWidth={1.5} |
58 | | - stroke="currentColor" |
59 | | - className="h-5 w-5 text-red-500" |
60 | | - > |
61 | | - <path |
62 | | - strokeLinecap="round" |
63 | | - strokeLinejoin="round" |
64 | | - d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" |
65 | | - /> |
66 | | - </svg> |
67 | | - ) : theme === "light" ? ( |
68 | | - <svg |
69 | | - xmlns="http://www.w3.org/2000/svg" |
70 | | - fill="none" |
71 | | - viewBox="0 0 24 24" |
72 | | - strokeWidth={1.5} |
73 | | - stroke="currentColor" |
74 | | - className="h-5 w-5 text-amber-500 transition-colors" |
75 | | - > |
76 | | - <path |
77 | | - strokeLinecap="round" |
78 | | - strokeLinejoin="round" |
79 | | - d="M12 3v2.25m6.364.386l-1.591 1.591M21 12h-2.25m-.386 6.364l-1.591-1.591M12 18.75V21m-4.773-4.227l-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" |
80 | | - /> |
81 | | - </svg> |
82 | | - ) : theme === "dark" ? ( |
83 | | - <svg |
84 | | - xmlns="http://www.w3.org/2000/svg" |
85 | | - fill="none" |
86 | | - viewBox="0 0 24 24" |
87 | | - strokeWidth={1.5} |
88 | | - stroke="currentColor" |
89 | | - className="h-5 w-5 text-accent transition-colors" |
90 | | - > |
91 | | - <path |
92 | | - strokeLinecap="round" |
93 | | - strokeLinejoin="round" |
94 | | - d="M21.752 15.002A9.718 9.718 0 0118 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 003 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 009.002-5.998z" |
95 | | - /> |
96 | | - </svg> |
97 | | - ) : ( |
98 | | - <div className="relative flex items-center justify-center"> |
99 | | - <svg |
| 53 | + <AnimatePresence mode="wait" initial={false}> |
| 54 | + {error ? ( |
| 55 | + <motion.svg |
| 56 | + key="error" |
| 57 | + initial={{ scale: 0.5, opacity: 0, rotate: -45 }} |
| 58 | + animate={{ scale: 1, opacity: 1, rotate: 0 }} |
| 59 | + exit={{ scale: 0.5, opacity: 0, rotate: 45 }} |
| 60 | + transition={{ duration: 0.2 }} |
100 | 61 | xmlns="http://www.w3.org/2000/svg" |
101 | 62 | fill="none" |
102 | 63 | viewBox="0 0 24 24" |
103 | 64 | strokeWidth={1.5} |
104 | 65 | stroke="currentColor" |
105 | | - className="h-5 w-5 text-slate-400 transition-colors" |
| 66 | + className="h-5 w-5 text-red-500" |
106 | 67 | > |
107 | 68 | <path |
108 | 69 | strokeLinecap="round" |
109 | 70 | strokeLinejoin="round" |
110 | | - d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" |
| 71 | + d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" |
111 | 72 | /> |
112 | | - </svg> |
113 | | - <div className="absolute -bottom-0.5 -right-0.5 flex h-2 w-2 items-center justify-center"> |
114 | | - <div className={`h-1.5 w-1.5 rounded-full transition-colors ${ |
115 | | - resolvedTheme === 'dark' ? 'bg-accent' : 'bg-amber-500' |
116 | | - }`} /> |
117 | | - </div> |
118 | | - </div> |
119 | | - )} |
| 73 | + </motion.svg> |
| 74 | + ) : theme === "light" ? ( |
| 75 | + <motion.svg |
| 76 | + key="light" |
| 77 | + initial={{ scale: 0.5, opacity: 0, rotate: -45 }} |
| 78 | + animate={{ scale: 1, opacity: 1, rotate: 0 }} |
| 79 | + exit={{ scale: 0.5, opacity: 0, rotate: 45 }} |
| 80 | + transition={{ duration: 0.2 }} |
| 81 | + xmlns="http://www.w3.org/2000/svg" |
| 82 | + fill="none" |
| 83 | + viewBox="0 0 24 24" |
| 84 | + strokeWidth={1.5} |
| 85 | + stroke="currentColor" |
| 86 | + className="h-5 w-5 text-amber-500 transition-colors" |
| 87 | + > |
| 88 | + <path |
| 89 | + strokeLinecap="round" |
| 90 | + strokeLinejoin="round" |
| 91 | + d="M12 3v2.25m6.364.386l-1.591 1.591M21 12h-2.25m-.386 6.364l-1.591-1.591M12 18.75V21m-4.773-4.227l-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0z" |
| 92 | + /> |
| 93 | + </motion.svg> |
| 94 | + ) : theme === "dark" ? ( |
| 95 | + <motion.svg |
| 96 | + key="dark" |
| 97 | + initial={{ scale: 0.5, opacity: 0, rotate: -45 }} |
| 98 | + animate={{ scale: 1, opacity: 1, rotate: 0 }} |
| 99 | + exit={{ scale: 0.5, opacity: 0, rotate: 45 }} |
| 100 | + transition={{ duration: 0.2 }} |
| 101 | + xmlns="http://www.w3.org/2000/svg" |
| 102 | + fill="none" |
| 103 | + viewBox="0 0 24 24" |
| 104 | + strokeWidth={1.5} |
| 105 | + stroke="currentColor" |
| 106 | + className="h-5 w-5 text-accent transition-colors" |
| 107 | + > |
| 108 | + <path |
| 109 | + strokeLinecap="round" |
| 110 | + strokeLinejoin="round" |
| 111 | + d="M21.752 15.002A9.718 9.718 0 0118 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 003 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 009.002-5.998z" |
| 112 | + /> |
| 113 | + </motion.svg> |
| 114 | + ) : ( |
| 115 | + <motion.div |
| 116 | + key="system" |
| 117 | + initial={{ scale: 0.5, opacity: 0, rotate: -45 }} |
| 118 | + animate={{ scale: 1, opacity: 1, rotate: 0 }} |
| 119 | + exit={{ scale: 0.5, opacity: 0, rotate: 45 }} |
| 120 | + transition={{ duration: 0.2 }} |
| 121 | + className="relative flex items-center justify-center" |
| 122 | + > |
| 123 | + <svg |
| 124 | + xmlns="http://www.w3.org/2000/svg" |
| 125 | + fill="none" |
| 126 | + viewBox="0 0 24 24" |
| 127 | + strokeWidth={1.5} |
| 128 | + stroke="currentColor" |
| 129 | + className="h-5 w-5 text-slate-400 transition-colors" |
| 130 | + > |
| 131 | + <path |
| 132 | + strokeLinecap="round" |
| 133 | + strokeLinejoin="round" |
| 134 | + d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" |
| 135 | + /> |
| 136 | + </svg> |
| 137 | + <div className="absolute -bottom-0.5 -right-0.5 flex h-2 w-2 items-center justify-center"> |
| 138 | + <div className={`h-1.5 w-1.5 rounded-full transition-colors ${ |
| 139 | + resolvedTheme === 'dark' ? 'bg-accent' : 'bg-amber-500' |
| 140 | + }`} /> |
| 141 | + </div> |
| 142 | + </motion.div> |
| 143 | + )} |
| 144 | + </AnimatePresence> |
120 | 145 | </button> |
121 | 146 | ); |
122 | 147 | } |
0 commit comments