Package Exports
- ypsilon-event-handler
Readme
YpsilonEventHandler
A lightweight, flexible event delegation utility for modern web applications. Simplifies event management by centralizing listeners and providing advanced routing options.
YpsilonEventHandler uses browser-native APIs (stable since 2000) for reliable, no-dependency event handling. Works on file:// with zero build tools.
โจ Features
- Event Delegation Made Easy: One listener handles dynamic elements with scope-based routing.
- Automatic Target Resolution: Handles nested elements (e.g., SVGs in buttons).
- Throttle & Debounce Support: Built-in performance controls.
- Dynamic Listener Management: Add/remove events at runtime.
- Flexible Handler Resolution: Class methods, external maps, or globals.
- Multi-Handler System: Closest-match resolution for nested components.
- Performance Tracking: Optional metrics for optimization.
- No Dependencies:
5kB gzipped, enterprise-ready (800 LOC).
For advanced patterns (e.g., reactive state, super delegation), see README.USAGE.md. For internals, see README.TECHNICAL.md.
๐ Quick Start
No setup, no build step, no server, just include the file.
Get started in 30 seconds โ try it live on JSFiddle
<!DOCTYPE html>
<html>
<head><title>YEH Demo</title></head>
<body>
<div id="app">
<button data-action="save">Save</button>
<button data-action="delete">Delete</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/ypsilon-event-handler@latest"></script>
<script>
class MyHandler extends YpsilonEventHandler {
constructor() {
super({ '#app': ['click'] }); // Falls back to handleClick()
}
handleClick(event, target) {
const action = target.dataset.action;
if (action && this[action]) this[action](target, event);
}
save(target) { console.log('Saving...'); }
delete(target) { console.log('Deleting...'); }
}
new MyHandler(); // Adding listeners Done
</script>
</body>
</html>30-second setup: Create app.html, copy & paste the above code, then double-click to run.
๐ก Universal Delegation Pattern
One listener on parent +
custom-selector= handles unlimited elements within the parent
๐ฆ Installation
CDN (Instant Setup)
<script src="https://cdn.jsdelivr.net/npm/ypsilon-event-handler@1.8.2/ypsilon-event-handler.min.js"></script>npm (Build Tools)
npm install ypsilon-event-handlerDirect Download
Download latest version - works with file:// protocol
โ๏ธ Configuration Options
Pass a third argument to the constructor to enable advanced features:
| Option | Type | Default | Description |
|---|---|---|---|
enableStats |
boolean |
false |
Track performance metrics like event count and distance cache hits. |
methods |
object |
null |
External method map for organizing handlers by event type. |
enableGlobalFallback |
boolean |
false |
Fallback to global window functions when no method is found. |
methodsFirst |
boolean |
false |
Check methods object before class methods during handler resolution. |
passiveEvents |
array |
auto | Override default passive events (scroll, touch, wheel, pointer). |
abortController |
boolean |
false |
Enable AbortController support for programmatic listener removal. |
enableDistanceCache |
boolean |
true |
Cache DOM distance calculations for performance (multi-handler scenarios). |
Example: new YpsilonEventHandler(events, aliases, { enableStats: true });
๐ Fluent Chaining API
Chain operations for complex event orchestration:
App.on('data-ready', 'handleData')
.on('user-login', 'handleLogin')
.emit('init-complete', { loaded: true });๐งน Cleanup
handler.destroy(); or handler.abort(); (if enabled).
๐ Performance Metrics
With enableStats: true: console.log(handler.getStats());
๐ Browser Support
Chrome | Firefox | Safari | Edge - all modern versions
Works with legacy browsers via Webpack + Babel.
๐ Comparison vs Popular Libraries
| Feature | YpsilonEventHandler | EventEmitter3 | Redux Toolkit | jQuery |
|---|---|---|---|---|
| Bundle Size | 5kB gzipped | 7kB gzipped | 12kB+ gzipped | 30kB+ gzipped |
| Dependencies | โ Zero | โ Zero | โ Many | โ Zero |
| Event Delegation | โ Advanced | โ None | โ None | โ Basic |
| Multi-Handler System | โ Unique | โ None | โ None | โ None |
| Throttle/Debounce | โ Built-in | โ None | โ None | โ None |
| Native Browser API | โ Yes | โ No | โ No | โ No |
| Dynamic Element Support | โ Zero-config | โ None | โ None | โ Re-bind |
| TypeScript Support | โ Full | โ Partial | โ Full | โ ๏ธ Community |
| Memory Leak Prevention | โ Automatic | โ ๏ธ Manual | โ Automatic | โ ๏ธ Manual |
| Performance | โ Native | โ ๏ธ Synthetic | โ ๏ธ Virtual | โ ๏ธ Abstraction |
| Custom Event Dispatch | โ Built-in | โ Yes | โ Yes | โ Yes |
| Learning Curve | โ Low | โ Low | โ Steep | โ Familiar |
Why YpsilonEventHandler Stands Out
- Smallest footprint with advanced features like multi-handler delegation.
- Native performance using browser APIs, avoiding synthetic event overhead.
- Zero dependencies and automatic memory management for scalability.
- Built-in utilities (throttle, debounce, stats) eliminate external needs.
๐ See It In Action
Interactive Examples Hub ~ Beautiful landing page with all examples organized by category
Feature Demonstrations ~ Interactive examples of specific capabilities
๐ File Structure
/
โโโ ypsilon-event-handler.js # Main library
โโโ README.md # Quick start and core guide
โโโ README.USAGE.md # Advanced patterns and techniques
โโโ README.TECHNICAL.md # Implementation details and architectureLicense
MIT License โ free to use in personal or commercial projects.
๐ฅ Authors & Contributors
- Claude Van DOM - Implementation and optimization
- Engin Ypsilon - Original concept and architecture
- The Y-Team - Sunny DeepSeek & Herr Von Grokk