
Product
Socket Threat Intel Now Shows Which Malware Campaigns Were Blocked or Detected
Socket Threat Intel now shows which malware campaigns were blocked or detected in your organization, with links to scans and Firewall events.
@solid-primitives/event-listener
Advanced tools
A set of primitives that help with listening to DOM and Custom Events.
makeEventListener — Non-reactive primitive for adding event listeners that gets removed onCleanup.makeEventListenerStack — Creates a stack of event listeners, that will be automatically disposed on cleanup.createEventListener — Reactive version of makeEventListener, that takes signal arguments to apply new listeners once changed.createEventSignal — Like createEventListener, but captured events are stored in a returned signal.createEventListenerMap — A helpful primitive that listens to a map of events. Handle them by individual callbacks.WindowEventListener — Listen to the window DOM Events, using a component.DocumentEventListener — Listen to the document DOM Events, using a component.preventDefault — Wraps event handler with e.preventDefault() call.stopPropagation — Wraps event handler with e.stopPropagation() call.stopImmediatePropagation — Wraps event handler with e.stopImmediatePropagation() call.npm install @solid-primitives/event-listener
# or
yarn add @solid-primitives/event-listener
# or
pnpm add @solid-primitives/event-listener
makeEventListener@2.0.0Can be used to listen to DOM or Custom Events on window, document, or any EventTarget.
Event listener is automatically removed on root cleanup. The clear() function is also returned for calling it early.
import { makeEventListener } from "@solid-primitives/event-listener";
const clear = makeEventListener(
document.getElementById("myButton"),
"mousemove",
e => console.log("x:", e.pageX, "y:", e.pageY),
{ passive: true }
);
// to clear all of the event listeners
clear();
// when listening to element refs, call it inside onMount
let ref!: HTMLDivElement
onMount(() => {
makeEventListener(ref, "click", e => {...}, { passive: true });
});
<div ref={ref} />;
// you can provide your own event map type as well:
// fill both type generics for the best type support
makeEventListener<{ myCustomEvent: MyEvent; other: Event }, "myCustomEvent">(
window,
"myCustomEvent",
() => console.log("yup!"),
);
// just don't use interfaces as EventMaps! (write them using `type` keyword)
makeEventListenerStack@2.0.0Creates a stack of event listeners, that will be automatically disposed on cleanup.
import { makeEventListenerStack } from "@solid-primitives/event-listener";
const [listen, clear] = makeEventListenerStack(target, { passive: true });
listen("mousemove", handleMouse);
listen("dragover", handleMouse);
// remove listener (will also happen on cleanup)
clear();
createEventListenerReactive version of makeEventListener, that can take signal target and type arguments to apply new listeners once changed.
import { createEventListener } from "@solid-primitives/event-listener";
// target element and event name can be reactive signals
const [ref, setRef] = createSignal<HTMLElement>();
const [type, setType] = createSignal("mousemove");
createEventListener(ref, type, e => {...});
// when using ref as a target, pass it in a function – function will be executed after mount
// or wrap the whole primitive in onMount
let ref;
createEventListener(() => ref, "mousemove", e => {});
<div ref={ref} />;
// it can also be used with any HTML Element if you can get a reference to it
createEventListener(
document.getElementById("myButton"),
"mousemove",
e => console.log("x:", e.pageX, "y:", e.pageY),
{ passive: true }
);
// you can provide your own event map type as well:
// fill both type generics for the best type support
createEventListener<{ myCustomEvent: MyEvent; other: Event }, "myCustomEvent">(
window,
"myCustomEvent",
() => console.log("yup!"),
);
// just don't use interfaces as EventMaps! (write them using `type` keyword)
Since version @2.0.0 createEventListener and other reactive primitives aren't returning a clear() function, because of it's flawed behavior described in this issue.
Although there are still ways to remove attached event listeners:
target or type arguments to an empty array.const [type, setType] = createSignal<"click" | []>("click");
createEventListener(window, type, e => {...});
// remove listener:
setType([]);
createEventListener primitive in Solid's createRoot or createBranch | createDisposable from "@solid-primitives/rootless".import { createDisposable } from "@solid-primitives/rootless";
const clear = createDisposable(() => createEventListener(element, "click", e => {...}));
// remove listener:
clear();
@1.4.3You can listen to multiple events with single createEventListener primitive.
createEventListener(el, ["mousemove", "mouseenter", "mouseleave"], e => {});
eventListener returns a ref callback. Pass the props as a tuple, or as an accessor to swap handlers on the fly.
import { eventListener } from "@solid-primitives/event-listener";
<button ref={eventListener(["click", () => console.log("Click")])}>Click!</button>;
// reactive props — the listener is re-attached when they change
<button ref={eventListener(() => ["click", handler()])}>Click!</button>;
createEventSignalLike createEventListener, but events are handled with the returned signal, instead of with a callback.
import { createEventSignal } from "@solid-primitives/event-listener";
// all arguments can be reactive signals
const lastEvent = createEventSignal(el, "mousemove", { passive: true });
createEffect(() => {
console.log(lastEvent()?.x, lastEvent()?.y);
});
createEventListenerMapA helpful primitive that listens to a map of events. Handle them by individual callbacks.
import { createEventListenerMap } from "@solid-primitives/event-listener";
createEventListenerMap(element, {
mousemove: mouseHandler,
mouseenter: e => {},
touchend: touchHandler,
});
// both target can be reactive:
const [target, setTarget] = createSignal(document.getElementById("abc"));
createEventListenerMap(
target,
{
mousemove: e => {},
touchstart: e => {},
},
{ passive: true },
);
// createEventListenerMap can be used to listen to custom events
// fill both type generics for the best type support
createEventListenerMap<{
myEvent: MyEvent;
custom: Event;
other: Event;
}>(target, {
myEvent: e => {},
custom: e => {},
});
WindowEventListenerListen to the window DOM Events, using a component.
You can use it with any Solid's Control-Flow components, e.g. <Show/> or <Switch/>.
The event handler prop is reactive, so you can use it with signals.
import { WindowEventListener } from "@solid-primitives/event-listener";
<WindowEventListener onMouseMove={e => console.log(e.x, e.y)} />;
DocumentEventListenerThe same as WindowEventListener, but listens to document events.
import { DocumentEventListener } from "@solid-primitives/event-listener";
<DocumentEventListener onMouseMove={e => console.log(e.x, e.y)} />;
preventDefaultWraps event handler with e.preventDefault() call.
import { preventDefault, makeEventListener } from "@solid-primitives/event-listener";
const handleClick = e => {
concole.log("Click!", e);
};
makeEventListener(window, "click", preventDefault(handleClick), true);
// or in jsx:
<div onClick={preventDefault(handleClick)} />;
stopPropagationWraps event handler with e.stopPropagation() call.
import { stopPropagation, makeEventListener } from "@solid-primitives/event-listener";
const handleClick = e => {
concole.log("Click!", e);
};
makeEventListener(window, "click", stopPropagation(handleClick), true);
// or in jsx:
<div onClick={stopPropagation(handleClick)} />;
stopImmediatePropagationWraps event handler with e.stopImmediatePropagation() call.
import { stopImmediatePropagation, makeEventListener } from "@solid-primitives/event-listener";
const handleClick = e => {
concole.log("Click!", e);
};
makeEventListener(window, "click", stopImmediatePropagation(handleClick), true);
// or in jsx:
<div onClick={stopImmediatePropagation(handleClick)} />;
See CHANGELOG.md
react-use is a collection of essential React Hooks, including hooks for managing event listeners. It provides similar functionality for React applications, allowing developers to easily manage event listeners in a declarative way. Unlike @solid-primitives/event-listener, which is specific to SolidJS, react-use is tailored for React.
use-gesture is a library for handling gestures in React applications. It provides hooks for managing complex gesture interactions, such as dragging and pinching. While it offers more advanced gesture handling compared to @solid-primitives/event-listener, it is specific to React and focuses on gesture events rather than general event listeners.
add-event-listener is a simple utility for adding and removing event listeners in JavaScript. It is a more general-purpose library compared to @solid-primitives/event-listener, which is specifically designed for use with SolidJS and provides reactive management of event listeners.
FAQs
SolidJS Primitives to manage creating event listeners.
The npm package @solid-primitives/event-listener receives a total of 0 weekly downloads. As such, @solid-primitives/event-listener popularity was classified as not popular.
We found that @solid-primitives/event-listener demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.

Product
Socket Threat Intel now shows which malware campaigns were blocked or detected in your organization, with links to scans and Firewall events.

Security News
Fixes for eMudhra and Brotli are out. Getting them everywhere they’re needed means tracking down bundled libraries, runtimes, and certificate stores.

Threat Research
/Security News
Socket uncovered secret-stealing workflows that ran in LiteLLM’s GitHub repository 11 days before the supply chain attack on PyPI and Docker.