This guide will teach you how to create a beautiful and elegant form using Gatsby. Creating contact forms in Gatsby shouldn’t be a tedious task if your site is built using a static/jamstack approach.
In case you're starting a brand new project, you'll need some initial steps. One of the most straightforward ways is to use the gatsby-cli package. To start:
npm install -g gatsby-cli
npm init gatsby
cd my-gatsby-site && npm run develop
Use your favorite code editor to work with files in ~/my-gatsby-site/src. You will be able to make a contact form there.
To effortlessly handle form submissions, you can use the herotofu-react package. It will do all the form submission process work for you.
1npm install --save herotofu-react
Create a new file called ContactForm.js in the src folder. You can use any type of field and any framework for building HTML, CSS, and Javascript. For now, let's stick with the standard "Name", "Email", and "Message" for our simple contact form. We're also going to use TailwindCSS to make it beautiful, but again, you can use your own customized CSS code too.
See how to install Gatsby Tailwind CSS here.
1import { useFormData } from 'herotofu-react';
2
3const ContactForm = () => {
4 // TODO - update to the correct endpoint
5 const { formState, getFormSubmitHandler } = useFormData('https://herotofu.com/start');
6
7 return (
8 <>
9 {!!formState.status && <div className="py-2">Current form status is: {formState.status}</div>}
10 <form onSubmit={getFormSubmitHandler()}>
11 <div className="pt-0 mb-3">
12 <input
13 type="text"
14 placeholder="Your name"
15 name="name"
16 className="focus:outline-none focus:ring relative w-full px-3 py-3 text-sm text-gray-600 placeholder-gray-400 bg-white border-0 rounded shadow outline-none"
17 required
18 />
19 </div>
20 <div className="pt-0 mb-3">
21 <input
22 type="email"
23 placeholder="Email"
24 name="email"
25 className="focus:outline-none focus:ring relative w-full px-3 py-3 text-sm text-gray-600 placeholder-gray-400 bg-white border-0 rounded shadow outline-none"
26 required
27 />
28 </div>
29 <div className="pt-0 mb-3">
30 <textarea
31 placeholder="Your message"
32 name="message"
33 className="focus:outline-none focus:ring relative w-full px-3 py-3 text-sm text-gray-600 placeholder-gray-400 bg-white border-0 rounded shadow outline-none"
34 required
35 />
36 </div>
37 <div className="pt-0 mb-3">
38 <button
39 className="active:bg-blue-600 hover:shadow-lg focus:outline-none px-6 py-3 mb-1 mr-1 text-sm font-bold text-white uppercase transition-all duration-150 ease-linear bg-blue-500 rounded shadow outline-none"
40 type="submit"
41 >
42 Send a message (simple)
43 </button>
44 </div>
45 </form>
46 </>
47 );
48};
49
50export default ContactForm;
Open src/pages/index.js in your src folder, add contact form component. If it's an existing project, open the file where the contact form should appear. You need to:
1import * as React from "react";
2import ContactForm from "../ContactForm";
3
4const IndexPage = () => {
5 return (
6 <main className="max-w-md p-6 mx-auto my-16 bg-gray-100">
7 <title>Home Page</title>
8 <h1 className="text-xl">Hello World!</h1>
9 <div className="pt-6">
10 <ContactForm />
11 </div>
12 </main>
13 );
14};
15
16export default IndexPage;
Head over to herotofu.com and create an account. It will handle the tedious and complicated form submission process for you. You'll get 14 free days of the free trial. Later, you can leave it with a free forever plan. For the sole purpose of using the contact form, it's usually more than enough.
HeroTofu registration is straightforward. Fill in the basic fields and then confirm your email address.
Once you have confirmed your email address, go to app.herotofu.com/forms to create your first form. Enter your name and email address where you want to receive your form submissions Slack and Zapier are also options, but you need to pay for them once the trial is over.
You'll get the endpoint URL once you hit Submit, so remember to copy that.
Once again, open the ContactForm.js file and fill in the form endpoint URL. You need to change the passed string to the useFormData() at the top of the component. It should look like this.
1import { useFormData } from 'herotofu-react';
2
3const ContactForm = () => {
4 const { formState, getFormSubmitHandler } = useFormData('ENDPOINT_URL_OR_HEROTOFU_FORM_ID');
5
6 // The rest of the code...
Done! Go ahead and test your contact form submission! You don't need to do any backend email work, as HeroTofu will handle everything.