@ehven/messaging-form-adapter-astro 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +38 -2
- package/package.json +43 -4
- package/src/MessagingFormShell.astro +101 -0
- package/src/MessagingIdentityFields.astro +53 -0
- package/src/messaging.css +51 -0
- package/src/mount.mjs +126 -0
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
|
-
#
|
|
1
|
+
# Ehven Messaging Form for Astro
|
|
2
2
|
|
|
3
|
-
|
|
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.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
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,126 @@
|
|
|
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 forms = new Map();
|
|
5
|
+
let loader;
|
|
6
|
+
|
|
7
|
+
function loadTurnstile() {
|
|
8
|
+
loader ??= new Promise((resolve, reject) => {
|
|
9
|
+
const timeout = setTimeout(() => reject(new Error('Turnstile took too long to initialize.')), 15_000);
|
|
10
|
+
function ready() {
|
|
11
|
+
const api = window.turnstile;
|
|
12
|
+
if (typeof api?.ready !== 'function') {
|
|
13
|
+
failed();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
api.ready(() => {
|
|
17
|
+
clearTimeout(timeout);
|
|
18
|
+
resolve(api);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function failed() {
|
|
22
|
+
clearTimeout(timeout);
|
|
23
|
+
reject(new Error('Turnstile could not load.'));
|
|
24
|
+
}
|
|
25
|
+
if (window.turnstile) {
|
|
26
|
+
ready();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const script = document.createElement('script');
|
|
30
|
+
script.src = scriptUrl;
|
|
31
|
+
script.async = true;
|
|
32
|
+
script.addEventListener('load', ready, { once: true });
|
|
33
|
+
script.addEventListener('error', failed, { once: true });
|
|
34
|
+
document.head.append(script);
|
|
35
|
+
});
|
|
36
|
+
return loader;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function mount(form) {
|
|
40
|
+
const button = form.querySelector('button[type="submit"]');
|
|
41
|
+
const status = form.querySelector('[data-form-status-verification]');
|
|
42
|
+
const widget = form.querySelector('[data-messaging-turnstile]');
|
|
43
|
+
if (!button || !status || !widget) return;
|
|
44
|
+
|
|
45
|
+
const events = new AbortController();
|
|
46
|
+
const noun = form.dataset.noun || 'submission';
|
|
47
|
+
let controller, api, widgetId;
|
|
48
|
+
let verified = false;
|
|
49
|
+
|
|
50
|
+
function setVerified(value) {
|
|
51
|
+
if (events.signal.aborted) return;
|
|
52
|
+
verified = value;
|
|
53
|
+
button.disabled = !verified || Boolean(controller?.pending);
|
|
54
|
+
if (verified) status.hidden = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function showUnavailable() {
|
|
58
|
+
setVerified(false);
|
|
59
|
+
status.textContent = 'Verification is temporarily unavailable. Reload the page to try again.';
|
|
60
|
+
status.hidden = false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
form.addEventListener('submit', event => {
|
|
64
|
+
if (!verified || controller?.pending) {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
event.stopImmediatePropagation();
|
|
67
|
+
}
|
|
68
|
+
}, { capture: true, signal: events.signal });
|
|
69
|
+
|
|
70
|
+
forms.set(form, () => {
|
|
71
|
+
events.abort();
|
|
72
|
+
controller?.destroy();
|
|
73
|
+
if (widgetId) {
|
|
74
|
+
try { api.remove(widgetId); } catch { /* The widget container may already be gone. */ }
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
void loadTurnstile().then(turnstile => {
|
|
79
|
+
if (events.signal.aborted || !form.isConnected) return;
|
|
80
|
+
api = turnstile;
|
|
81
|
+
controller = enhanceMessagingForm(form, {
|
|
82
|
+
messages: {
|
|
83
|
+
working: `Sending your ${noun}…`,
|
|
84
|
+
accepted: `Thank you. Your ${noun} has been sent.`,
|
|
85
|
+
},
|
|
86
|
+
verification: {
|
|
87
|
+
reset() {
|
|
88
|
+
verified = false;
|
|
89
|
+
queueMicrotask(() => {
|
|
90
|
+
if (events.signal.aborted) return;
|
|
91
|
+
setVerified(false);
|
|
92
|
+
try { api.reset(widgetId); } catch { showUnavailable(); }
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
widgetId = api.render(widget, {
|
|
98
|
+
sitekey: widget.dataset.sitekey,
|
|
99
|
+
action: widget.dataset.action,
|
|
100
|
+
theme: 'auto',
|
|
101
|
+
callback: () => setVerified(true),
|
|
102
|
+
'expired-callback': () => setVerified(false),
|
|
103
|
+
'timeout-callback': () => setVerified(false),
|
|
104
|
+
'error-callback': () => { showUnavailable(); return true; },
|
|
105
|
+
'unsupported-callback': showUnavailable,
|
|
106
|
+
});
|
|
107
|
+
if (!widgetId) throw new Error('Turnstile did not render.');
|
|
108
|
+
}).catch(() => {
|
|
109
|
+
if (!events.signal.aborted) showUnavailable();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function refreshForms() {
|
|
114
|
+
for (const [form, destroy] of forms) {
|
|
115
|
+
if (!form.isConnected) {
|
|
116
|
+
destroy();
|
|
117
|
+
forms.delete(form);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const form of document.querySelectorAll('form[data-messaging-astro]')) {
|
|
121
|
+
if (!forms.has(form) && !form.hasAttribute('data-messaging-unavailable')) mount(form);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
refreshForms();
|
|
126
|
+
document.addEventListener('astro:page-load', refreshForms);
|