@vulcantech/forms 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -2
- package/dist/index.d.mts +715 -22
- package/dist/index.d.ts +715 -22
- package/dist/index.js +913 -209
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +875 -210
- package/dist/index.mjs.map +1 -1
- package/fingerprint.json +4 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -22,9 +22,10 @@ interface FormDefinition {
|
|
|
22
22
|
submissionType?: FormSubmissionType; // absent = "contact"; decides the target collection
|
|
23
23
|
bookingConfig?: FormBookingConfig; // only read when submissionType === "booking"
|
|
24
24
|
sections: FormSection[]; // render order is `order`, NOT array order
|
|
25
|
-
submitConfig: FormSubmitConfig; // button text, success/error copy, redirect, spam settings
|
|
25
|
+
submitConfig: FormSubmitConfig; // button text, success/error copy, redirect, spam + conversion settings
|
|
26
26
|
style?: FormStyle; // absent = DEFAULT_FORM_STYLE (structure only, no colors)
|
|
27
27
|
defaultTextColor?: string; // hex; per-section `textColor` overrides it
|
|
28
|
+
defaultTextColorDark?: string; // dark-mode counterpart — see Dark mode colors
|
|
28
29
|
metadata?: { trackAdReferral?: boolean; customFields?: Record<string, unknown> };
|
|
29
30
|
integrations?: FormIntegrations; // SERVER-ONLY — stripped
|
|
30
31
|
notificationEmailTemplate?: NotificationEmailTemplate; // SERVER-ONLY — stripped
|
|
@@ -43,6 +44,17 @@ Field notes:
|
|
|
43
44
|
- **`style` is colors-optional by design.** `DEFAULT_FORM_STYLE` supplies only the structural choices (`preset`, `borderRadius`, `spacing`, `buttonVariant`, `buttonPlacement`, `sectionDividers`, `inputVariant`). Every color is optional and resolves through CSS `var()` fallbacks, so an unstyled form inherits the host page rather than forcing its own palette.
|
|
44
45
|
- **`FormDefinitionJSON` vs `FormDefinition`.** The strict type keeps string-literal unions; JSON parsed off a `fetch` widens them to `string` and won't assign. Type the raw value as `FormDefinitionJSON`, then call `parseFormDefinition(json)` — an unchecked structural cast that trusts the CMS, not a validator.
|
|
45
46
|
|
|
47
|
+
### Dark mode colors
|
|
48
|
+
|
|
49
|
+
Every color slot has an optional dark-mode counterpart: `style.dark` carries the same ten keys as the light palette, `defaultTextColorDark` pairs with `defaultTextColor`, and a section's `textColorDark` pairs with its `textColor`. They are authored with the Light / Dark toggle in the CMS form builder and apply only while the host page is in dark mode.
|
|
50
|
+
|
|
51
|
+
- **Activation is the `.dark` class, not `prefers-color-scheme`.** `DynamicForm` looks for an ancestor matching `.dark` — the class next-themes puts on `<html>` in every VulcanTech site with a `ThemeToggle` — so a light-only site never has it and never shows the dark palette, whatever the visitor's OS is set to. Pass `darkModeSelector` to use another selector; it must match `<html>` or `<body>`, because the select popover is portaled to `<body>` and has to fall under the same selector as the form.
|
|
52
|
+
- **An unset dark slot means "same as light"**, never "package default". Each slot falls back to its own light value, so a record only overrides what actually changes.
|
|
53
|
+
- **Section headers resolve scheme-first.** In dark mode: the section's `textColorDark`, then `defaultTextColorDark`, then the light chain — so one form-level dark color re-themes every header, including sections that pinned a light `textColor` before dark mode existed.
|
|
54
|
+
- **Records without dark colors are untouched.** Unless the definition carries at least one dark color the renderer emits nothing dark-related: no attribute, no stylesheet, byte-identical markup.
|
|
55
|
+
|
|
56
|
+
How it works, for when you are debugging it: the light palette stays inline on the form root as `--form-bg`, `--form-input-bg`, …; a record with dark colors additionally publishes `--form-bg-dark`, … on the same element, marks it `data-vt-form-dark`, and renders a small `<style>` whose one rule (`.dark [data-vt-form-dark] { --form-bg: var(--form-bg-dark) !important; … }`) swaps the variables under the dark selector. The `!important` is deliberate: it is the one cascade level that outranks the inline light value without changing what light mode renders. Headers with a dark color carry `data-vt-text-dark` plus `--form-text-dark` and get the same swap. Consumer wiring does not change.
|
|
57
|
+
|
|
46
58
|
### Where a submission lands (`submissionType`)
|
|
47
59
|
|
|
48
60
|
`submissionType` is read **from the form definition on the server**, never from the request body. A submission whose `formId` doesn't resolve always routes to `contact`.
|
|
@@ -69,13 +81,52 @@ Other `DynamicForm` props: `onSuccess(data?)` (receives the parsed response body
|
|
|
69
81
|
- **`url` binding → `400 {"error":"Invalid url"}`.** The CMS compares the submitted `url` against the project's stored `url` by exact string equality. Proxies therefore hardcode the production origin, which means **submissions from a localhost dev server are rejected**. Test against the CMS directly (or temporarily point the project's `url` at your dev origin) rather than assuming the form is broken.
|
|
70
82
|
- **A top-level `email` is required on every submission type.** After the credential, URL, rate-limit and honeypot gates, the CMS runs a Yup schema requiring a valid `email` and returns `400 {"error":"Validation failed","details":[…]}`. Because builder-generated field ids look like `field-1712…`, a form satisfies this only if its email field's id is literally `email`, or the proxy adds a canonical `email` key alongside the raw field ids. A second pass then re-enforces the definition's own `required` rules (skipping `showWhen`-hidden fields), reporting `"<Field label> is required"` in the same `details` array.
|
|
71
83
|
|
|
84
|
+
### Conversion tracking on success
|
|
85
|
+
|
|
86
|
+
On a **genuine** success — the `/api/contact` response came back OK — `DynamicForm` fires two Google signals, once each, before any success UI or `redirectUrl` navigation:
|
|
87
|
+
|
|
88
|
+
**1. A `dataLayer` push. Always, no configuration.** Both plain `gtag.js` and Google Tag Manager see it; in GTM it drives a Custom Event trigger named `form_submit_success`.
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
window.dataLayer.push({
|
|
92
|
+
event: "form_submit_success",
|
|
93
|
+
formId, // definition.id
|
|
94
|
+
formName, // definition.name
|
|
95
|
+
formType, // extraData.submissionType, defaulting to "contact"
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The payload carries **only the form's identity — never submitted field values.** `dataLayer` is created if the Google tag hasn't loaded yet, so the event is not lost in the race (gtag.js and GTM both replay the pre-existing array).
|
|
100
|
+
|
|
101
|
+
**2. A direct Google Ads conversion, only when a `send_to` label resolves.** For sites with an Ads conversion action but no GTM container:
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
window.gtag("event", "conversion", { send_to: "AW-18321257894/AbC-D3fGhIjKlmNoP" });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The label is resolved in this order, and **must never be hardcoded in a consumer app**:
|
|
108
|
+
|
|
109
|
+
1. `definition.submitConfig.conversion.sendTo` — a per-form override, set in the CMS form's submit config.
|
|
110
|
+
2. `window.__vtFormConversionSendTo` — the project-wide value, published by `@vulcantech/analytics`'s `CMSTags` from the `formConversionSendTo` tracking tag (CMS → Integrations → Tracking Tags). This is the normal way to set it: one value per project, and every form on the site picks it up with no per-form and no per-app wiring.
|
|
111
|
+
|
|
112
|
+
If neither resolves, or `window.gtag` isn't a function, no conversion fires and the `dataLayer` push still happens.
|
|
113
|
+
|
|
114
|
+
**Where the label comes from:** Google Ads → Goals → Summary → the conversion action → Tag setup → "Install the tag yourself". The `send_to` in that snippet is the whole value (conversion ID `/` label).
|
|
115
|
+
|
|
116
|
+
Two behaviours worth knowing:
|
|
117
|
+
|
|
118
|
+
- **The spam short-circuit fires neither signal.** A honeypot hit or a sub-`minSubmitTime` submit shows the bot a success banner without contacting the server; counting that as a conversion is exactly the pollution the short-circuit exists to prevent. Same rule as `redirectUrl`, which the spam path also skips.
|
|
119
|
+
- **Nothing here can break a submission.** Every call is guarded and swallows its own errors — a missing global, a hijacked `dataLayer`, or a throwing `gtag` cannot turn a stored lead into an error banner. The helpers are exported (`pushFormSubmitSuccess`, `fireAdsConversion`, `trackFormConversion`) if you need to fire the same signals from a custom render path.
|
|
120
|
+
|
|
121
|
+
This is independent of `onFormSubmit` / `window.__vtTrack`, which feeds VulcanTech's own first-party analytics. Both fire on the same success.
|
|
122
|
+
|
|
72
123
|
### Failure mode
|
|
73
124
|
|
|
74
125
|
**`DynamicForm` never throws at the call site** — it catches everything internally, sets its own error banner, and calls `onError(error)`. There is no promise to await and no exception to catch around the component.
|
|
75
126
|
|
|
76
127
|
Three outcomes look like success but aren't a stored lead, and each is deliberate:
|
|
77
128
|
|
|
78
|
-
- **Spam (honeypot filled, or submitted faster than `minSubmitTime`, default 3s).** The renderer shows the success banner, calls `onSuccess()` **with no argument**, and makes **no network request at all**. Treat a missing argument in `onSuccess` as "no server response", not as an error.
|
|
129
|
+
- **Spam (honeypot filled, or submitted faster than `minSubmitTime`, default 3s).** The renderer shows the success banner, calls `onSuccess()` **with no argument**, and makes **no network request at all**. Treat a missing argument in `onSuccess` as "no server response", not as an error. No conversion signals fire — see [Conversion tracking on success](#conversion-tracking-on-success).
|
|
79
130
|
- **Server-side honeypot.** If a filled honeypot does reach the CMS it writes the row with `status: "spam"` and still answers `200 {"success":true}` — indistinguishable from success by design.
|
|
80
131
|
- **Duplicate.** The CMS fingerprints each submission (all values except `apiKey`, `projectId`, `url`, `formId`, `formType`, `submittedAt`, `_hp_website`) and suppresses an identical resubmit within **10 minutes**, answering `200 {"success":true,"duplicate":true}` with the original `referenceNumber`. No second document, no second notification email. Dedupe fails open — a Firestore outage lets the write through rather than dropping a lead. Forms with sensitive fields are never fingerprinted (fresh ciphertext each submit).
|
|
81
132
|
|
|
@@ -174,5 +225,49 @@ export function ContactForm({ definition }: { definition?: FormDefinition }) {
|
|
|
174
225
|
- `FormDefinition` is the CMS-authored schema (sections, fields, submit config, style) — see [Form shape](#form-shape-formdefinition) above.
|
|
175
226
|
- `style.buttonPlacement: "inline"` renders the submit button beside the fields instead of full-width below them — suited to single-field forms (newsletter email + Join). Editable in the CMS form builder under Style → Submit Button → Button Placement.
|
|
176
227
|
- Newsletter forms (`submissionType: "newsletter"`) should submit to an app route that proxies to the CMS `/api/contact` — the CMS routes them into `newsletter_subscribers`.
|
|
228
|
+
- **After a success the button does not go back to "Submit".** It shows `submitConfig.submittedText` (default `"Sent"`) and stays disabled for as long as the values are unchanged since that submission — an identical resubmit is a duplicate by definition. Editing any field restores `buttonText` and re-enables it, so `reset`/`message` forms can still be used again. The success banner is also scrolled into view, because it renders above the fields and is usually off-screen on mobile at the moment it appears. The CMS-side dedupe gate stays the backstop; this is only the client-side half.
|
|
229
|
+
- `submitConfig.conversion.sendTo` overrides the project-wide Google Ads conversion label for one form; leave it unset and the form uses the project's `formConversionSendTo` tracking tag. See [Conversion tracking on success](#conversion-tracking-on-success).
|
|
177
230
|
- `submitConfig.redirectUrl` takes precedence over `successBehavior` on genuine submissions (typically a thank-you page for ad conversion tracking). It fires only on a real success — the spam path deliberately stays on-page so a detected bot never triggers the conversion tag. Values are passed through `getSafeRedirectUrl`, which accepts absolute http(s) URLs and site-relative paths only.
|
|
178
231
|
- The package also ships `buildYupSchema` / `buildInitialValues` for custom render paths, but consumer sites use the server-fetcher pattern above for ISR caching.
|
|
232
|
+
|
|
233
|
+
## Renderer fingerprint — how a missed deploy gets noticed
|
|
234
|
+
|
|
235
|
+
`yarn build` emits `dist/fingerprint.json`: a content hash of the built
|
|
236
|
+
renderer, plus the package version for readability.
|
|
237
|
+
|
|
238
|
+
```json
|
|
239
|
+
{ "fingerprint": "e74334072c40", "version": "0.4.0" }
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
The daily form health check compares this hash between the CMS and each
|
|
243
|
+
consumer site. A mismatch means one side was not redeployed after the
|
|
244
|
+
renderer changed — the CMS can be serving form config (closed days, closed
|
|
245
|
+
time windows) that an out-of-date site's bundled renderer ignores, and
|
|
246
|
+
nothing else surfaces that.
|
|
247
|
+
|
|
248
|
+
**The hash is compared, not the version.** A version only works if someone
|
|
249
|
+
remembers to bump it, and a renderer change shipped without a bump makes
|
|
250
|
+
every site report a match while running different code — worse than no
|
|
251
|
+
check. Nothing has to be remembered here.
|
|
252
|
+
|
|
253
|
+
What it does and doesn't move on:
|
|
254
|
+
|
|
255
|
+
- Any change to the shipped bundle changes it.
|
|
256
|
+
- A comment-only edit does **not** — tsup strips comments, so doc churn
|
|
257
|
+
raises no false alarm.
|
|
258
|
+
- Two builds of identical source produce an identical hash. The file carries
|
|
259
|
+
no timestamp, and only `index.mjs`, `index.js` and `index.d.ts` are hashed
|
|
260
|
+
— never the sourcemaps, which embed paths that differ between build
|
|
261
|
+
machines and would report drift that isn't there.
|
|
262
|
+
|
|
263
|
+
Consumers read it through the `./fingerprint` export, which exists for
|
|
264
|
+
exactly this purpose and pulls in no React:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import rendererBuild from "@vulcantech/forms/fingerprint";
|
|
268
|
+
// → report rendererBuild.fingerprint on a health-check probe
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
`dist/` is gitignored, so the fingerprint is produced at build time. Any
|
|
272
|
+
consumer build already requires this package to be built first, so there is
|
|
273
|
+
no new ordering constraint.
|