|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { FC, useState, useEffect, useCallback } from "react" |
| 4 | +import { cn } from "@/lib/utils" |
| 5 | +import Image from "next/image" |
| 6 | + |
| 7 | +export interface ThemePreview { |
| 8 | + chromeBorder: string |
| 9 | + bgBody: string |
| 10 | + bgSidebar: string |
| 11 | + icon: string |
| 12 | + heading: string |
| 13 | +} |
| 14 | + |
| 15 | +export interface ThemeInfo { |
| 16 | + name: string |
| 17 | + label: string |
| 18 | + image: string |
| 19 | + preview: ThemePreview |
| 20 | +} |
| 21 | + |
| 22 | +const PaletteIcon: FC<{ className?: string }> = ({ className }) => ( |
| 23 | + <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" className={className}> |
| 24 | + <path fill="currentColor" d="M17.5 12a1.5 1.5 0 0 1-1.5-1.5A1.5 1.5 0 0 1 17.5 9a1.5 1.5 0 0 1 1.5 1.5a1.5 1.5 0 0 1-1.5 1.5m-3-4A1.5 1.5 0 0 1 13 6.5A1.5 1.5 0 0 1 14.5 5A1.5 1.5 0 0 1 16 6.5A1.5 1.5 0 0 1 14.5 8m-5 0A1.5 1.5 0 0 1 8 6.5A1.5 1.5 0 0 1 9.5 5A1.5 1.5 0 0 1 11 6.5A1.5 1.5 0 0 1 9.5 8m-3 4A1.5 1.5 0 0 1 5 10.5A1.5 1.5 0 0 1 6.5 9A1.5 1.5 0 0 1 8 10.5A1.5 1.5 0 0 1 6.5 12M12 3a9 9 0 0 0-9 9a9 9 0 0 0 9 9a1.5 1.5 0 0 0 1.5-1.5c0-.39-.15-.74-.39-1c-.23-.27-.38-.62-.38-1a1.5 1.5 0 0 1 1.5-1.5H16a5 5 0 0 0 5-5c0-4.42-4.03-8-9-8" /> |
| 25 | + </svg> |
| 26 | +) |
| 27 | + |
| 28 | +const lightThemes = [ |
| 29 | + { name: "light_slate", label: "Light Slate", image: "/img/themes/light_slate.webp", preview: { |
| 30 | + "chromeBorder": "border-gray-200", |
| 31 | + "bgBody": "bg-white", |
| 32 | + "bgSidebar": "bg-gray-50", |
| 33 | + "icon": "text-gray-500", |
| 34 | + "heading": "text-gray-900" |
| 35 | + } }, |
| 36 | + { name: "soft_pink", label: "Soft Pink", image: "/img/themes/soft_pink.webp", preview: { |
| 37 | + "chromeBorder": "border-pink-200", |
| 38 | + "bgBody": "bg-pink-50", |
| 39 | + "bgSidebar": "bg-pink-100", |
| 40 | + "icon": "text-pink-500", |
| 41 | + "heading": "text-pink-900" |
| 42 | + } }, |
| 43 | + { name: "light_sky", label: "Light Sky", image: "/img/themes/light_sky.webp", preview: { |
| 44 | + "chromeBorder": "border-sky-200", |
| 45 | + "bgBody": "bg-sky-50", |
| 46 | + "bgSidebar": "bg-sky-100", |
| 47 | + "icon": "text-sky-500", |
| 48 | + "heading": "text-sky-900" |
| 49 | + } }, |
| 50 | + { name: "light", label: "Light", image: "/img/themes/light.webp", preview: { |
| 51 | + "chromeBorder": "border-gray-200", |
| 52 | + "bgBody": "bg-white", |
| 53 | + "bgSidebar": "bg-gray-50", |
| 54 | + "icon": "text-gray-500", |
| 55 | + "heading": "text-gray-900" |
| 56 | + } }, |
| 57 | +] |
| 58 | + |
| 59 | +const darkThemes = [ |
| 60 | + { name: "nord", label: "Nord", image: "/img/themes/nord.webp", preview: { |
| 61 | + "chromeBorder": "border-frost-700/50", |
| 62 | + "bgBody": "bg-nord-900", |
| 63 | + "bgSidebar": "bg-nord-800", |
| 64 | + "icon": "text-nord-300", |
| 65 | + "heading": "text-nord-200" |
| 66 | + } }, |
| 67 | + { name: "matrix", label: "Matrix", image: "/img/themes/matrix.webp", preview: { |
| 68 | + "chromeBorder": "border-green-500/50", |
| 69 | + "bgBody": "bg-black", |
| 70 | + "bgSidebar": "bg-green-950/80", |
| 71 | + "icon": "text-green-500", |
| 72 | + "heading": "text-green-400 font-mono" |
| 73 | + } }, |
| 74 | + { name: "blue_smoke", label: "Blue Smoke", image: "/img/themes/blue_smoke.webp", preview: { |
| 75 | + "chromeBorder": "border-blue-500/60", |
| 76 | + "bgBody": "bg-gray-950", |
| 77 | + "bgSidebar": "bg-gray-950", |
| 78 | + "icon": "text-blue-400", |
| 79 | + "heading": "text-blue-100" |
| 80 | + } }, |
| 81 | + { name: "dark", label: "Dark", image: "/img/themes/dark.webp", preview: { |
| 82 | + "chromeBorder": "border-gray-700", |
| 83 | + "bgBody": "bg-gray-900", |
| 84 | + "bgSidebar": "bg-gray-800/50", |
| 85 | + "icon": "text-gray-400", |
| 86 | + "heading": "text-indigo-200" |
| 87 | + } }, |
| 88 | +] |
| 89 | + |
| 90 | +const themes: ThemeInfo[] = [ |
| 91 | + ...darkThemes, |
| 92 | + ...lightThemes, |
| 93 | +] |
| 94 | + |
| 95 | +interface ThemeCarouselProps { |
| 96 | + autoPlayInterval?: number |
| 97 | + compact?: boolean |
| 98 | + className?: string |
| 99 | +} |
| 100 | + |
| 101 | +export const ThemeCarousel: FC<ThemeCarouselProps> = ({ |
| 102 | + autoPlayInterval = 10000, |
| 103 | + compact, |
| 104 | + className, |
| 105 | +}) => { |
| 106 | + const [currentIndex, setCurrentIndex] = useState(0) |
| 107 | + const [isPaused, setIsPaused] = useState(false) |
| 108 | + |
| 109 | + const goToSlide = useCallback((index: number) => { |
| 110 | + setCurrentIndex(index) |
| 111 | + }, []) |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + if (isPaused || themes.length <= 1) return |
| 115 | + |
| 116 | + const interval = setInterval(() => { |
| 117 | + setCurrentIndex((prev) => (prev + 1) % themes.length) |
| 118 | + }, autoPlayInterval) |
| 119 | + |
| 120 | + return () => clearInterval(interval) |
| 121 | + }, [isPaused, autoPlayInterval, currentIndex]) |
| 122 | + |
| 123 | + const current = themes[currentIndex] |
| 124 | + |
| 125 | + return ( |
| 126 | + <div |
| 127 | + className={cn("not-prose relative w-full", className)} |
| 128 | + onMouseEnter={() => setIsPaused(true)} |
| 129 | + onMouseLeave={() => setIsPaused(false)} |
| 130 | + > |
| 131 | + {/* Main image */} |
| 132 | + <div className="relative rounded-xl overflow-hidden shadow-2xl border border-slate-200 dark:border-slate-700"> |
| 133 | + <div className="relative w-full aspect-[2240/2228]"> |
| 134 | + {themes.map((theme, index) => ( |
| 135 | + <Image |
| 136 | + key={theme.name} |
| 137 | + src={theme.image} |
| 138 | + alt={`${theme.label} theme`} |
| 139 | + fill |
| 140 | + className={cn( |
| 141 | + "object-cover transition-opacity duration-500", |
| 142 | + index === currentIndex ? "opacity-100" : "opacity-0" |
| 143 | + )} |
| 144 | + priority={index === 0} |
| 145 | + sizes="(max-width: 768px) 100vw, (max-width: 1280px) 80vw, 1024px" |
| 146 | + /> |
| 147 | + ))} |
| 148 | + </div> |
| 149 | + {/* Theme name badge */} |
| 150 | + <div className="absolute bottom-4 left-4 px-3 py-1.5 bg-black/60 backdrop-blur-sm rounded-lg text-white text-sm font-medium"> |
| 151 | + {current.label} |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + |
| 155 | + {/* Theme selectors */} |
| 156 | + <div className={cn("mt-6 flex flex-col gap-3", !compact && "items-center")}> |
| 157 | + {[darkThemes, lightThemes].map((group, groupIndex) => ( |
| 158 | + <div key={groupIndex} className={cn("flex gap-2", !compact && "justify-center")}> |
| 159 | + {group.map((theme) => { |
| 160 | + const index = themes.indexOf(theme) |
| 161 | + const p = theme.preview |
| 162 | + return ( |
| 163 | + <button type="button" |
| 164 | + key={theme.name} |
| 165 | + onClick={() => goToSlide(index)} |
| 166 | + className={cn( |
| 167 | + "rounded-lg border overflow-hidden transition-all duration-300 flex items-stretch", |
| 168 | + compact ? "w-40" : "w-44", |
| 169 | + p.bgBody, p.chromeBorder, |
| 170 | + index === currentIndex |
| 171 | + ? "shadow-lg scale-105" |
| 172 | + : "hover:scale-[1.02]" |
| 173 | + )} |
| 174 | + aria-label={`Select ${theme.label} theme`} |
| 175 | + > |
| 176 | + <div className={cn("flex items-center justify-center px-2.5", p.bgSidebar)}> |
| 177 | + <PaletteIcon className={cn("size-5", p.icon)} /> |
| 178 | + </div> |
| 179 | + <span className={cn("text-sm font-medium truncate flex-1 px-3 py-2", compact && "text-left", p.heading)}> |
| 180 | + {theme.label} |
| 181 | + </span> |
| 182 | + {!compact && ( |
| 183 | + <div className={cn("flex items-center pr-2.5", p.icon)}> |
| 184 | + <svg className={cn( |
| 185 | + "size-2 flex-shrink-0 transition-all duration-300", |
| 186 | + index === currentIndex ? "scale-100" : "scale-0" |
| 187 | + )} viewBox="0 0 8 8"> |
| 188 | + <circle cx="4" cy="4" r="4" fill="currentColor" /> |
| 189 | + </svg> |
| 190 | + </div> |
| 191 | + )} |
| 192 | + </button> |
| 193 | + ) |
| 194 | + })} |
| 195 | + </div> |
| 196 | + ))} |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + ) |
| 200 | +} |
0 commit comments