An email field and button, a consent line, and the answer in place once sent.
<section class="cap-newsletter" aria-labelledby="nl-card-title" id="nl-card">
<h3 class="cap-newsletter-title" id="nl-card-title">Newsletter</h3>
<p class="cap-newsletter-text">Field notes and new posts, once a month.</p>
<p tabindex="-1" role="alert" class="cap-sr-only cap-form-status"></p>
<p tabindex="-1" role="status" class="cap-sr-only cap-form-status"></p>
<form class="cap-newsletter-form" method="post" action="#" aria-labelledby="nl-card-title">
<div class="cap-field">
<label class="cap-field-label cap-sr-only" for="nl-card-email">Email address</label>
<div class="cap-field-row">
<input id="nl-card-email" class="cap-input" type="email" name="email" required autocomplete="email" inputmode="email" spellcheck="false" placeholder="you@example.com" />
<button type="submit" class="cap-btn" data-variant="primary">Subscribe</button>
</div>
<p class="cap-field-error" id="nl-card-email-error" hidden></p>
</div>
<label class="cap-check cap-newsletter-consent"><input type="checkbox" name="consent" required /><span>I agree to receive the newsletter by email and accept the <a href="#privacy">privacy policy</a>. Unsubscribe with the link in any email.</span></label>
</form>
</section>
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/newsletter-signup.css";
import "capsomer/tokens.css";
import "capsomer/newsletter-signup.css";
import { NewsletterSignup } from "capsomer/react/newsletter-signup";
Usage
<section class="cap-newsletter" aria-labelledby="nl-title"> <!-- data-variant="plain" -->
<h2 class="cap-newsletter-title" id="nl-title">Newsletter</h2>
<p class="cap-newsletter-text">One line.</p>
<form class="cap-newsletter-form" method="post" action="/subscribe" aria-labelledby="nl-title">
<div class="cap-newsletter-row">
<label class="cap-sr-only" for="nl-email">Email address</label>
<input id="nl-email" class="cap-input" type="email" name="email" required autocomplete="email" inputmode="email" spellcheck="false" placeholder="you@example.com" />
<button type="submit" class="cap-btn" data-variant="primary">Subscribe</button>
</div>
<label class="cap-check cap-newsletter-consent"><input type="checkbox" name="consent" required /><span>I agree ... <a href="/privacy">privacy policy</a>.</span></label>
</form>
<p class="cap-newsletter-error" id="nl-error" role="alert">Please enter a valid email address.</p> <!-- on error -->
</section>
<!-- sent: the form becomes <p class="cap-newsletter-done" role="status">Almost done: check your inbox ...</p> -->
Examples
With an error
<section class="cap-newsletter" aria-labelledby="nl-error-title" id="nl-error">
<h3 class="cap-newsletter-title" id="nl-error-title">Newsletter</h3>
<p class="cap-newsletter-text">Field notes and new posts, once a month.</p>
<p tabindex="-1" role="alert" class="cap-sr-only cap-form-status"></p>
<p tabindex="-1" role="status" class="cap-sr-only cap-form-status"></p>
<form class="cap-newsletter-form" method="post" action="#" aria-labelledby="nl-error-title">
<div class="cap-field" data-invalid="true">
<label class="cap-field-label cap-sr-only" for="nl-error-email">Email address</label>
<div class="cap-field-row">
<input id="nl-error-email" class="cap-input" type="email" name="email" required autocomplete="email" inputmode="email" spellcheck="false" placeholder="you@example.com" aria-invalid="true" aria-describedby="nl-error-error" />
<button type="submit" class="cap-btn" data-variant="primary">Subscribe</button>
</div>
<p class="cap-field-error" id="nl-error-error" role="alert">Please enter a valid email address.</p>
</div>
<label class="cap-check cap-newsletter-consent"><input type="checkbox" name="consent" required /><span>I agree to receive the newsletter by email and accept the <a href="#privacy">privacy policy</a>. Unsubscribe with the link in any email.</span></label>
</form>
</section>
Plain, at the end of a post
<section class="cap-newsletter" data-variant="plain" aria-labelledby="nl-plain-title" id="nl-plain">
<h3 class="cap-newsletter-title" id="nl-plain-title">Get the next episode</h3>
<p tabindex="-1" role="alert" class="cap-sr-only cap-form-status"></p>
<p tabindex="-1" role="status" class="cap-sr-only cap-form-status"></p>
<form class="cap-newsletter-form" method="post" action="#" aria-labelledby="nl-plain-title">
<div class="cap-field">
<label class="cap-field-label cap-sr-only" for="nl-plain-email">Your email</label>
<div class="cap-field-row">
<input id="nl-plain-email" class="cap-input" type="email" name="address" required autocomplete="email" />
<button type="submit" class="cap-btn">Sign up</button>
</div>
<p class="cap-field-error" id="nl-plain-email-error" hidden></p>
</div>
<label class="cap-check cap-newsletter-consent"><input type="checkbox" name="agree" required /><span>Send me new episodes by email (<a href="#privacy">privacy</a>).</span></label>
</form>
</section>
Sent
<section class="cap-newsletter" aria-labelledby="nl-done-title" id="nl-done">
<h3 class="cap-newsletter-title" id="nl-done-title">Newsletter</h3>
<p tabindex="-1" role="alert" class="cap-sr-only cap-form-status"></p>
<p tabindex="-1" role="status" class="cap-alert cap-form-status" data-tone="ok"><svg class="cap-status-glyph" viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path fill="currentColor" fill-rule="evenodd" d="M8 1a7 7 0 1 0 0 14A7 7 0 1 0 8 1zM3.9 9l3 3 5.2-5.6-1.4-1.4-3.8 4.2-1.6-1.6z"></path></svg><span class="cap-alert-body">Almost done: check your inbox for a link to confirm your subscription.</span></p>
</section>
API reference
NewsletterSignup
| Prop | Type | Required | Does |
|---|
action | string | | Where the form posts: the site's action, which sends the confirmation link (double opt-in). |
|---|
title | ReactNode | | |
|---|
text | ReactNode | | |
|---|
consent | ReactNode | | The consent line, a required checkbox (a link to the privacy page inside it). |
|---|
state | NewsletterState | | The action's answer: sent, or the error to show. |
|---|
done | ReactNode | | |
|---|
button | ReactNode | | |
|---|
placeholder | string | | |
|---|
plain | boolean | | Card (default) or on the page's own ground. |
|---|
headingLevel | "h2" | "h3" | | |
|---|
renderForm | (props: NewsletterFormProps) => ReactNode | | Draw the form with the app's router form (React Router's Form), so the post needs no reload. |
|---|
CSS variables
| Variable | Default |
|---|
--cap-newsletter-bg | var(--surface) |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-newsletter | the root |
|---|
.cap-newsletter-consent | a part |
|---|
.cap-newsletter-form | a part |
|---|
.cap-newsletter-text | a part |
|---|
.cap-newsletter-title | a part |
|---|
| Attribute | Values |
|---|
data-variant | plain |
|---|
Accessibility
| Key | Does |
|---|
| Tab | Field, button, consent box, and the privacy link |
| Enter | In the field, sends the form |
| Space | Ticks the consent box |
- A
section named by its heading; the form named the same. - The sent line and the error are announced when they appear.