Skip to content

A React.js + TailwindCSS app to generate beautiful linear and radial gradients. Instantly create random color combinations, preview gradients, and copy CSS code. Simple, responsive, and fun for designers and developers to explore color creativity. ๐ŸŽจโš›๏ธ

Notifications You must be signed in to change notification settings

SharwanKunwar/gradient-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ€ Gradient Generator

A beautiful and interactive Gradient Generator built using React.js.
It dynamically generates random linear or radial gradients and lets you copy their CSS code with a single click.


๐Ÿš€ Features

  • ๐ŸŽจ Generate random color gradients.
  • ๐Ÿ” Choose between Linear and Radial types.
  • ๐Ÿงฎ Set how many gradients you want to generate.
  • ๐Ÿ“‹ Copy gradient CSS code to clipboard.
  • ๐Ÿ’ฌ Toast notification when code is copied.
  • ๐Ÿ’ป Responsive design using Tailwind CSS.

๐Ÿ› ๏ธ Tech Stack

  • React.js โ€” Frontend framework
  • React Toastify โ€” Notifications
  • React Icons / Remix Icons โ€” Icon pack
  • Tailwind CSS โ€” Styling

๐Ÿ“‚ Project Structure

src/
โ”‚
โ”œโ”€โ”€ components/
โ”‚ โ””โ”€โ”€ GenerateGradients.jsx # Main gradient generator component
โ”‚
โ”œโ”€โ”€ assets/
โ”‚ โ””โ”€โ”€ color.png # App logo
โ”‚
โ””โ”€โ”€ App.js # Entry point


โš™๏ธ Code Explanation

1. Importing Dependencies

import React, { useEffect, useState } from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'remixicon/fonts/remixicon.css';
  • useState โ€” manages component state.
  • useEffect โ€” runs code when state changes.
  • toast โ€” displays success messages when gradients are copied.

2. State Management

const [num, setNum] = useState(10);
const [type, setType] = useState("Linear");
const [gradients, setGradient] = useState([]);
  • num: number of gradients to display.
  • type: gradient type (Linear or Radial).
  • gradients: array of generated gradient objects.
const generateColor = () => {
  const rgb = 255 * 255 * 255;
  const random = Math.random() * rgb;
  const int = Math.floor(random);
  const hexCode = int.toString(16);
  const colorHex = hexCode.padStart(6, "0");
  return `#${colorHex}`;
};

โœ… Creates a random hex color (like #A1B2C3) by:

  • Generating a random number within 24-bit color range.
  • Converting it to hexadecimal.
  • Padding zeros to ensure itโ€™s a 6-digit code.

4. Generating Gradient Array

const generateGradient = () => {
  const color = [];
  for (let i = 0; i < num; i++) {
    const color1 = generateColor();
    const color2 = generateColor();
    const deg = Math.floor(Math.random() * 360);
    const degString = `${deg}deg`;

    if (type === "Linear") {
      color.push({
        gradient: `linear-gradient(${degString},${color1},${color2})`,
        css: `background: 'linear-gradient(${degString},${color1},${color2})'`,
        colorType: 'Linear'
      });
    } else {
      color.push({
        gradient: `radial-gradient(circle,${color1},${color2})`,
        css: `background: 'radial-gradient(circle,${color1},${color2})'`,
        colorType: 'Radial'
      });
    }
  }
  setGradient(color);
};

๐Ÿ”น This creates an array of gradient objects with two random colors. Each gradient object stores:

{
  gradient: "linear-gradient(120deg, #a1b2c3, #f3d1a1)",
  css: "background: 'linear-gradient(120deg, #a1b2c3, #f3d1a1)'",
  colorType: "Linear"
}

5. Copying Gradient CSS

const copy = (colorCode, colType) => {
  navigator.clipboard.writeText(colorCode);
  toast.success(`${colType} Gradient Code is copied`, { position: "top-center" });
};

Copies the gradient CSS to the clipboard and shows a toast notification confirming the action.

6. Automatically Generate Gradients on Change

useEffect(() => {
  generateGradient();
}, [num, type]);

Whenever the number or type of gradients changes, new gradients are generated automatically.

7. Displaying Gradients

{
  gradients.map((item, index) => (
    <div
      key={index}
      className='w-full h-[150px] rounded-lg flex justify-end items-start'
      style={{ background: item.gradient }}
    >
      <button
        onClick={() => copy(item.css, item.colorType)}
        className='bg-black/10 w-[60px] p-2 rounded-tr-lg'
      >
        copy
      </button>
    </div>
  ))
}

Each gradient box:

  • Uses inline CSS to display its gradient.
  • Has a โ€œcopyโ€ button that copies the gradient CSS.

๐Ÿ’ก Example Workflow

  1. The app loads โ†’ 10 Linear gradients appear automatically.
  2. User changes gradient type to โ€œRadialโ€ โ†’ all gradients regenerate.
  3. User enters 15 โ†’ 15 new gradients appear.
  4. Clicking โ€œcopyโ€ copies the CSS code and shows a toast message.

๐ŸงพSummary

The Gradient Generator is a fun and visually appealing React project that demonstrates:

Random color generation.

  • Conditional rendering (Linear vs. Radial).
  • React Hooks for interactivity.
  • Clipboard and notification functionality.
  • Responsive, modern UI built with Tailwind CSS.

This project is not just about generating gradients โ€” itโ€™s about bringing creativity and logic together. It shows how a few lines of JavaScript can turn random numbers into beautiful color combinations, and how Reactโ€™s state management can make everything update instantly with just a few clicks.

Building this app taught me how to think in terms of components, handle user input smoothly, and give instant feedback through toasts and dynamic rendering. Every time a new gradient appears, it feels like a small creative moment powered by code.

Itโ€™s a perfect project for anyone who wants to strengthen their understanding of React, JavaScript logic, and interactive UI design โ€” while also having fun seeing endless combinations of colors come to life on screen. ๐ŸŽจ๐Ÿ’ป

๐Ÿ“ธ Preview

Gradient Generator Screenshot Gradient Generator Screenshot

๐Ÿ‘จโ€๐Ÿ’ป Author

Sharwan Jung Kunwar Full-Stack Developer | React & Java Enthusiast๐Ÿ“ Nepal

About

A React.js + TailwindCSS app to generate beautiful linear and radial gradients. Instantly create random color combinations, preview gradients, and copy CSS code. Simple, responsive, and fun for designers and developers to explore color creativity. ๐ŸŽจโš›๏ธ

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published