@amirafa/xevent
v1.0.2
Published
DOM-native event extensions
Maintainers
Readme
@amirafa/xevent
xevent is a lightweight, DOM-first utility library that adds extra browser-like events that you can listen to using the native addEventListener API — fully typed in TypeScript.
No dependencies.
Just plug it into any JS environment and events behave like native browser events.
🚀 Features
🔌 DOM-native custom events
Listen usingwindow.addEventListener("event", handler)— no imports needed.🎯 Full TypeScript support
Events extendWindowEventMapautomatically.🧩 Modular & extensible architecture
Add custom events in a clean, scalable pattern.🔄 Single initialization
Each event bootstrap runs only once.🌐 SSR-safe
No errors on server-side rendering environments.🌲 Tree-shaking friendly
Optional auto-init or manual init — your choice.
📦 Installation
npm install @amirafa/xevent🧠 Usage Example
📦 Option 0 — CDN (Browser Global Script)
Include the CDN script in your HTML:
<script src="https://cdn.jsdelivr.net/npm/@amirafa/xevent/dist/umd.global.js"></script>
<script>
// Option A — enable ALL events
XEvent.setupAllEvents();
// Option B — enable a single event
// XEvent.setupExternalLinkEvent();
</script>Option 1 — Manual Initialization (Tree-shaking Friendly)
import { setupAllEvents } from "@amirafa/xevent";
setupAllEvents(); // enables all eventsOption 2 — Auto Initialization (Simple Mode)
import "@amirafa/xevent/auto"; // initializes all events automaticallyOption 3 — Single Event Only
import { setupExternalLinkEvent } from "@amirafa/xevent";
setupExternalLinkEvent();🎧 Listening to Events
"external-link"
window.addEventListener("external-link", (e) => {
console.log("External link:", e.detail.href);
});"page-leave"
window.addEventListener("page-leave", (e) => {
console.log("Leaving page:", e.detail.reason);
});📜 Current Events
1. external-link
Fires when the user clicks a link pointing outside the current domain.
{
href: string;
target: string | null;
originalEvent: MouseEvent;
}2. page-leave
Fires when the user:
- closes the tab
- refreshes
- switches tabs
- navigates away
{
reason: "unload" | "visibility-hidden";
originalEvent: Event;
}➕ Adding New Events
Adding a new event is easy thanks to the shared initializer system.
1️⃣ Step 1 — Create a new event file
Create:
src/events/<yourEvent>.tsExample:
import { registerEvent } from "../core/utilsRegistry";
import { createEventInitializer } from "../core/createEventInitializer";
export const SCROLL_END_EVENT = "scroll-end" as const;
export interface ScrollEndEventDetail {
lastScrollY: number;
}
export const setupScrollEndEvent = createEventInitializer(
SCROLL_END_EVENT,
() => {
let timeout: any;
window.addEventListener("scroll", () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
window.dispatchEvent(
new CustomEvent<ScrollEndEventDetail>(SCROLL_END_EVENT, {
detail: { lastScrollY: window.scrollY }
})
);
}, 120);
});
}
);
registerEvent(SCROLL_END_EVENT, setupScrollEndEvent);2️⃣ Step 2 — Add Type Support
Edit:
src/types/global.d.tsAdd:
import { ScrollEndEventDetail } from "../events/scrollEnd";
declare global {
interface WindowEventMap {
"scroll-end": CustomEvent<ScrollEndEventDetail>;
}
}3️⃣ Step 3 — Export It
In:
src/index.tsAdd:
export * from "./events/scrollEnd";4️⃣ Step 4 — Document It (README)
### 3. `scroll-end`
Fires when scrolling stops.
Event detail:
```ts
{
lastScrollY: number;
}🤝 Contributing
- Add your event under
src/events/ - Register it with
registerEvent - Add typings in
global.d.ts - Export it from
index.ts - Update README
📜 License
MIT
