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, content management, and element lifecycle through a simple configuration object.
- 🚀 Easy Setup: Create and manage HTML elements with a simple, intuitive config
- ♿ Built-in Accessibility: ARIA attributes handled automatically
- 🔒 Type Safe: Ensures valid elements and attributes at runtime
- 📦 Modular: Features are isolated for easy debugging and maintenance
- 🎛️ Event Handling: Simplified event management with auto-cleanup
- ⚡ State Management: Supports boolean attributes with proper HTML5 compliance
- 🎨 Style Handling: Clean API with automatic kebab-case conversion
- 👶 Child Elements: Easy creation, management, and cleanup of child elements
- 🔄 Lifecycle Control: Manages creation, updates, and destruction
- 🌀 Cloning: Supports deep and shallow cloning with config preservation
- 🧹 Memory Safe: Auto-cleans events and references when elements are removed
Using npm:
npm install wences
Using yarn:
yarn add wences
Using bun:
bun install wences
import Wences from 'wences';
// Create a button with various configurations
const button = new Wences('button', {
accessibility: {
label: 'Submit form',
role: 'button',
describedBy: 'description'
},
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: true,
hidden: false
}
});
// Create and append child elements
const icon = button.appendChild('span', {
general: { class: 'icon' }
});
// Append to a parent element
button.appendTo('#form-container');
Handle accessibility attributes without requiring the 'aria-' prefix:
{
accessibility: {
// Standard role attribute (remains unchanged)
role: 'button',
// Aria attributes (automatically prefixed with 'aria-')
label: 'Submit button', // becomes aria-label
describedBy: 'description', // becomes aria-describedby
current: 'page', // becomes aria-current
expanded: 'false', // becomes aria-expanded
hidden: 'true' // becomes aria-hidden
}
}
Manage child elements and content nodes:
{
contents: {
// Array of valid DOM nodes
children: [
document.createElement('div'),
document.createTextNode('Hello'),
document.createElement('svg'),
new Text('World')
]
}
}
Handle DOM event listeners with automatic cleanup:
{
events: {
// Standard DOM events
click: (e) => console.log('Clicked'),
mouseenter: (e) => console.log('Mouse entered'),
// Events with 'on' prefix (automatically normalized)
onChange: (e) => console.log('Changed'),
// Custom events
'custom-event': (e) => console.log('Custom event fired')
}
}
Manage standard HTML attributes:
{
general: {
// Standard HTML attributes
id: 'my-element',
class: 'button primary',
name: 'submit-button',
// Data attributes
'data-test': 'value',
'data-user-id': '123',
// Custom attributes
title: 'My Button',
lang: 'en'
}
}
Handle boolean attributes following HTML5 specifications:
{
state: {
// Standard boolean attributes
disabled: true, // adds disabled attribute
hidden: false, // removes hidden attribute
required: true, // adds required attribute
checked: false, // removes checked attribute
}
}
Valid boolean attributes include: async
, autofocus
, autoplay
, checked
, disabled
, hidden
, multiple
, readonly
, required
, selected
, and more.
Manage inline styles with support for camelCase and kebab-case:
{
style: {
// CamelCase properties
backgroundColor: '#fff',
fontSize: '16px',
marginTop: '10px',
// Kebab-case properties
'background-color': '#fff',
'font-size': '16px',
'margin-top': '10px',
// Transforms and animations
transform: 'translateX(10px)',
transition: 'all 0.3s ease'
}
}
new Wences(tagName: string, config: WencesConfig)
Returns the underlying DOM element.
const element = wencesInstance.getElement();
Creates and appends a child Wences element.
const child = wencesInstance.appendChild('div', {
style: { color: 'red' }
});
Appends the element to a parent element or selector.
wencesInstance.appendTo('#container');
Removes the element and cleans up all event listeners and references.
wencesInstance.destroy();
Updates the configuration of the element.
wencesInstance.update({
style: { color: 'blue' },
state: { disabled: true }
});
Creates a clone of the current Wences instance.
const clone = wencesInstance.clone(true); // Deep clone
Gets the current configuration of the element.
const config = wencesInstance.getConfig();
For TypeScript users:
interface WencesConfig {
accessibility?: {
[key: string]: string;
role?: string;
};
contents?: {
children: (HTMLElement | SVGElement | Text)[];
};
events?: {
[key: string]: (event: Event) => void;
};
general?: {
[key: string]: string;
};
state?: {
[key: string]: boolean;
};
style?: {
[key: string]: string;
};
}
Wences includes comprehensive runtime validation and will throw errors for:
- Invalid HTML tag names
- Invalid children types (must be HTML elements or text nodes)
- Invalid parent elements when using appendTo()
- Invalid configuration object structure
- Invalid event handlers
- Invalid style values
- Memory leaks (via console warnings)
- Always Destroy Elements: Call
destroy()
when removing elements to prevent memory leaks - Use Child Management: Prefer
appendChild()
over direct DOM manipulation - Config Updates: Use
update()
instead of direct property manipulation - Error Handling: Wrap Wences operations in try-catch blocks for proper error handling
- Configuration Storage: Use
getConfig()
to store/restore element states
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
# Install dependencies
npm install
# Run tests
npm test
# Build
npm run build
# Run linter
npm run lint
This project is licensed under the MIT License - see the LICENSE.md file for details.
For support, please:
- Check the documentation
- Search existing issues
- Open a new issue
Made with ❤️ by Chessurisme