From f738cd9db29252e985305e0f748a70876833c37d Mon Sep 17 00:00:00 2001 From: Prashant Solanki Date: Wed, 22 May 2024 19:49:50 +0530 Subject: [PATCH] Toggle Button for Light/Dark added --- .gitignore | 22 +++++ client/package-lock.json | 4 +- client/package.json | 6 +- client/src/app/globals.css | 10 ++- client/src/app/page.tsx | 128 ++++++++++++++------------- client/src/components/ThemeToggle.js | 35 ++++++++ 6 files changed, 137 insertions(+), 68 deletions(-) create mode 100644 .gitignore create mode 100644 client/src/components/ThemeToggle.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d440b9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Node modules +node_modules + +# Next.js build artifacts +.next/ +out/ + +# Environment variables +.env.local +.env.development.local +.env.test.local +.env.production.local + +# IDE / Editor files +.vscode/ +.idea/ + +# macOS +.DS_Store + +# Windows +Thumbs.db diff --git a/client/package-lock.json b/client/package-lock.json index 2fda46b..dd67eb5 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -12,8 +12,8 @@ "axios": "^1.6.8", "next": "^14.2.3", "next-themes": "^0.3.0", - "react": "^18", - "react-dom": "^18", + "react": "^18.3.1", + "react-dom": "^18.3.1", "react-hot-toast": "^2.4.1", "sharp": "^0.33.4" }, diff --git a/client/package.json b/client/package.json index d080587..565a7e4 100644 --- a/client/package.json +++ b/client/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "node .next/standalone/server.js", "lint": "next lint" }, "dependencies": { @@ -13,8 +13,8 @@ "axios": "^1.6.8", "next": "^14.2.3", "next-themes": "^0.3.0", - "react": "^18", - "react-dom": "^18", + "react": "^18.3.1", + "react-dom": "^18.3.1", "react-hot-toast": "^2.4.1", "sharp": "^0.33.4" }, diff --git a/client/src/app/globals.css b/client/src/app/globals.css index d4bc6bb..17be94b 100644 --- a/client/src/app/globals.css +++ b/client/src/app/globals.css @@ -16,9 +16,17 @@ } } - @layer utilities { .text-balance { text-wrap: balance; } } + +body { + @apply bg-white text-black; + transition: background-color 0.3s ease, color 0.3s ease; +} + +.dark body { + @apply bg-gray-900 text-white; +} diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx index 15b3986..19a0c0e 100644 --- a/client/src/app/page.tsx +++ b/client/src/app/page.tsx @@ -1,10 +1,11 @@ +// pages/index.js or pages/home.js "use client"; import { CopyButtonIcon } from "@/icons/CopyButtonIcon"; import Image from "next/image"; import Link from "next/link"; import { FormEvent, useState } from "react"; -import toast from "react-hot-toast"; -import { Toaster } from "react-hot-toast"; +import toast, { Toaster } from "react-hot-toast"; +import ThemeToggle from "@/components/ThemeToggle"; export default function Home() { const [isSubmitting, setIsSubmitting] = useState(false); @@ -18,7 +19,7 @@ export default function Home() { try { setIsSubmitting(true); setResponseUrl(""); - + const response = await fetch(`${process.env.NEXT_PUBLIC_SERVER_URL}/`, { method: "POST", headers: { @@ -28,15 +29,13 @@ export default function Home() { }); if (response.status == 429) { - toast.error('Too Many Requests. Please try again later.'); + toast.error("Too Many Requests. Please try again later."); } - - // Assuming response is JSON + const responseData = await response.json(); - setResponseUrl(responseData); } catch (error) { - console.log("error :",error); + console.log("error :", error); } finally { setIsSubmitting(false); } @@ -44,65 +43,70 @@ export default function Home() { return ( <> - -
- http -

Tired of big URLs ?

-

- Make Your URL Short -

- -
- - -
+ +
+
+ +
+
+ http +

Tired of big URLs?

+

+ Make Your URL Short +

- {responseUrl.length > 0 && ( -
-

- Shortened URL -

-
- - {responseUrl} - +
+ - {message && ( -
-
Copied!
+ + + {responseUrl.length > 0 && ( +
+

+ Shortened URL +

+
+ + {responseUrl} + + + {message && ( +
+
Copied!
+
+ )}
- )} -
-
- )} -
+
+ )} +
+ ); } diff --git a/client/src/components/ThemeToggle.js b/client/src/components/ThemeToggle.js new file mode 100644 index 0000000..73f6be4 --- /dev/null +++ b/client/src/components/ThemeToggle.js @@ -0,0 +1,35 @@ +// components/ThemeToggle.js +import { useEffect, useState } from "react"; + +export default function ThemeToggle() { + const [theme, setTheme] = useState("light"); + + useEffect(() => { + const savedTheme = localStorage.getItem("theme") || "light"; + setTheme(savedTheme); + document.documentElement.classList.add(savedTheme); + }, []); + + const toggleTheme = () => { + if (theme === "light") { + document.documentElement.classList.remove("light"); + document.documentElement.classList.add("dark"); + setTheme("dark"); + localStorage.setItem("theme", "dark"); + } else { + document.documentElement.classList.remove("dark"); + document.documentElement.classList.add("light"); + setTheme("light"); + localStorage.setItem("theme", "light"); + } + }; + + return ( + + ); +}