Component overview
TheContactanos component provides a full-featured contact form with:
- Four input fields (name, last name, email, phone)
- Message textarea
- Form validation
- Loading state during submission
- Success/error notifications
- Integration with backend API
src/components/Contactanos.astro
Form structure
The form includes five fields:Form submission handling
The form uses client-side JavaScript to handle submissions asynchronously:The form prevents default submission behavior and uses
fetch() to send data asynchronously, providing a better user experience without page reloads.Loading states
The submit button shows visual feedback during form submission:| State | Button Text | Disabled |
|---|---|---|
| Initial | ”Enviar” | No |
| Submitting | ”Enviando…” | Yes |
| Complete | ”Enviar” | No |
SweetAlert2 integration
The component uses SweetAlert2 for beautiful alert notifications:Success notification
Success notification
Error notification
Error notification
API endpoint integration
The form sends data to the/api/contact endpoint:
Endpoint: src/pages/api/contact.ts
Request format
Server-side validation
The API endpoint performs comprehensive validation:Complete API endpoint code
Layout and styling
The form is wrapped in a centered container with amber background:Responsive width
| Breakpoint | Form Width |
|---|---|
| Mobile | 100% |
| Large (lg) | 50% |
Input styling
All form inputs use consistent styling:- Semi-transparent black background (5% opacity)
- Indigo outline on focus
- Gray placeholder text
- Rounded corners
WhatsApp fallback
The component includes a<Whatsapp /> component for alternative contact:
Usage in pages
src/pages/index.astro
Accessibility features
- Label associations: All inputs have associated
<label>elements - Autocomplete: Proper autocomplete attributes for browser autofill
- Semantic HTML: Uses
<form>element for proper form submission - Focus indicators: Clear visual focus states on all inputs
- Error messaging: SweetAlert2 provides accessible modal dialogs
Related resources
Product catalog
View products customers can inquire about
Animations
Learn about page animations and transitions
