Skip to content

Commit

Permalink
Merge pull request #104 from daviddarnes/enhance-form-include
Browse files Browse the repository at this point in the history
#103 Enhance form include
  • Loading branch information
daviddarnes authored Sep 2, 2018
2 parents 18db20b + 3314e4b commit a809121
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
- Modular Jekyll components
- Post category support in the form of a single post index page grouped by category
- Built in live search using JavaScript
- **Contact form** built in using [Formspree](https://formspree.io/)
- **Contact form** built in using [Formspree](https://formspree.io/) or [Netlify Forms](https://www.netlify.com/features/#forms)
- Designed with **[Siteleaf](http://www.siteleaf.com/)** in mind
- Has 9 of the most popular networks as performant sharing buttons
- Has documentation
Expand Down Expand Up @@ -219,11 +219,16 @@ Available options:
- `id`: The map ID for the video _required_

### `site-form.html`
Adds a contact form to the page.
Adds a contact form to the page. This can be used with [Formspree](https://formspree.io/) or [Netlify Forms](https://www.netlify.com/docs/form-handling/) depending on your setup.

Example usage: `{% include site-form.html %}`

This include has no options. Use the `email` option in the `/_config.yml` to change to the desired email.
Available options:
- `netlify_form=true`: Set whether you would like to use Netlify Forms, otherwise the form will default to Formspree
- `name`: Give the form a name, by default the form is called "Contact". The name will be reflected when form submissions come through in Netlify or in your email client. The name is also used in the label and input elements for accessibility


Use the `email` option in the `/_config.yml` to change to the desired email.

### `site-search.html`
Adds a search form to the page.
Expand Down
66 changes: 46 additions & 20 deletions _includes/site-form.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
<div class="form form--contact">
<form class="contact-form" action="" method="POST">
<label class="label" for="name">Name: <span class="required">*</span></label>
<input class="input" id="name" type="text" name="name" value="" placeholder="Your Name" required="required" />
{% if include.name %}
{% assign name = include.name %}
{% else %}
{% assign name = "Contact" %}
{% endif %}

<label class="label" for="_replyto">Email Address: <span class="required">*</span></label>
<input class="input" id="_replyto" type="email" name="_replyto" value="" placeholder="[email protected]" required="required" />
{% assign name_id = name | downcase | replace: " ", "-" %}

<label class="label" for="message">Message: <span class="required">*</span></label>
<textarea class="textarea" id="message" name="message" placeholder="Your message..." required="required"></textarea>
{% if include.thanks_url %}
{% assign thanks_url = include.thanks_url %}
{% else %}
{% assign thanks_url = "/thanks/" %}
{% endif %}

<form class="form form--{{ name_id }}" method="post" name="{{ name }}" action="{{ thanks_url }}"
{% if include.netlify_form %}
netlify-honeypot="bot-field"
netlify
{% endif %}
>
<fieldset class="fieldset form__fieldset">
<legend class="legend form__legend">{{ name }}</legend>
<label class="label" for="name--{{ name_id }}">Name: <span class="required">*</span></label>
<input class="input" id="name--{{ name_id }}" type="text" name="name" value="" placeholder="Your Name" required="required" />

<label class="label" for="email--{{ name_id }}">Email Address: <span class="required">*</span></label>
<input class="input" id="email--{{ name_id }}" type="email" name="email" value="" placeholder="[email protected]" required="required" />

<label class="label" for="message--{{ name_id }}">Message: <span class="required">*</span></label>
<textarea class="textarea" id="message--{{ name_id }}" name="message" placeholder="Your message..." required="required"></textarea>

<input class="button" type="submit" value="Send message" />
<br/>
<small class="small"><span class="required">*</span> indicates a required field</small>

<input type="text" name="_gotcha" style="display:none">
<input type="hidden" name="_subject" value="Message from {{ site.name }} website">
<input type="hidden" name="_next" value="/thanks/" />
</form>
</div>
<script>
var email = "{{ site.email | split: "" | reverse | join: "" }}";
var unraveledEmail = email.split("").reverse().join("");
var form = document.querySelector(".contact-form");
form.setAttribute("action", "https://formspree.io/" + unraveledEmail);
</script>
<noscript>Please enable JavaScript to use the form.</noscript>
{% if include.netlify_form %}
<label style="display:none">Don’t fill this out if you’re human: <input name="bot-field" /></label>
{% else %}
<input type="text" name="_gotcha" style="display:none">
<input type="hidden" name="_subject" value="{{ site.title }} submission from {{ name }}">
<input type="hidden" name="_next" value="{{ thanks_url }}" />
{% endif %}
</fieldset>
</form>
{% unless include.netlify_form %}
<script>
var email = "{{ site.email | split: "" | reverse | join: "" }}";
var unraveledEmail = email.split("").reverse().join("");
var form = document.querySelector(".form--{{ name_id }}");
form.setAttribute("action", "https://formspree.io/" + unraveledEmail);
</script>
<noscript>Please enable JavaScript to use the form.</noscript>
{% endunless %}
15 changes: 14 additions & 1 deletion _sass/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ img {

.figure,
.video,
.map {
.map,
.form {
margin-bottom: .8rem;
}

Expand All @@ -271,6 +272,18 @@ video {


// Form elements and buttons
.form {
position: relative;
&__legend {
font-style: italic;
color: $captionColour;
position: absolute;
overflow: hidden;
right: 0;
clip: rect(0 0 0 0);
}
}

button,
.button,
input[type="text"],
Expand Down

0 comments on commit a809121

Please sign in to comment.