@@ -265,6 +265,84 @@ const actOnGenerator = (attribute: string, outputElement: HTMLElement) => {
265265 } ) ;
266266 }
267267} ;
268+
269+ function extractDegreeFromGradient ( gradient : string ) : number {
270+ const regex = / l i n e a r - g r a d i e n t \( ( \d + ) d e g / ;
271+ const match = gradient . match ( regex ) ;
272+
273+ if ( match ) {
274+ const degree = parseInt ( match [ 1 ] ) ;
275+ return degree ;
276+ }
277+
278+ return 0 ;
279+ }
280+
281+ function getTailwindDirectionClass ( angle : number ) {
282+ const gradientDirections = [
283+ { angle : 0 , class : 'to-r' } ,
284+ { angle : 45 , class : 'to-tr' } ,
285+ { angle : 90 , class : 'to-t' } ,
286+ { angle : 135 , class : 'to-tl' } ,
287+ { angle : 180 , class : 'to-l' } ,
288+ { angle : 225 , class : 'to-bl' } ,
289+ { angle : 270 , class : 'to-b' } ,
290+ { angle : 315 , class : 'to-br' } ,
291+ ] ;
292+
293+ const closest = gradientDirections . reduce ( ( prev , curr ) => {
294+ return Math . abs ( curr . angle - angle ) < Math . abs ( prev . angle - angle )
295+ ? curr
296+ : prev ;
297+ } ) ;
298+ return closest . class ;
299+ }
300+
301+ function rgbToHex ( rgb : string ) : string {
302+ const regex = / r g b \( ( \d + ) , ( \d + ) , ( \d + ) \) / ;
303+ const match = rgb . match ( regex ) ;
304+
305+ if ( match ) {
306+ const r = parseInt ( match [ 1 ] ) . toString ( 16 ) . padStart ( 2 , '0' ) ;
307+ const g = parseInt ( match [ 2 ] ) . toString ( 16 ) . padStart ( 2 , '0' ) ;
308+ const b = parseInt ( match [ 3 ] ) . toString ( 16 ) . padStart ( 2 , '0' ) ;
309+ return `#${ r } ${ g } ${ b } ` ;
310+ }
311+
312+ return '' ;
313+ }
314+
315+ function extractRGBColorsFromGradient ( gradient : string ) : string [ ] {
316+ const regex = / r g b \( \d + , \d + , \d + \) / g;
317+ const matches = gradient . match ( regex ) ;
318+
319+ if ( matches ) {
320+ return matches . map ( ( rgb ) => rgbToHex ( rgb ) ) ;
321+ }
322+
323+ return [ ] ;
324+ }
325+
326+ function generateTailwindClasses ( colors : string [ ] ) : string {
327+ if ( colors . length === 2 ) {
328+ return `from-[${ colors [ 0 ] } ] [to-[${ colors [ 1 ] } ]` ;
329+ } else if ( colors . length === 3 ) {
330+ return `from-[${ colors [ 0 ] } ] via-[${ colors [ 1 ] } ] to-[${ colors [ 2 ] } ]` ;
331+ } else if ( colors . length === 4 ) {
332+ return `from-[${ colors [ 0 ] } ] via-[${ colors [ 1 ] } ] via-[${ colors [ 2 ] } ] to-[${ colors [ 3 ] } ]` ;
333+ }
334+
335+ return '' ;
336+ }
337+
338+ function convertLinearGradientToTailwind ( gradient : string ) : string {
339+ const angle = extractDegreeFromGradient ( gradient ) ;
340+ const direction = getTailwindDirectionClass ( angle ) ;
341+ const rbgColors = extractRGBColorsFromGradient ( gradient ) ;
342+ const gradientClass = generateTailwindClasses ( rbgColors ) ;
343+
344+ return `bg-gradient-${ direction } ${ gradientClass } ` ;
345+ }
268346/**
269347 * what should copy when the copy Tailwind button is clicked
270348 *
@@ -282,17 +360,19 @@ const actOnTailwindGenerator = (
282360 codeToCopy = `` ;
283361 break ;
284362 case 'gradient-text' :
285- codeToCopy = `
286- text-[${ ( outputElement . children [ 0 ] as HTMLElement ) . style . fontSize } ]
287- bg-[${ ( outputElement . children [ 0 ] as HTMLElement ) . style . background } ]
288- text-transparent
289- bg-clip-text` ;
363+ const output = outputElement . children [ 0 ] as HTMLElement ;
364+
365+ codeToCopy = `text-[${
366+ output . style . fontSize
367+ } ] ${ convertLinearGradientToTailwind (
368+ output . style . backgroundImage
369+ ) } text-transparent bg-clip-text`;
290370 break ;
291371 case 'gradient-border' :
292372 codeToCopy = `` ;
293373 break ;
294374 case 'border-radius' :
295- codeToCopy = `rounded -[${ element . borderRadius . replace ( / / g, '_' ) } ]` ;
375+ codeToCopy = `bg -[${ element . borderRadius . replace ( / / g, '_' ) } ]` ;
296376 break ;
297377 case 'box-shadow' :
298378 codeToCopy = `shadow-[${ element . boxShadow . replace ( / / g, '_' ) } ]` ;
0 commit comments