Skip to content

Organisms · Newsletter form

Newsletter form

An email field, a line saying what you're agreeing to, and a button. It checks input the same way every form here does, is honest about waiting while it sends, and tells you when it worked.

Best for
Collecting one email address for a newsletter, with a consent line.
Not for
Creating an account: use AuthForm.

This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).

Examples

A sign-up that succeeds

Demo: any address is accepted after a short wait. Nothing is sent.

Code
tsx
const wait = (ms: number) => new Promise(resolve => window.setTimeout(resolve, ms))​const CONSENT = 'One email a month. Every one has an unsubscribe link.'​async function accept() {  await wait(700)}​<div className="flex w-full flex-col gap-3">  <p className="font-body text-sm text-[var(--color-text-secondary)]">    Demo: any address is accepted after a short wait. Nothing is sent.  </p>  <NewsletterForm label="Field notes by email" consent={CONSENT} onSubmit={accept} headingLevel={4} /></div>

A sign-up that fails

Demo: this one rejects every address, so you can see the alert, and the address you typed stays in the field.

Code
tsx
const wait = (ms: number) => new Promise(resolve => window.setTimeout(resolve, ms))​async function refuse() {  await wait(700)  throw new Error('The demo server is switched off.')}​<div className="flex w-full flex-col gap-3">  <p className="font-body text-sm text-[var(--color-text-secondary)]">    Demo: this one rejects every address, so you can see the alert, and the address you typed stays in the field.  </p>  <NewsletterForm    label="Restock alerts by email"    consent="An email when a sold-out replica is back. Nothing else."    submitLabel="Notify me"    busyLabel="Signing you up"    onSubmit={refuse}    headingLevel={4}  /></div>

Anatomy

PartWhat it is
FormScreen readers name it by the label prop. It sits on opaque surface.base, in a hairline frame with the corner crosshair.
Error summaryAppears when a submit fails and takes focus. Its entry moves focus to the field. It waits for someone to try submitting: leaving the field (a blur) never brings it up on its own.
EmailA FormField wrapped around an Input, set to type email, autocomplete email and required.
ConsentRequired. What subscribing means and how to stop. Screen readers read it as the submit button's description.
SubmitA Button that keeps focus and shows it's busy while onSubmit is pending. aria-busy and aria-disabled, two ARIA attributes (Accessible Rich Internet Applications, the labels screen readers rely on), tell screen readers the same.
Submit errorAn alert region that is always there and gets its text when onSubmit fails. The visible message only appears then. It clears before the next try.
SuccessTakes the place of the form: a check icon, a heading that takes focus, and the address that was subscribed.
StatusHidden from view and present from the start. It announces the success only if focus had already left the form.

Props

labelstring
Required. The form's accessible name, the name screen readers say, e.g. "Newsletter sign-up". Leaving it out is a compile error.
onSubmit(email: string) => void | Promise<void>
Required. Called with the trimmed address once it passes validation. Resolve (finish successfully) to show the success state. Reject (fail) to show the alert, and what was typed stays in the field.
consentReactNode
Required. The consent line. Leaving it out is a compile error.
fieldLabelstringDefault 'Email'
The field's label and the name screen readers say for it, as one string. Say what the address is for when the page might have another email field, such as a footer under a sign-in form.
submitLabelstringDefault 'Subscribe'
The button text. The button is secondary, so a sign-up in a footer never competes with the page’s one primary action.
busyLabelstringDefault 'Subscribing'
What a screen reader hears while onSubmit is pending.
successTitlestringDefault 'You are subscribed'
The success heading.
successBody(email: string) => ReactNodeDefault email => `Updates go to ${email}.`
The line under the success heading. It receives the subscribed address.
headingLevel2 | 3 | 4Default 3
The level of the error summary heading and the success heading. Make it one below the heading of the section the form sits in.
classNamestring
Added to the root element.
refRef<HTMLDivElement>
Passed to the root element, which stays on the page when the success message replaces the form.

Keyboard

KeysResult
TabMoves to the field, then the button. Screen readers read the consent line with the button.
EnterIn the field or on the button, it submits, and it does nothing while a submit is pending. On the error summary entry, it moves focus to the field.

Do and don't

Do

  • Say in the consent line how often you write and how to stop.
  • Start the section it sits in with a heading that says what the emails are about.
  • Reject onSubmit when the request fails. The form shows the alert and keeps the address.
  • Set headingLevel to one below the section heading above it.

Don't

  • Don't pre-tick anything, or sign people up as a side effect of another form.
  • Don't put network code in it. onSubmit belongs to you; the form handles busy, failure and success.
  • Don't place it on a raised panel and remove its fill. Field errors sit on surface.base.
  • Don't ask for a name. Having just one field is the point.