@@ -4,6 +4,7 @@ import { zodResolver } from "@hookform/resolvers/zod"
44import html2canvas from "html2canvas"
55import { Cog , Info } from "lucide-react"
66import Image from "next/image"
7+ import { useEffect } from "react"
78import { useForm } from "react-hook-form"
89import * as z from "zod"
910import AppleMusic from "@/components/icons/apple-music"
@@ -36,6 +37,7 @@ const formSchema = z.object({
3637 subTitle : z . string ( ) ,
3738 footer : z . string ( ) ,
3839 gradient : z . string ( ) ,
40+ color : z . string ( ) ,
3941} )
4042
4143export default function Home ( ) {
@@ -48,8 +50,11 @@ export default function Home() {
4850 subTitle : "" ,
4951 footer : "" ,
5052 gradient : "0" ,
53+ color : "" ,
5154 } ) ,
5255 } )
56+ const { dirtyFields } = form . formState
57+ const isEdited = dirtyFields . bigTitle ?? dirtyFields . subTitle ?? dirtyFields . footer
5358
5459 const handleDownload = ( ) => {
5560 const element = document . getElementById ( "coverElement" )
@@ -63,6 +68,10 @@ export default function Home() {
6368 }
6469 }
6570
71+ useEffect ( ( ) => {
72+ form . setValue ( "color" , "" )
73+ } , [ form . watch ( "gradient" ) ] )
74+
6675 return (
6776 < main className = "flex justify-center" >
6877 < style jsx global > { `
@@ -112,7 +121,9 @@ export default function Home() {
112121 < div id = "coverElement" style = { { width : 300 , height : 300 , position : "relative" } } >
113122 { /* Background Image */ }
114123 < Image
115- src = { `/assets/gradients/${ form . watch ( "gradient" ) } .png` }
124+ src = { `/assets/${
125+ form . watch ( "color" ) === "" ? `gradients/${ form . watch ( "gradient" ) } ` : `colors/${ form . watch ( "color" ) } `
126+ } .png`}
116127 loading = "eager"
117128 alt = { `Gradient ${ form . watch ( "gradient" ) } ` }
118129 layout = "fill"
@@ -127,15 +138,19 @@ export default function Home() {
127138 ) }
128139 { /* Big Title */ }
129140 < div style = { { position : "absolute" , top : 55 , left : 25 } } >
130- < h1 className = "text-[3em] font-semibold text-white" > { form . watch ( "bigTitle" ) } </ h1 >
141+ < h1 className = "text-[3em] font-semibold text-white" >
142+ { isEdited ? form . watch ( "bigTitle" ) : "Big Title" }
143+ </ h1 >
131144 </ div >
132145 { /* Sub Title */ }
133146 < div style = { { position : "absolute" , top : 105 , left : 25 } } >
134- < h2 className = "text-[2.5em] font-thin text-white" > { form . watch ( "subTitle" ) } </ h2 >
147+ < h2 className = "text-[2.5em] font-thin text-white" >
148+ { isEdited ? form . watch ( "subTitle" ) : "Sub Title" }
149+ </ h2 >
135150 </ div >
136151 { /* Footer */ }
137152 < div style = { { position : "absolute" , bottom : 25 , left : 25 } } >
138- < h3 className = "text-sm text-slate-300 " > { form . watch ( "footer" ) } </ h3 >
153+ < h3 className = "text-sm text-white text-opacity-60 " > { isEdited ? form . watch ( "footer" ) : "Footer" } </ h3 >
139154 </ div >
140155 </ div >
141156 </ div >
@@ -163,7 +178,11 @@ export default function Home() {
163178 render = { ( { field } ) => (
164179 < FormItem >
165180 < FormControl >
166- < Switch checked = { field . value } onCheckedChange = { field . onChange } title = "Toggle Apple Music Logo" />
181+ < Switch
182+ checked = { field . value }
183+ onCheckedChange = { field . onChange }
184+ title = "Toggle Apple Music Logo"
185+ />
167186 </ FormControl >
168187 </ FormItem >
169188 ) }
@@ -215,7 +234,6 @@ export default function Home() {
215234 < TabsTrigger value = "color" className = "w-full" >
216235 Color
217236 </ TabsTrigger >
218- < Separator orientation = "vertical" />
219237 < TabsTrigger value = "image" className = "w-full" >
220238 Image
221239 </ TabsTrigger >
@@ -232,14 +250,19 @@ export default function Home() {
232250 defaultValue = { field . value }
233251 className = "grid grid-cols-5 gap-4"
234252 >
235- { [ ...Array ( 30 ) ] . map ( ( _ , index ) => (
253+ { [ ...Array ( 40 ) ] . map ( ( _ , index ) => (
236254 < Label
237255 key = { index }
238256 htmlFor = { `${ index } ` }
239257 className = "rounded-full border-2 border-muted bg-popover p-0.5 hover:cursor-pointer hover:bg-accent hover:text-accent-foreground [&:has([data-state=checked])]:border-primary"
240258 >
241259 < Avatar className = "h-full w-full" >
242- < RadioGroupItem value = { `${ index } ` } id = { `${ index } ` } className = "sr-only" title = { `Gradient ${ index } ` } />
260+ < RadioGroupItem
261+ value = { `${ index } ` }
262+ id = { `${ index } ` }
263+ className = "sr-only"
264+ title = { `Gradient ${ index } ` }
265+ />
243266 < Image
244267 src = { `/assets/gradients/${ index } .png` }
245268 alt = { `Gradient ${ index } ` }
@@ -256,11 +279,45 @@ export default function Home() {
256279 />
257280 </ TabsContent >
258281 < TabsContent value = "color" className = "mt-4" >
259- < Alert >
260- < Info className = "h-4 w-4" />
261- < AlertTitle > Coming soon</ AlertTitle >
262- < AlertDescription > Color selection will be available soon.</ AlertDescription >
263- </ Alert >
282+ < FormField
283+ control = { form . control }
284+ name = "color"
285+ render = { ( { field } ) => (
286+ < FormItem >
287+ < FormControl >
288+ < RadioGroup
289+ onValueChange = { field . onChange }
290+ defaultValue = { field . value }
291+ className = "grid grid-cols-5 gap-4"
292+ >
293+ { [ ...Array ( 7 ) ] . map ( ( _ , index ) => (
294+ < Label
295+ key = { index }
296+ htmlFor = { `${ index } ` }
297+ className = "rounded-full border-2 border-muted bg-popover p-0.5 hover:cursor-pointer hover:bg-accent hover:text-accent-foreground [&:has([data-state=checked])]:border-primary"
298+ >
299+ < Avatar className = "h-full w-full" >
300+ < RadioGroupItem
301+ value = { `${ index } ` }
302+ id = { `${ index } ` }
303+ className = "sr-only"
304+ title = { `Color ${ index } ` }
305+ />
306+ < Image
307+ src = { `/assets/colors/${ index } .png` }
308+ alt = { `Color ${ index } ` }
309+ width = { 70 }
310+ height = { 70 }
311+ />
312+ </ Avatar >
313+ </ Label >
314+ ) ) }
315+ </ RadioGroup >
316+ </ FormControl >
317+ </ FormItem >
318+ ) }
319+ />
320+ { /* <Input id="image" type="color" /> */ }
264321 </ TabsContent >
265322 < TabsContent value = "image" className = "mt-4" >
266323 < Alert >
0 commit comments