Skip to content

Commit 356b0d0

Browse files
lpssformspreeclaude
andcommitted
Add ajax-demo example and update @formspree/ajax API
- Replace formEndpoint with simpler formId + optional origin config - Add Vite-based demo app with contact form UI - Include loading states, error handling, and form validation feedback - Export appendExtraData from @formspree/core for extra form data Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9f16451 commit 356b0d0

File tree

12 files changed

+825
-332
lines changed

12 files changed

+825
-332
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Formspree Configuration
2+
# Copy this file to .env.local and add your form ID
3+
VITE_FORMSPREE_FORM_ID=YOUR_FORM_ID

examples/ajax-demo/.eslintrc.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root: true
2+
extends:
3+
- '../../.eslintrc.yml'
4+
parserOptions:
5+
project: './tsconfig.json'

examples/ajax-demo/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Formspree AJAX Demo
2+
3+
A pure JavaScript/TypeScript example demonstrating how to use `@formspree/ajax` for form submissions without any frontend framework.
4+
5+
## Features
6+
7+
- Pure HTML/CSS/TypeScript implementation
8+
- No React, Vue, or other framework dependencies
9+
- Beautiful, responsive contact form UI
10+
- Loading states and error handling
11+
- Form validation feedback
12+
- Success/error messages
13+
- Built with Vite for fast development
14+
15+
## Setup
16+
17+
1. Install dependencies:
18+
19+
```bash
20+
yarn install
21+
```
22+
23+
2. Get your Formspree form ID:
24+
25+
- Go to [formspree.io](https://formspree.io)
26+
- Create a new form or use an existing one
27+
- Copy your form ID (e.g., `xyzabc123`)
28+
29+
3. Create a `.env.local` file from the template:
30+
31+
```bash
32+
cp .env.local.template .env.local
33+
```
34+
35+
4. Update `VITE_FORMSPREE_FORM_ID` in `.env.local` with your form ID.
36+
37+
## Development
38+
39+
Run the development server:
40+
41+
```bash
42+
yarn dev
43+
```
44+
45+
The demo will be available at `http://localhost:5173`
46+
47+
## Build
48+
49+
Build for production:
50+
51+
```bash
52+
yarn build
53+
```
54+
55+
The output will be in the `dist` directory.
56+
57+
## Usage
58+
59+
### Basic Form Initialization
60+
61+
```typescript
62+
import { initForm } from '@formspree/ajax';
63+
64+
initForm({
65+
formElement: '#contact-form',
66+
formId: 'your-form-id',
67+
onSuccess: ({ form }) => {
68+
console.log('Success!');
69+
form.reset();
70+
},
71+
onError: (_context, error) => {
72+
const messages = error.getFormErrors().map((e) => e.message);
73+
console.error('Validation errors:', messages);
74+
},
75+
onFailure: (_context, error) => {
76+
console.error('Unexpected error:', error);
77+
},
78+
});
79+
```
80+
81+
### With Extra Data
82+
83+
```typescript
84+
initForm({
85+
formElement: '#contact-form',
86+
formId: 'your-form-id',
87+
data: {
88+
source: 'website',
89+
timestamp: new Date().toISOString(),
90+
},
91+
onSuccess: ({ form }) => {
92+
form.reset();
93+
},
94+
});
95+
```
96+
97+
### Custom Origin (e.g., staging)
98+
99+
```typescript
100+
initForm({
101+
formElement: '#contact-form',
102+
formId: 'your-form-id',
103+
origin: 'https://staging.formspree.io',
104+
debug: true,
105+
});
106+
```
107+
108+
## File Structure
109+
110+
```
111+
ajax-demo/
112+
├── public/
113+
│ ├── index.html # HTML form with styling
114+
│ └── main.ts # TypeScript implementation
115+
├── .env.local.template # Environment variables template
116+
├── package.json
117+
├── tsconfig.json
118+
├── vite.config.ts
119+
└── README.md
120+
```
121+
122+
## Learn More
123+
124+
- [Formspree Documentation](https://help.formspree.io/)
125+
- [@formspree/ajax Package](../../packages/formspree-ajax)

0 commit comments

Comments
 (0)