Below is a Shadcn UI implementation using React and Tailwind CSS that meets your requirements. This code includes a rainbow button that, when clicked, displays an image with text below it. There's also a black button for uploading an image and text.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
export default function Home() {
const [imageUrl, setImageUrl] = useState("");
const [text, setText] = useState("");
const [showContent, setShowContent] = useState(false);
const handleClickMe = () => {
// Replace with your desired image URL and text
setImageUrl("https://via.placeholder.com/300"); // Example placeholder image
setText("This is the text below the image!");
setShowContent(true);
};
const handleUpload = (event) => {
event.preventDefault();
const formData = new FormData(event.target);
const uploadedImage = formData.get("image");
const uploadedText = formData.get("text");
if (uploadedImage && uploadedText) {
setImageUrl(URL.createObjectURL(uploadedImage));
setText(uploadedText);
setShowContent(true);
}
};
return (
<div className="flex flex-col items-center justify-center min-h-screen gap-8">
{/* Rainbow Button */}
<Button
onClick={handleClickMe}
className="bg-gradient-to-r from-red-500 via-yellow-500 to-blue-500 hover:from-red-600 hover:via-yellow-600 hover:to-blue-600 text-white px-6 py-2 rounded-full shadow-lg transform transition-transform hover:scale-105"
>
Click Me!
</Button>
{/* Upload Section */}
<form onSubmit={handleUpload} className="flex flex-col gap-4">
<div className="grid w-full max-w-sm items-center gap-1.5">
<Label htmlFor="image">Upload Image</Label>
<Input id="image" name="image" type="file" accept="image/*" />
</div>
<div className="grid w-full max-w-sm items-center gap-1.5">
<Label htmlFor="text">Enter Text</Label>
<Input id="text" name="text" type="text" placeholder="Type text here..." />
</div>
<Button
type="submit"
className="bg-black text-white hover:bg-gray-800 px-6 py-2 rounded-full shadow-lg"
>
Upload
</Button>
</form>
{/* Display Section */}
{showContent && (
<div className="flex flex-col items-center gap-4 mt-8">
<img
src={imageUrl}
alt="Displayed Image"
className="rounded-lg shadow-lg max-w-md w-full"
/>
<p className="text-lg text-center">{text}</p>
</div>
)}
</div>
);
}
Explanation:
-
Rainbow Button:
- Uses a gradient background with Tailwind CSS classes for the rainbow effect.
- On click, it sets a placeholder image and text, then displays them.
-
Upload Button:
- A black button that submits a form containing an image upload field and a text input.
- When submitted, it updates the displayed image and text.
-
Display Section:
- Conditionally renders the image and text below it when either button is clicked.
Prerequisites:
This code provides a functional and visually appealing solution using Shadcn UI components and Tailwind CSS utilities.