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.
-
🔌 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.
npm install @amirafa/xeventInclude 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>import { setupAllEvents } from "@amirafa/xevent";
setupAllEvents(); // enables all eventsimport "@amirafa/xevent/auto"; // initializes all events automaticallyimport { setupExternalLinkEvent } from "@amirafa/xevent";
setupExternalLinkEvent();window.addEventListener("external-link", (e) => {
console.log("External link:", e.detail.href);
});window.addEventListener("page-leave", (e) => {
console.log("Leaving page:", e.detail.reason);
});Fires when the user clicks a link pointing outside the current domain.
{
href: string;
target: string | null;
originalEvent: MouseEvent;
}Fires when the user:
- closes the tab
- refreshes
- switches tabs
- navigates away
{
reason: "unload" | "visibility-hidden";
originalEvent: Event;
}Adding a new event is easy thanks to the shared initializer system.
Create:
src/events/<yourEvent>.ts
Example:
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);Edit:
src/types/global.d.ts
Add:
import { ScrollEndEventDetail } from "../events/scrollEnd";
declare global {
interface WindowEventMap {
"scroll-end": CustomEvent<ScrollEndEventDetail>;
}
}In:
src/index.ts
Add:
export * from "./events/scrollEnd";### 3. `scroll-end`
Fires when scrolling stops.
Event detail:
```ts
{
lastScrollY: number;
}- Add your event under
src/events/ - Register it with
registerEvent - Add typings in
global.d.ts - Export it from
index.ts - Update README
MIT