Skip to content

Commit d284342

Browse files
author
Andrea Aramini, INI-MBM-BNC (EXT)
committed
✨ feature: clean up form and add animation on submit
1 parent 9ef700e commit d284342

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/app/components/ContactSection.jsx

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use client';
2-
import React, { useState } from 'react';
2+
import React, { useRef } from 'react';
33
import Link from 'next/link';
44
import Image from 'next/image';
55
import { sendContactMeEmail } from '../lib/client.js';
6+
import { motion, useAnimationControls } from 'framer-motion';
67

78
const EmailSection = () => {
8-
const [emailSubmitted, setEmailSubmitted] = useState(false);
9+
const formRef = useRef(null);
10+
const controls = useAnimationControls();
911
const handleSubmit = async (e) => {
1012
e.preventDefault();
1113
await sendContactMeEmail(
@@ -14,8 +16,7 @@ const EmailSection = () => {
1416
e.target.message.value,
1517
).then((response) => {
1618
if (response.status === 201) {
17-
console.log(`Message sent`);
18-
setEmailSubmitted(true);
19+
formRef.current.reset();
1920
}
2021
});
2122
};
@@ -55,7 +56,7 @@ const EmailSection = () => {
5556
</div>
5657
</div>
5758
<div>
58-
<form className="flex flex-col" onSubmit={handleSubmit}>
59+
<form ref={formRef} className="flex flex-col" onSubmit={handleSubmit}>
5960
<div className="mb-6">
6061
<label
6162
htmlFor="email"
@@ -107,14 +108,28 @@ const EmailSection = () => {
107108
<button
108109
type="submit"
109110
className="rounded hover:bg-primary-400 bg-primary-600 text-primaryText w-full py-2.5 flex justify-center"
111+
onClick={() => {
112+
controls.set({ opacity: 0, scale: 1 });
113+
controls.start({
114+
opacity: 1,
115+
scale: 1,
116+
transition: {
117+
repeat: 1,
118+
repeatType: 'reverse',
119+
duration: 2,
120+
},
121+
});
122+
}}
110123
>
111124
Send Message
112125
</button>
113-
{emailSubmitted && (
114-
<p className="text-primary-500 text-sm mt-2 block">
115-
Email sent succesfully!
116-
</p>
117-
)}
126+
<motion.p
127+
className="text-primary-500 text-sm mt-2 block"
128+
initial={{ opacity: 0, scale: 1 }}
129+
animate={controls}
130+
>
131+
Email sent succesfully!
132+
</motion.p>
118133
</form>
119134
</div>
120135
</section>

0 commit comments

Comments
 (0)