Convert Figma logo to code with AI

fabian-hiller logomodular-forms

The modular and type-safe form library for SolidJS, Qwik and Preact

1,065
55
1,065
108

Top Related Projects

📋 React Hooks for form state management and validation (Web + React Native)

34,041

Build forms in React, without the tears 😭

4,460

Performance-focused API for React forms 🚀

🏁 High performance subscription-based form state management for React

3,724

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

Quick Overview

Modular Forms is a lightweight and type-safe form library for React, Preact, and Solid. It provides a modular approach to form handling, offering flexibility and ease of use while maintaining strong typing support.

Pros

  • Type-safe: Leverages TypeScript for robust type checking and autocompletion
  • Modular: Allows for easy composition and customization of form elements
  • Framework-agnostic: Supports multiple popular JavaScript frameworks
  • Lightweight: Minimal bundle size impact on applications

Cons

  • Learning curve: May require some time to understand the modular approach
  • Limited ecosystem: Fewer third-party integrations compared to more established form libraries
  • Documentation: While improving, it may not be as comprehensive as some larger projects

Code Examples

Creating a basic form:

import { useForm, email, required } from '@modular-forms/react';

const LoginForm = () => {
  const [form, { Form, Field }] = useForm({
    initialValues: { email: '', password: '' },
    validate: {
      email: [required('Email is required'), email('Invalid email address')],
      password: required('Password is required'),
    },
  });

  return (
    <Form onSubmit={(values) => console.log(values)}>
      <Field name="email">
        {(field, props) => <input {...props} type="email" />}
      </Field>
      <Field name="password">
        {(field, props) => <input {...props} type="password" />}
      </Field>
      <button type="submit">Login</button>
    </Form>
  );
};

Using custom validation:

import { useForm } from '@modular-forms/react';

const customValidator = (value) => {
  if (value.length < 8) {
    return 'Password must be at least 8 characters long';
  }
  return null;
};

const [form, { Field }] = useForm({
  validate: {
    password: customValidator,
  },
});

Handling form submission:

import { useForm } from '@modular-forms/react';

const [form, { Form }] = useForm();

const handleSubmit = async (values) => {
  try {
    await submitToAPI(values);
    form.reset();
  } catch (error) {
    console.error('Submission failed:', error);
  }
};

return <Form onSubmit={handleSubmit}>{/* Form fields */}</Form>;

Getting Started

To start using Modular Forms, first install the package:

npm install @modular-forms/react
# or
yarn add @modular-forms/react

Then, import and use the library in your React component:

import { useForm } from '@modular-forms/react';

function MyForm() {
  const [form, { Form, Field }] = useForm();

  return (
    <Form onSubmit={(values) => console.log(values)}>
      <Field name="username">
        {(field, props) => <input {...props} />}
      </Field>
      <button type="submit">Submit</button>
    </Form>
  );
}

This sets up a basic form with a single field and a submit button. You can expand on this by adding more fields, validation, and custom logic as needed.

Competitor Comparisons

📋 React Hooks for form state management and validation (Web + React Native)

Pros of react-hook-form

  • More mature and widely adopted, with a larger community and ecosystem
  • Extensive documentation and examples available
  • Performance optimized for large forms with minimal re-renders

Cons of react-hook-form

  • Steeper learning curve due to more complex API
  • Less flexibility in form structure and validation rules
  • Heavier bundle size compared to modular-forms

Code Comparison

react-hook-form:

import { useForm } from "react-hook-form";

const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);

<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register("firstName")} />
  <input type="submit" />
</form>

modular-forms:

import { useForm } from "@modular-forms/react";

const [form, { Field }] = useForm();
const handleSubmit = (values) => console.log(values);

<form.Form onSubmit={handleSubmit}>
  <Field name="firstName">
    {(field) => <input {...field} />}
  </Field>
  <button type="submit">Submit</button>
</form.Form>

Both libraries aim to simplify form handling in React applications, but they differ in their approach and feature set. react-hook-form offers a more comprehensive solution with advanced features, while modular-forms provides a lighter, more flexible alternative with a focus on modularity and ease of use.

34,041

Build forms in React, without the tears 😭

Pros of Formik

  • More mature and widely adopted in the React ecosystem
  • Extensive documentation and community support
  • Integrates well with popular UI libraries and form validation schemas

Cons of Formik

  • Larger bundle size, which may impact performance in smaller applications
  • More opinionated approach, which can be limiting for complex custom forms
  • Steeper learning curve for beginners due to its comprehensive feature set

Code Comparison

Formik:

import { Formik, Form, Field } from 'formik';

<Formik initialValues={{ email: '' }} onSubmit={handleSubmit}>
  <Form>
    <Field name="email" type="email" />
    <button type="submit">Submit</button>
  </Form>
</Formik>

Modular Forms:

import { useForm, email } from '@modular-forms/react';

const [form, { Field }] = useForm({
  initialValues: { email: '' },
  onSubmit: handleSubmit,
});

<form use:form>
  <Field name="email" type="email" validate={email()} />
  <button type="submit">Submit</button>
</form>

Both libraries aim to simplify form management in React applications, but Formik offers a more comprehensive solution with wider adoption, while Modular Forms provides a lighter-weight alternative with a focus on modularity and performance.

4,460

Performance-focused API for React forms 🚀

Pros of Unform

  • More comprehensive form library with a wider range of form components
  • Better integration with React Native, supporting mobile development
  • Larger community and more frequent updates

Cons of Unform

  • Steeper learning curve due to its extensive API
  • Heavier bundle size compared to Modular Forms
  • Less focus on performance optimization

Code Comparison

Unform:

import { Form } from '@unform/web';
import Input from './components/Input';

function MyForm() {
  const handleSubmit = (data) => {
    console.log(data);
  };

  return (
    <Form onSubmit={handleSubmit}>
      <Input name="email" />
      <button type="submit">Submit</button>
    </Form>
  );
}

Modular Forms:

import { useForm } from '@modular-forms/react';

function MyForm() {
  const [form, { Field }] = useForm();

  const handleSubmit = (data) => {
    console.log(data);
  };

  return (
    <form onSubmit={form.handleSubmit(handleSubmit)}>
      <Field name="email">
        {(field) => <input {...field} />}
      </Field>
      <button type="submit">Submit</button>
    </form>
  );
}

🏁 High performance subscription-based form state management for React

Pros of react-final-form

  • More mature and widely adopted, with a larger community and ecosystem
  • Offers a more flexible API, allowing for complex form scenarios
  • Better documentation and examples available

Cons of react-final-form

  • Larger bundle size due to its extensive feature set
  • Steeper learning curve for beginners
  • Less focus on modern React patterns like hooks

Code Comparison

react-final-form:

import { Form, Field } from 'react-final-form'

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" />
    </form>
  )}
/>

modular-forms:

import { useForm, email, required } from 'modular-forms'

const [form, { Field }] = useForm({
  initialValues: { email: '' },
  validate: { email: [required(), email()] }
})

<form onSubmit={form.handleSubmit}>
  <Field name="email" />
</form>

modular-forms focuses on a more modern, hook-based approach with built-in validation, while react-final-form offers a render prop pattern with separate validation logic. modular-forms provides a more concise API for simple forms, but react-final-form's flexibility may be advantageous for complex scenarios.

3,724

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

Pros of TanStack Form

  • Framework-agnostic, supporting React, Vue, Solid, and Svelte
  • More comprehensive feature set, including advanced validation and error handling
  • Larger community and ecosystem, with more frequent updates and contributions

Cons of TanStack Form

  • Steeper learning curve due to its more complex API and extensive features
  • Larger bundle size, which may impact performance in smaller projects
  • Less opinionated, requiring more setup and configuration

Code Comparison

Modular Forms:

import { useForm } from '@modular-forms/react';

const form = useForm({
  initialValues: { name: '', email: '' },
  onSubmit: values => console.log(values),
});

TanStack Form:

import { useForm } from '@tanstack/react-form';

const form = useForm({
  defaultValues: { name: '', email: '' },
  onSubmit: async values => console.log(values),
});

Both libraries offer similar basic functionality, but TanStack Form provides more advanced features and configuration options out of the box. Modular Forms focuses on simplicity and ease of use, while TanStack Form offers greater flexibility and power at the cost of increased complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Modular Forms

Modular Forms is a JavaScript library built on SolidJS, Qwik, Preact and React to validate and handle various types of forms. It is type-safe, fast by default and the bundle size is small due to a modular design. Try it out in our playground!

✨ Highlights

  • Small bundle size starting at 3 KB
  • It's fast – DOM updates are fine-grained
  • Type safety with autocompletion in editor
  • Validate everything from emails to files
  • Minimal, readable and well thought out API
  • Progressively enhanced forms with actions
  • Modular design – only use what you need
  • No dependencies – except for the framework
  • It's headless – you define the visual
  • Supports all native HTML form fields

🏔 Mission

The mission of Modular Forms is to gather the latest available experience and knowledge to provide the best possible form library for JavaScript frameworks. We pay special attention to type safety, bundle size, performance and input validation. You can expect that every decision we make during development is well thought out and always takes these main features into account.

🪐 Vision

At the time of the initial development of Modular Forms, there was no form library with comparable type safety, bundle size and performance. However, many of the ideas came from previous open source projects and only the development of the last few years has made a form library in this way possible. The vision of Modular Forms is to shape the future of the web together and to share our knowledge and experience with the open source community.

🙌🏼 Credits

When creating Modular Forms, I took inspiration from React Hook Form and Formik. I also want to thank Ryan Carniato for his inspiring streams with exclusive and deep insights into different technologies. Furthermore, I would like to thank David Di Biase, Miško Hevery and Manu Martínez-Almeida for the support I received during the initial development.

✏️ Feedback

Find a bug or have an idea how to improve the library? Please fill out an issue. Together we can make the library even better!

🔑 License

This project is available free of charge and licensed under the MIT License.

NPM DownloadsLast 30 Days