Skip to content

noundry/Elements

Repository files navigation

Noundry Elements

A powerful, lightweight form validation library for modern web applications

Noundry Elements is a plain JavaScript form validation library inspired by Informel, built specifically for vanilla JavaScript and Alpine.js integration. It provides comprehensive form validation, state management, and seamless user experience without the overhead of heavy frameworks.

πŸš€ Features

  • βœ… Web Component Architecture - Custom <noundry-element> tag
  • βœ… Native HTML5 Validation - Leverages built-in browser validation
  • βœ… Custom Validation Rules - Add your own validation logic
  • βœ… Real-time State Tracking - Monitor dirty, valid, and submitting states
  • βœ… Automatic Error Display - Creates and manages error message elements
  • βœ… AJAX Form Submission - Built-in fetch API integration
  • βœ… Alpine.js Integration - Seamless reactive integration
  • βœ… Framework Agnostic - Works with any JavaScript framework or vanilla JS
  • βœ… Accessibility Ready - ARIA live regions and screen reader support
  • βœ… TypeScript Ready - Full TypeScript support included
  • βœ… Lightweight - ~8KB minified, zero dependencies

πŸ“¦ Installation

NPM (Recommended)

npm install @noundryfx/noundry-elements

View on npmjs.com: https://www.npmjs.com/package/@noundryfx/noundry-elements

CDN

<script src="https://unpkg.com/@noundryfx/noundry-elements@latest/noundry-elements.js"></script>

Download

Download the noundry-elements.js file and include it in your project.

🏁 Quick Start

1. Basic HTML Setup

<!DOCTYPE html>
<html>
<head>
    <title>My Form</title>
</head>
<body>
    <noundry-element>
        <form action="/api/contact" method="POST">
            <input type="text" name="name" required data-error="Name is required">
            <input type="email" name="email" required data-error="Valid email required">
            <button type="submit">Submit</button>
        </form>
    </noundry-element>

    <script src="noundry-elements.js"></script>
</body>
</html>

2. With Alpine.js

<div x-data="{ formData: {}, formState: {} }">
    <noundry-element 
        x-ref="myForm"
        @noundry-change="formData = $event.detail.values; formState = $refs.myForm.$noundry"
    >
        <form>
            <input type="text" name="username" x-model="formData.username">
            <button :disabled="!formState.isValid">Submit</button>
        </form>
    </noundry-element>
</div>

3. JavaScript API

const formElement = document.querySelector('noundry-element');

// Listen for events
formElement.addEventListener('noundry-submit', (e) => {
    console.log('Form data:', e.detail.values);
});

// Access form state
const state = formElement.$noundry;
console.log(state.isValid, state.isDirty, state.errors);

// Programmatic control
formElement.setValues({ name: 'John Doe' });
formElement.reset();

πŸ“š Documentation

Core Documentation

Live Examples

🎯 Key Concepts

Attributes

Configure form behavior using HTML attributes:

Attribute Description
error-disable-submit Disable submit button when form has validation errors
reset-on-submit Automatically reset form after successful submission
default-submit Allow default form submission behavior (no AJAX)

Events

Listen for form lifecycle events:

Event Description Detail Properties
noundry-input Fired on input events field, value, values
noundry-change Fired on change events field, value, values
noundry-submit Fired on form submission values, formData
noundry-invalid Fired when validation fails errors
noundry-reset Fired when form is reset -
noundry-request-start AJAX request started -
noundry-request-success AJAX request succeeded response
noundry-request-error AJAX request failed error

State Properties

Access real-time form state via $noundry:

const form = document.querySelector('noundry-element');
const state = form.$noundry;

// State properties
state.values        // Current form values (object)
state.errors        // Current validation errors (object)
state.isDirty       // Has form been modified? (boolean)
state.isValid       // Is form currently valid? (boolean)
state.isSubmitting  // Is form being submitted? (boolean)

πŸ› οΈ Advanced Usage

Custom Validation

formElement.setValidationRule('password', (value, allValues) => {
    if (!value || value.length < 8) {
        return 'Password must be at least 8 characters';
    }
    if (!/[A-Z]/.test(value)) {
        return 'Password must contain uppercase letter';
    }
    return null; // Valid
});

Error Message Customization

<!-- Custom error messages -->
<input 
    type="email" 
    name="email" 
    required
    data-error="Please enter a valid email address"
    data-error-typemismatch="Email format is incorrect"
    data-error-valuemissing="Email is required"
>

Alpine.js Reactive Integration

<div x-data="contactForm()">
    <noundry-element 
        x-ref="form"
        @noundry-input="updateState"
        @noundry-submit="handleSubmit"
    >
        <form>
            <input type="text" name="name" x-model="formData.name">
            <div x-show="formState.errors.name" x-text="formState.errors.name"></div>
            <button :disabled="!formState.isValid">Submit</button>
        </form>
    </noundry-element>
</div>

<script>
function contactForm() {
    return {
        formData: {},
        formState: {},
        updateState() {
            this.formState = this.$refs.form.$noundry;
            this.formData = this.formState.values;
        },
        handleSubmit(event) {
            console.log('Submitted:', event.detail.values);
        }
    };
}
</script>

🎨 Styling

Style form states using CSS attribute selectors:

/* Form states */
noundry-element[data-dirty="true"] { border-left: 4px solid orange; }
noundry-element[data-valid="true"] { border-left: 4px solid green; }
noundry-element[data-valid="false"] { border-left: 4px solid red; }
noundry-element[data-submitting="true"] { opacity: 0.7; }

/* Error messages */
.noundry-error {
    color: #ef4444;
    font-size: 0.875rem;
    margin-top: 0.25rem;
}

/* Custom styling for different validation states */
input:invalid { border-color: #ef4444; }
input:valid { border-color: #10b981; }

🌐 Browser Support

  • Chrome/Edge: 67+
  • Firefox: 63+
  • Safari: 10.3+
  • All modern browsers with Custom Elements v1 support

For older browsers, include the Web Components polyfill.

πŸ”§ Configuration Options

Form Element Attributes

<noundry-element
    error-disable-submit    <!-- Disable submit on errors -->
    reset-on-submit        <!-- Reset after successful submission -->
    default-submit="false" <!-- Control AJAX behavior -->
>
    <form>...</form>
</noundry-element>

Input Field Configuration

<!-- Basic validation -->
<input type="text" name="username" required minlength="3">

<!-- Custom error messages -->
<input 
    type="password" 
    name="password" 
    required
    minlength="8"
    data-error="Password is required"
    data-error-tooshort="Password must be at least 8 characters"
>

<!-- Pattern validation -->
<input 
    type="tel" 
    name="phone"
    pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
    data-error-patternmismatch="Phone format: 123-456-7890"
>

<!-- Custom error placement -->
<input type="email" name="email" required>
<div data-error-for="email" class="custom-error-style"></div>

πŸ“‹ Form Element Types

Noundry Elements supports all HTML5 input types and form controls:

Text Inputs

  • text, email, url, tel, password
  • search, number, range
  • date, time, datetime-local, month, week

Selection Inputs

  • radio, checkbox
  • select (single and multiple)
  • datalist

File Inputs

  • file (single and multiple)
  • File size and type validation

Text Areas

  • textarea with character counting
  • Auto-resize functionality

Specialized Inputs

  • hidden fields
  • color picker
  • Custom validation rules for any input type

πŸš€ Performance

  • Lightweight: ~8KB minified (3KB gzipped)
  • Zero Dependencies: No external libraries required
  • Lazy Validation: Only validates on user interaction
  • Efficient DOM Updates: Minimal DOM manipulation
  • Memory Efficient: Proper cleanup on element removal

πŸ”’ Security

  • XSS Protection: Sanitized error message display
  • CSRF Ready: Works with CSRF tokens
  • Content Security Policy: Compatible with strict CSP
  • No Eval: No dynamic code execution

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Add tests for new functionality
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

Development Setup

git clone https://github.com/your-username/noundry-elements.git
cd noundry-elements
npm install
npm run dev      # Start development server
npm run test     # Run tests
npm run build    # Build for production

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ†š Comparison

Feature Noundry Elements Informel React Hook Form Formik
Framework Vanilla JS + Alpine.js Svelte React React
Bundle Size 8KB 12KB 25KB 45KB
Dependencies Zero Svelte React React + Yup
Custom Elements βœ… βœ… ❌ ❌
Alpine.js Integration βœ… ❌ ❌ ❌
TypeScript βœ… βœ… βœ… βœ…
Form Arrays ⚠️ (planned) βœ… βœ… βœ…
Schema Validation ⚠️ (planned) βœ… (Zod) βœ… (Yup) βœ… (Yup)

πŸ’‘ FAQ

Q: Can I use Noundry Elements with React/Vue/Angular?
A: Yes! As a Web Component, it works with any framework. However, framework-specific form libraries might be more idiomatic.

Q: Does it support nested forms or form arrays?
A: Basic nested object support is included. Advanced form arrays are planned for v2.0.

Q: Can I customize the error message styling?
A: Yes! Error elements use the .noundry-error class and can be fully customized via CSS.

Q: Is server-side validation supported?
A: Yes! Listen for noundry-submit events and add server validation errors programmatically.

Q: Can I disable AJAX and use traditional form submission?
A: Yes! Add default-submit="true" to allow traditional form submission.

🎯 Roadmap

v1.1 (Current)

  • βœ… Core validation and state management
  • βœ… Alpine.js integration
  • βœ… File upload support
  • βœ… Custom validation rules

v1.2 (Next)

  • πŸ”„ Schema validation (Zod integration)
  • πŸ”„ Form arrays and nested forms
  • πŸ”„ Enhanced TypeScript definitions
  • πŸ”„ React integration helpers

v2.0 (Future)

  • πŸ“‹ Visual form builder
  • πŸ“‹ Advanced field types
  • πŸ“‹ Multi-language support
  • πŸ“‹ Performance optimizations

Made with ❀️ for the modern web

GitHub β€’ Documentation β€’ Examples β€’ Discord

About

Noundry Elements is a plain JavaScript form validation library inspired by Informe, built specifically for vanilla JavaScript and Alpine.js integration. It provides comprehensive form validation, state management, and seamless user experience without the overhead of heavy frameworks.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors