Skip to content

Commit 94bc2ba

Browse files
committed
✨ added randomize option to generator gradients randomly
1 parent d036a2e commit 94bc2ba

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/showcase/effects/gradients/CustomGradientGenerator.tsx

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { gradientDirection, gradientShape } from '@/data/GradientData'
1616
import { generateCssGradient, generateTailwindGradient } from '@/lib/gradients'
1717
import { AnimatePresence, motion } from 'framer-motion'
18-
import { Check, Copy } from 'lucide-react'
18+
import { Check, Copy, Shuffle } from 'lucide-react'
1919
import { useState } from 'react'
2020
import { HexColorPicker } from 'react-colorful'
2121
import { toast } from 'sonner'
@@ -74,6 +74,27 @@ const CustomGradientGenerator = () => {
7474
toast.success('Gradient copied to clipboard!')
7575
}
7676

77+
const getRandomColor = () => {
78+
return '#' + Math.floor(Math.random() * 16777215).toString(16)
79+
}
80+
81+
const getRandomDirection = () => {
82+
return gradientDirection[
83+
Math.floor(Math.random() * gradientDirection.length)
84+
]
85+
}
86+
87+
const generateRandomGradient = () => {
88+
const newFromColor = getRandomColor()
89+
const newToColor = getRandomColor()
90+
const newDirection = getRandomDirection()
91+
92+
setFromColor(newFromColor)
93+
setToColor(newToColor)
94+
setSelected(newDirection)
95+
setShowVia(false) // Optionally, you can set this to Math.random() > 0.5 to randomly include or exclude the via color
96+
}
97+
7798
const variants = {
7899
hidden: { opacity: 0, scale: 0.5 },
79100
visible: { opacity: 1, scale: 1 },
@@ -84,15 +105,23 @@ const CustomGradientGenerator = () => {
84105
{/* Gradient Generator */}
85106
<Card>
86107
<CardContent className="flex flex-col items-start justify-center gap-4 pt-6">
87-
<label className="flex w-fit cursor-pointer select-none items-center text-left">
88-
<input
89-
type="checkbox"
90-
checked={showVia}
91-
onChange={() => setShowVia(!showVia)}
92-
className="mr-2"
93-
/>
94-
Add Via
95-
</label>
108+
<div className="flex w-full items-center justify-between gap-2">
109+
<label className="flex w-fit cursor-pointer select-none items-center text-left">
110+
<input
111+
type="checkbox"
112+
checked={showVia}
113+
onChange={() => setShowVia(!showVia)}
114+
className="mr-2"
115+
/>
116+
Add Via
117+
</label>
118+
<button
119+
onClick={generateRandomGradient}
120+
className="rounded-lg border p-2 hover:bg-gray-100"
121+
>
122+
<Shuffle size={16} />
123+
</button>
124+
</div>
96125
<div className="mx-auto flex w-full items-center justify-start gap-1">
97126
<ColorPickerPopover
98127
color={fromColor}
@@ -112,6 +141,7 @@ const CustomGradientGenerator = () => {
112141
label="To"
113142
/>
114143
</div>
144+
115145
<div className="w-full ">
116146
<Select
117147
value={selected.value}

0 commit comments

Comments
 (0)