|
| 1 | +import React, { useState } from "react"; |
| 2 | + |
| 3 | +export default function Base64Tool() { |
| 4 | + const [input, setInput] = useState(""); |
| 5 | + const [output, setOutput] = useState(""); |
| 6 | + const [error, setError] = useState(null); |
| 7 | + |
| 8 | + const handleEncode = () => { |
| 9 | + setError(null); |
| 10 | + try { |
| 11 | + const encoded = btoa(unescape(encodeURIComponent(input))); |
| 12 | + setOutput(encoded); |
| 13 | + } catch (err) { |
| 14 | + setError("Failed to encode text."); |
| 15 | + } |
| 16 | + }; |
| 17 | + |
| 18 | + const handleDecode = () => { |
| 19 | + setError(null); |
| 20 | + try { |
| 21 | + const decoded = decodeURIComponent(escape(atob(input))); |
| 22 | + setOutput(decoded); |
| 23 | + } catch (err) { |
| 24 | + setError("Invalid Base64 string."); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + const handleClear = () => { |
| 29 | + setInput(""); |
| 30 | + setOutput(""); |
| 31 | + setError(null); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="max-w-3xl mx-auto p-4 space-y-4"> |
| 36 | + <h2 className="text-lg font-semibold">Base64 Encoder / Decoder</h2> |
| 37 | + <p className="text-sm text-slate-500"> |
| 38 | + Encode or decode Base64 text directly in your browser. |
| 39 | + </p> |
| 40 | + |
| 41 | + <textarea |
| 42 | + className="w-full border rounded-md p-3 text-sm focus:outline-none focus:ring-1 focus:ring-slate-400" |
| 43 | + rows="4" |
| 44 | + placeholder="Enter text or Base64 string here..." |
| 45 | + value={input} |
| 46 | + onChange={(e) => setInput(e.target.value)} |
| 47 | + /> |
| 48 | + |
| 49 | + <div className="flex flex-wrap gap-2"> |
| 50 | + <button |
| 51 | + onClick={handleEncode} |
| 52 | + className="px-4 py-2 rounded-md border text-sm hover:bg-slate-700" |
| 53 | + > |
| 54 | + Encode |
| 55 | + </button> |
| 56 | + <button |
| 57 | + onClick={handleDecode} |
| 58 | + className="px-4 py-2 rounded-md border text-sm hover:bg-slate-700" |
| 59 | + > |
| 60 | + Decode |
| 61 | + </button> |
| 62 | + <button |
| 63 | + onClick={handleClear} |
| 64 | + className="px-4 py-2 rounded-md border text-sm hover:bg-slate-700" |
| 65 | + > |
| 66 | + Clear |
| 67 | + </button> |
| 68 | + </div> |
| 69 | + |
| 70 | + {error && ( |
| 71 | + <div className="text-red-600 text-sm border border-red-300 rounded-md p-2"> |
| 72 | + {error} |
| 73 | + </div> |
| 74 | + )} |
| 75 | + |
| 76 | + <div> |
| 77 | + <h3 className="font-medium mb-2">Output</h3> |
| 78 | + <textarea |
| 79 | + className="w-full border rounded-md p-3 text-sm bg-transparent focus:outline-none" |
| 80 | + rows="4" |
| 81 | + readOnly |
| 82 | + value={output} |
| 83 | + placeholder="Result will appear here..." |
| 84 | + /> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div className="text-sm text-slate-500"> |
| 88 | + <p>⚙️ Example for testing:</p> |
| 89 | + <p><strong>Input:</strong> Hello World</p> |
| 90 | + <p><strong>Encoded Output:</strong> SGVsbG8gV29ybGQ=</p> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments