Wences is a lightweight, modular JavaScript utility for creating and managing HTML elements with a clean, declarative API. It provides a structured way to handle HTML attributes, accessibility, events, and content management through a simple configuration object.
- Declarative API: Create HTML elements with a simple, intuitive configuration object
- Built-in Accessibility: Automatic handling of ARIA attributes without prefix requirements
- Type Safety: Runtime validation for HTML elements and attributes
- Modular Architecture: Each feature is isolated in its own module for easy debugging and maintenance
- Event Management: Simplified event handling with automatic 'on' prefix normalization
- State Management: Built-in support for boolean attributes with proper HTML5 compliance
- Style Handling: Clean API for managing inline styles with automatic kebab-case conversion
Using npm:
npm install wences
Using yarn:
yarn add wences
import Wences from 'wences';
// Create a button with various configurations
const button = new Wences('button', {
accessibility: {
label: 'Submit form', // Becomes aria-label
role: 'button', // Stays as role
describedBy: 'description' // Becomes aria-describedby
},
general: {
class: 'primary-button',
id: 'submit-btn'
},
style: {
backgroundColor: 'blue',
color: 'white'
},
events: {
click: () => console.log('Button clicked'),
mouseenter: () => console.log('Mouse entered')
},
state: {
disabled: false
},
contents: {
children: [
document.createElement('span') // Must be HTML elements
]
}
});
// Append to a parent element
button.appendTo('#form-container');
Handle accessibility attributes without requiring the 'aria-' prefix:
{
accessibility: {
label: 'Submit', // becomes aria-label
role: 'button', // remains as role
describedBy: 'desc', // becomes aria-describedby
expanded: 'false' // becomes aria-expanded
}
}
Set any standard HTML attributes:
{
general: {
class: 'my-class',
id: 'my-id',
name: 'my-name',
title: 'My Title'
}
}
Apply inline styles using camelCase properties:
{
style: {
backgroundColor: '#fff',
fontSize: '16px',
marginTop: '10px'
}
}
Attach event listeners (with or without 'on' prefix):
{
events: {
click: () => {}, // both work
onClick: () => {}, // both work
mouseenter: () => {},
touchstart: () => {}
}
}
Manage boolean attributes:
{
state: {
disabled: true,
required: false,
checked: true
}
}
Add child elements:
{
contents: {
children: [document.createElement('div'), document.createElement('span')];
}
}
new Wences(tagName: string, config: WencesConfig)
Returns the underlying DOM element.
const element = wencesInstance.getElement();
Wences includes runtime validation and will throw errors for:
- Invalid HTML tag names
- Invalid children types (must be HTML elements)
- Invalid parent elements when using appendTo()
- Invalid configuration object structure
Wences supports all modern browsers:
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
We welcome contributions! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE.md file for details.
For support, please:
- Check the documentation
- Open an issue
- Inspired by modern web development practices
- Built with accessibility in mind
- Designed for developer experience