@ehven/messaging-form-adapter-astro 0.0.0-stage → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Gilad Ehven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # Ehven Messaging Form for Astro
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Use the shell and identity fields in an Astro page with a same-origin endpoint that implements the messaging form contract.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @ehven/messaging-form-core @ehven/messaging-form-adapter-astro
9
+ ```
10
+
11
+ Requires Node.js 24 or later and an existing Astro project on Astro 7.3.8 or a later compatible 7.x release.
12
+
13
+ ```astro
14
+ ---
15
+ import MessagingFormShell from '@ehven/messaging-form-adapter-astro/shell';
16
+ import MessagingIdentityFields from '@ehven/messaging-form-adapter-astro/identity-fields';
17
+ ---
18
+
19
+ <MessagingFormShell source="contact-page" endpoint="/api/contact" privacyUrl="/privacy/">
20
+ <MessagingIdentityFields />
21
+ <label>
22
+ Message
23
+ <textarea name="message" required></textarea>
24
+ </label>
25
+ </MessagingFormShell>
26
+ ```
27
+
28
+ - Set `PUBLIC_TURNSTILE_SITE_KEY` or pass `turnstileSiteKey` to the shell. Use `disabled` to keep the form offline. The browser needs JavaScript to complete Turnstile. Each available form requires its own valid challenge before submission.
29
+ - Validate Turnstile on the server and keep the secret there. The endpoint accepts form data and returns the messaging form JSON contract.
30
+ - Use the slot for additional fields. Set `variant` on both components to use matching classes. Use `privacyText` to supply your own disclosure.
31
+ - The theme uses CSS custom properties beginning with `--ehven-`. The shell imports its styles automatically.
32
+
33
+ Run `npm run check` to inspect the package or `npm run check:astro` to build a fixture from packed dependencies. Use `MESSAGING_CORE_PATH` to point the fixture at a checkout of messaging-form-core. The default is a sibling checkout.
34
+
35
+ MIT
36
+
37
+ ## Support
38
+
39
+ Patch releases within 0.1 are intended to be compatible. Future 0.x minor releases may change APIs. Open an issue for bugs and usage questions. For a security concern, use the contact form to request a private reporting channel and keep credentials, tokens and submissions out of the public tracker. [Issues](https://github.com/ehven-consultants-npm-products/ehven-messaging-form-adapter-astro/issues) · [Contact](https://ehven.com/contact/)
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@ehven/messaging-form-adapter-astro",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "description": "Astro components for the portable Ehven messaging form.",
7
+ "exports": {
8
+ "./shell": "./src/MessagingFormShell.astro",
9
+ "./identity-fields": "./src/MessagingIdentityFields.astro",
10
+ "./messaging.css": "./src/messaging.css"
11
+ },
12
+ "files": [
13
+ "src/MessagingFormShell.astro",
14
+ "src/MessagingIdentityFields.astro",
15
+ "src/messaging.css",
16
+ "src/mount.mjs"
17
+ ],
18
+ "sideEffects": [
19
+ "**/*.astro",
20
+ "**/*.css",
21
+ "./src/mount.mjs"
22
+ ],
23
+ "engines": {
24
+ "node": ">=24"
25
+ },
26
+ "peerDependencies": {
27
+ "@ehven/messaging-form-core": "^0.1.0",
28
+ "astro": "^7.3.8"
29
+ },
30
+ "scripts": {
31
+ "check": "node --test test/package.test.mjs",
32
+ "check:astro": "node test/packed-astro.mjs"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/ehven-consultants-npm-products/ehven-messaging-form-adapter-astro.git"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "homepage": "https://github.com/ehven-consultants-npm-products/ehven-messaging-form-adapter-astro#readme",
42
+ "bugs": {
43
+ "url": "https://github.com/ehven-consultants-npm-products/ehven-messaging-form-adapter-astro/issues"
44
+ }
45
+ }
@@ -0,0 +1,101 @@
1
+ ---
2
+ import './messaging.css';
3
+
4
+ interface Props {
5
+ source: string;
6
+ privacyUrl?: string;
7
+ privacyText?: string;
8
+ turnstileSiteKey?: string;
9
+ endpoint: string;
10
+ class?: string;
11
+ disabled?: boolean;
12
+ submitLabel?: string;
13
+ variant?: string;
14
+ noun?: string;
15
+ formAttributes?: Record<string, string>;
16
+ }
17
+
18
+ const {
19
+ source,
20
+ endpoint,
21
+ class: className,
22
+ disabled = false,
23
+ submitLabel = 'Submit',
24
+ variant = 'messaging',
25
+ noun = 'submission',
26
+ formAttributes = {},
27
+ privacyUrl,
28
+ privacyText = 'I will use the information you provide to respond to your inquiry and manage our conversation.',
29
+ turnstileSiteKey = import.meta.env.PUBLIC_TURNSTILE_SITE_KEY,
30
+ } = Astro.props;
31
+
32
+ const unavailable = disabled || !turnstileSiteKey;
33
+ const submitted = Astro.url.searchParams.get(variant) === 'sent';
34
+ const prefix = `ehven-${variant}-form`;
35
+
36
+ const formClass = ['ehven-form', prefix, className]
37
+ .filter(Boolean)
38
+ .join(' ');
39
+ ---
40
+
41
+ <form
42
+ {...formAttributes}
43
+ class={formClass}
44
+ action={endpoint}
45
+ method="post"
46
+ data-ehven-form
47
+ data-messaging-astro
48
+ data-messaging-unavailable={unavailable ? '' : undefined}
49
+ data-noun={noun}
50
+ >
51
+ <input type="hidden" name="source" value={source} />
52
+
53
+ <slot />
54
+
55
+ <label class={`${prefix}__honeypot`} data-form-honeypot aria-hidden="true">
56
+ <span>Website</span>
57
+ <input
58
+ type="text"
59
+ name="website"
60
+ autocomplete="off"
61
+ tabindex="-1"
62
+ />
63
+ </label>
64
+
65
+ {!unavailable && (
66
+ <>
67
+ <div data-messaging-turnstile data-sitekey={turnstileSiteKey} data-action={variant}></div>
68
+ <p class={`${prefix}__status`} data-form-status-verification data-kind="error" aria-live="polite" hidden></p>
69
+ <noscript>Enable JavaScript to complete verification and send this form.</noscript>
70
+ </>
71
+ )}
72
+
73
+ <p class={`${prefix}__privacy`} data-form-privacy>
74
+ {privacyText} {privacyUrl && <a href={privacyUrl}>Privacy Policy</a>}
75
+ </p>
76
+
77
+ <p
78
+ id={`${variant}-status-${source}`}
79
+ class={`${prefix}__status`}
80
+ data-form-status
81
+ data-kind={submitted ? 'success' : unavailable ? 'error' : undefined}
82
+ aria-live="polite"
83
+ hidden={!submitted && !unavailable}
84
+ >
85
+ {submitted ? `Thank you. Your ${noun} has been sent.` : unavailable ? 'Form delivery is temporarily unavailable.' : ''}
86
+ </p>
87
+
88
+ <button
89
+ class="ehven-button ehven-button-active"
90
+ type="submit"
91
+ disabled
92
+ aria-disabled={unavailable ? 'true' : undefined}
93
+ title={unavailable ? 'Form delivery is temporarily unavailable' : undefined}
94
+ >
95
+ {submitLabel}
96
+ </button>
97
+ </form>
98
+
99
+ <script>
100
+ import './mount.mjs';
101
+ </script>
@@ -0,0 +1,53 @@
1
+ ---
2
+ import { MESSAGING_FIELD_LIMITS } from '@ehven/messaging-form-core';
3
+ interface Props { variant?: string; }
4
+ const { variant = 'messaging' } = Astro.props;
5
+ const prefix = `ehven-${variant}-form`;
6
+ ---
7
+ <div class={`${prefix}__pair`} data-form-pair>
8
+ <label class="ehven-field">
9
+ <span>First Name</span>
10
+ <input
11
+ type="text"
12
+ name="first_name"
13
+ autocomplete="given-name"
14
+ maxlength={MESSAGING_FIELD_LIMITS.firstName}
15
+ required
16
+ />
17
+ </label>
18
+
19
+ <label class="ehven-field">
20
+ <span>Last Name</span>
21
+ <input
22
+ type="text"
23
+ name="last_name"
24
+ autocomplete="family-name"
25
+ maxlength={MESSAGING_FIELD_LIMITS.lastName}
26
+ required
27
+ />
28
+ </label>
29
+ </div>
30
+
31
+ <div class={`${prefix}__pair`} data-form-pair>
32
+ <label class="ehven-field">
33
+ <span>Your Email</span>
34
+ <input
35
+ type="email"
36
+ name="email"
37
+ autocomplete="email"
38
+ maxlength={MESSAGING_FIELD_LIMITS.email}
39
+ required
40
+ />
41
+ </label>
42
+
43
+ <label class="ehven-field">
44
+ <span>Your Email Again</span>
45
+ <input
46
+ type="email"
47
+ name="email_confirmation"
48
+ autocomplete="off"
49
+ maxlength={MESSAGING_FIELD_LIMITS.email}
50
+ required
51
+ />
52
+ </label>
53
+ </div>
@@ -0,0 +1,51 @@
1
+ @layer forms {
2
+ .ehven-form {
3
+ inline-size: 100%;
4
+ max-inline-size: var(--ehven-measure-wide);
5
+ margin-inline: auto;
6
+ }
7
+
8
+ :where(.ehven-form) [data-form-pair] {
9
+ display: grid;
10
+ gap: var(--ehven-space-medium);
11
+ }
12
+
13
+ .ehven-form .ehven-field > span {
14
+ color: var(--ehven-color-text-emphasis-default);
15
+ font-size: var(--ehven-text-sm);
16
+ font-weight: 750;
17
+ line-height: 1.35;
18
+ }
19
+
20
+ :where(.ehven-form) [data-form-honeypot] {
21
+ position: absolute;
22
+ inline-size: 1px;
23
+ block-size: 1px;
24
+ overflow: hidden;
25
+ clip-path: inset(50%);
26
+ white-space: nowrap;
27
+ }
28
+
29
+ :where(.ehven-form) :is([data-form-status], [data-form-status-verification]) {
30
+ margin: 0;
31
+ }
32
+
33
+ :where(.ehven-form) :is([data-form-status], [data-form-status-verification])[data-kind='error'] {
34
+ color: var(--ehven-color-status-danger-text, #8b1a1a);
35
+ }
36
+
37
+ :where(.ehven-form) [data-form-status][data-kind='success'] {
38
+ color: var(--ehven-color-status-success-text, #176b35);
39
+ }
40
+
41
+ :where(.ehven-form) [data-form-privacy] {
42
+ color: var(--ehven-color-text-subtle, currentColor);
43
+ font-size: var(--ehven-text-sm);
44
+ }
45
+
46
+ @media (min-width: 40rem) {
47
+ :where(.ehven-form) [data-form-pair] {
48
+ grid-template-columns: repeat(2, minmax(0, 1fr));
49
+ }
50
+ }
51
+ }
package/src/mount.mjs ADDED
@@ -0,0 +1,133 @@
1
+ import { enhanceMessagingForm } from '@ehven/messaging-form-core/browser';
2
+
3
+ const scriptUrl = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
4
+ const onloadName = 'ehvenMessagingFormTurnstileLoaded';
5
+ const forms = new Map();
6
+ let loader;
7
+
8
+ function loadTurnstile() {
9
+ loader ??= new Promise((resolve, reject) => {
10
+ if (typeof window.turnstile?.render === 'function') {
11
+ resolve(window.turnstile);
12
+ return;
13
+ }
14
+
15
+ function cleanup() {
16
+ clearTimeout(timeout);
17
+ if (window[onloadName] === loaded) delete window[onloadName];
18
+ }
19
+
20
+ function fail(message) {
21
+ cleanup();
22
+ reject(new Error(message));
23
+ }
24
+
25
+ function loaded() {
26
+ const api = window.turnstile;
27
+ if (typeof api?.render !== 'function') {
28
+ fail('Turnstile could not load.');
29
+ return;
30
+ }
31
+ cleanup();
32
+ resolve(api);
33
+ }
34
+
35
+ const timeout = setTimeout(() => fail('Turnstile took too long to initialize.'), 15_000);
36
+ window[onloadName] = loaded;
37
+ const script = document.createElement('script');
38
+ script.src = `${scriptUrl}&onload=${onloadName}`;
39
+ script.async = true;
40
+ script.addEventListener('error', () => fail('Turnstile could not load.'), { once: true });
41
+ document.head.append(script);
42
+ });
43
+ return loader;
44
+ }
45
+
46
+ function mount(form) {
47
+ const button = form.querySelector('button[type="submit"]');
48
+ const status = form.querySelector('[data-form-status-verification]');
49
+ const widget = form.querySelector('[data-messaging-turnstile]');
50
+ if (!button || !status || !widget) return;
51
+
52
+ const events = new AbortController();
53
+ const noun = form.dataset.noun || 'submission';
54
+ let controller, api, widgetId;
55
+ let verified = false;
56
+
57
+ function setVerified(value) {
58
+ if (events.signal.aborted) return;
59
+ verified = value;
60
+ button.disabled = !verified || Boolean(controller?.pending);
61
+ if (verified) status.hidden = true;
62
+ }
63
+
64
+ function showUnavailable() {
65
+ setVerified(false);
66
+ status.textContent = 'Verification is temporarily unavailable. Reload the page to try again.';
67
+ status.hidden = false;
68
+ }
69
+
70
+ form.addEventListener('submit', event => {
71
+ if (!verified || controller?.pending) {
72
+ event.preventDefault();
73
+ event.stopImmediatePropagation();
74
+ }
75
+ }, { capture: true, signal: events.signal });
76
+
77
+ forms.set(form, () => {
78
+ events.abort();
79
+ controller?.destroy();
80
+ if (widgetId) {
81
+ try { api.remove(widgetId); } catch { /* The widget container may already be gone. */ }
82
+ }
83
+ });
84
+
85
+ void loadTurnstile().then(turnstile => {
86
+ if (events.signal.aborted || !form.isConnected) return;
87
+ api = turnstile;
88
+ controller = enhanceMessagingForm(form, {
89
+ messages: {
90
+ working: `Sending your ${noun}…`,
91
+ accepted: `Thank you. Your ${noun} has been sent.`,
92
+ },
93
+ verification: {
94
+ reset() {
95
+ verified = false;
96
+ queueMicrotask(() => {
97
+ if (events.signal.aborted) return;
98
+ setVerified(false);
99
+ try { api.reset(widgetId); } catch { showUnavailable(); }
100
+ });
101
+ },
102
+ },
103
+ });
104
+ widgetId = api.render(widget, {
105
+ sitekey: widget.dataset.sitekey,
106
+ action: widget.dataset.action,
107
+ theme: 'auto',
108
+ callback: () => setVerified(true),
109
+ 'expired-callback': () => setVerified(false),
110
+ 'timeout-callback': () => setVerified(false),
111
+ 'error-callback': () => { showUnavailable(); return true; },
112
+ 'unsupported-callback': showUnavailable,
113
+ });
114
+ if (!widgetId) throw new Error('Turnstile did not render.');
115
+ }).catch(() => {
116
+ if (!events.signal.aborted) showUnavailable();
117
+ });
118
+ }
119
+
120
+ function refreshForms() {
121
+ for (const [form, destroy] of forms) {
122
+ if (!form.isConnected) {
123
+ destroy();
124
+ forms.delete(form);
125
+ }
126
+ }
127
+ for (const form of document.querySelectorAll('form[data-messaging-astro]')) {
128
+ if (!forms.has(form) && !form.hasAttribute('data-messaging-unavailable')) mount(form);
129
+ }
130
+ }
131
+
132
+ refreshForms();
133
+ document.addEventListener('astro:page-load', refreshForms);