Skip to content

Commit d27977e

Browse files
committed
fix(contact): add Chrome compatibility and fallback for email functionality
- Implement multiple methods to open email client (window.open, location.href) - Add fallback modal when email client doesn't open automatically - Include copy-to-clipboard functionality for email content - Show formatted email content directly on page as backup option - Add retry button for mailto links - Ensure cross-browser compatibility including Chrome
1 parent 52bae2b commit d27977e

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

src/components/Contact.tsx

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const Contact: React.FC = () => {
1010
});
1111
const [isSubmitting, setIsSubmitting] = useState(false);
1212
const [submitStatus, setSubmitStatus] = useState<'idle' | 'success' | 'error'>('idle');
13+
const [showEmailFallback, setShowEmailFallback] = useState(false);
14+
const [emailContent, setEmailContent] = useState('');
1315

1416
const handleSubmit = (e: React.FormEvent) => {
1517
e.preventDefault();
@@ -39,8 +41,33 @@ This message was sent from your portfolio contact form at https://davidagustin.g
3941
// Create mailto link
4042
const mailtoLink = `mailto:davidsyagustin@gmail.com?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
4143

42-
// Open default email client
43-
window.open(mailtoLink, '_blank');
44+
// Try multiple methods to open email client (works across all browsers)
45+
let emailOpened = false;
46+
47+
try {
48+
// Method 1: Direct window.open
49+
const emailWindow = window.open(mailtoLink, '_blank');
50+
51+
// Method 2: If window.open fails, try location.href
52+
if (!emailWindow || emailWindow.closed || typeof emailWindow.closed === 'undefined') {
53+
window.location.href = mailtoLink;
54+
emailOpened = true;
55+
} else {
56+
emailOpened = true;
57+
}
58+
} catch (windowError) {
59+
// Method 3: Fallback to location.href
60+
window.location.href = mailtoLink;
61+
emailOpened = true;
62+
}
63+
64+
// If email client didn't open, show fallback
65+
setTimeout(() => {
66+
if (!emailOpened) {
67+
setEmailContent(body);
68+
setShowEmailFallback(true);
69+
}
70+
}, 1000);
4471

4572
// Show success message
4673
setSubmitStatus('success');
@@ -201,6 +228,57 @@ This message was sent from your portfolio contact form at https://davidagustin.g
201228
</div>
202229
)}
203230

231+
{/* Email Fallback Modal */}
232+
{showEmailFallback && (
233+
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
234+
<div className="bg-white rounded-lg p-6 max-w-2xl w-full max-h-[80vh] overflow-y-auto">
235+
<div className="flex justify-between items-center mb-4">
236+
<h3 className="text-xl font-bold text-gray-800">Email Client Not Found</h3>
237+
<button
238+
onClick={() => setShowEmailFallback(false)}
239+
className="text-gray-500 hover:text-gray-700 text-2xl"
240+
>
241+
×
242+
</button>
243+
</div>
244+
<p className="text-gray-600 mb-4">
245+
Your email client didn't open automatically. Here's your formatted message - you can copy and paste it into your email client:
246+
</p>
247+
<div className="bg-gray-50 p-4 rounded-lg border">
248+
<div className="mb-2">
249+
<strong>To:</strong> davidsyagustin@gmail.com
250+
</div>
251+
<div className="mb-2">
252+
<strong>Subject:</strong> Portfolio Contact from {formData.name}
253+
</div>
254+
<div className="mb-4">
255+
<strong>Message:</strong>
256+
</div>
257+
<pre className="whitespace-pre-wrap text-sm text-gray-700 font-mono bg-white p-3 rounded border">
258+
{emailContent}
259+
</pre>
260+
</div>
261+
<div className="mt-4 flex gap-2">
262+
<button
263+
onClick={() => {
264+
navigator.clipboard.writeText(emailContent);
265+
alert('Email content copied to clipboard!');
266+
}}
267+
className="btn btn-primary"
268+
>
269+
Copy to Clipboard
270+
</button>
271+
<a
272+
href={`mailto:davidsyagustin@gmail.com?subject=${encodeURIComponent(`Portfolio Contact from ${formData.name}`)}&body=${encodeURIComponent(emailContent)}`}
273+
className="btn btn-secondary"
274+
>
275+
Try Mailto Link Again
276+
</a>
277+
</div>
278+
</div>
279+
</div>
280+
)}
281+
204282
<button
205283
type="submit"
206284
className={`w-full btn btn-primary ${isSubmitting ? 'opacity-50 cursor-not-allowed' : ''}`}

0 commit comments

Comments
 (0)