Home > Technology peripherals > It Industry > Brighten Up Your Astro Site with KwesForms and Rive

Brighten Up Your Astro Site with KwesForms and Rive

Joseph Gordon-Levitt
Release: 2025-02-08 09:07:09
Original
854 people have browsed it

This article demonstrates how to integrate KwesForms and Rive to create dynamic, interactive forms within Astro websites. Custom events trigger Rive animations based on form actions, enhancing user experience.

Brighten Up Your Astro Site with KwesForms and Rive

The process involves configuring KwesForms in Astro (using a script element or npm), creating a form with validation attributes, and setting up Rive (via Astro's Vite config and a downloaded or custom animation). Event listeners on form elements trigger Rive animation state changes.

Brighten Up Your Astro Site with KwesForms and Rive

A complete code example and live preview are available:

  • ? [Preview Link](Placeholder for Preview Link)
  • ⚙️ [Repository Link](Placeholder for Repository Link)

Getting Started:

Astro setup can be done via the CLI or manual installation. Create index.astro (in src/pages) and rive-form.astro (in src/components). Include rive-form in index.astro:

// src/pages/index.astro
---
import RiveForm from '../components/rive-form.astro';
---

<RiveForm />
Copy after login

KwesForms Integration:

KwesForms simplifies client-side and server-side form validation. Include the KwesForms script (using is:inline to prevent Astro processing):

// src/components/rive-form.astro
<🎜>
Copy after login

The form uses Tailwind CSS and includes data-kw-rules attributes for validation:

// src/components/rive-form.astro
<form id="riveForm" class="kwes-form flex flex-col gap-6" action="https://kwesforms.com/api/foreign/forms/abc123" data-kw-no-reload>
  {/* Form fields with data-kw-rules attributes */}
  <button type="submit">Send Message</button>
</form>
Copy after login

Rive Setup:

Rive animations are imported as .riv files. Configure Astro's astro.config.mjs:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  vite: {
    assetsInclude: ['**/*.riv'],
  },
});
Copy after login

Download a Rive animation (e.g., "Animated Login Character") and place it in the public directory. Initialize Rive in rive-form.astro:

// src/components/rive-form.astro
<🎜>

<🎜>
Copy after login

Brighten Up Your Astro Site with KwesForms and Rive

Event Handling and Animation:

Add event listeners to form elements (using KwesForms and standard JavaScript events). A getTrigger function accesses Rive state machines:

// ... inside the <script> tag in rive-form.astro
const getTrigger = (name) => {
  return r.stateMachineInputs('Login Machine').find((input) => input.name === name);
};

// Example event listener
form.addEventListener('kwHasErrors', () => {
  const trigger = getTrigger('trigFail');
  trigger.fire();
});
Copy after login

Brighten Up Your Astro Site with KwesForms and Rive

This approach combines KwesForms and Rive for visually engaging form interactions. Further details and support are available on Twitter/X: @PaulieScanlon. Learn more about Astro in the SitePoint Premium book, "Unleashing the Power of Astro."

Remember to replace the placeholder links with the actual links to the preview and repository. Also, ensure all code snippets are correctly integrated into your rive-form.astro file and that the paths to your Rive animation and other resources are accurate.

The above is the detailed content of Brighten Up Your Astro Site with KwesForms and Rive. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template