Vanilla Breeze

data-vb-newsletter-form

Newsletter signup, unsubscribe, and preferences forms wired through VBService('newsletter'). Progressive enhancement on top of native form action.

Overview

Add data-vb-newsletter-form to a <form> to wire it through VBService('newsletter') against the /go/newsletter/* endpoints. Three modes share the same attribute — subscribe (default), unsubscribe, and preferences. Without JS the form falls back to its native action, so the same markup works as a server-side post.

Subscribe

The default mode. Reads list ids from any <input type="checkbox" name="lists"> children that are checked, or from data-vb-newsletter-lists as a comma-separated default. POSTs { email, lists } to /go/newsletter/subscribe.

For a single-list signup, drop the checkboxes and pass the list via the data attribute:

Unsubscribe

Same shape, different endpoint. POSTs { email, lists } to /go/newsletter/unsubscribe.

Preferences

Loads existing subscriptions for the email on commit (blur or change of the email field) and fills in the checkboxes. Submitting computes the diff and posts subscribe + unsubscribe in parallel, then re-snapshots the new state. Useful as a per-user “manage your subscriptions” surface.

The vb-newsletter-form:preferences event fires when the load completes, with detail { email, subscriptions, available } — useful for syncing parent UI (e.g., disabling lists not exposed by the backend).

Attributes

AttributeRequiredPurpose
data-vb-newsletter-formyessubscribe (default), unsubscribe, or preferences
data-vb-newsletter-listsnoComma-separated default list ids when the form has no checkboxes (e.g. single-list signup).
data-vb-newsletter-loading-labelnoReplaces the submit button label while in flight.
data-vb-newsletter-subscribednoSuccess message for subscribe mode (default: “Subscribed. Check your inbox.”)
data-vb-newsletter-unsubscribednoSuccess message for unsubscribe mode (default: “Unsubscribed.”)
data-vb-newsletter-savednoSuccess message for preferences mode (default: “Preferences saved.”)
data-vb-newsletter-errornoError message (default: “Sorry, that did not work. Try again.”)
data-vb-newsletter-no-resetnoSkip the auto form.reset() on subscribe success.

Events

EventDetailWhen
vb-newsletter-form:submitting{ mode }Just before the request goes out
vb-newsletter-form:success{ mode, lists, response }2xx response
vb-newsletter-form:error{ mode, error, status?, body? }Network failure or non-2xx
vb-newsletter-form:preferences{ email, subscriptions, available }Preferences mode finished loading existing subscriptions

All events bubble + composed.

Related