solid2-on
v0.0.3
Published
Chainable DOM event listeners for Solid 2 with typed custom events and lifecycle cleanup.
Maintainers
Readme
solid2-on
Chainable DOM event listeners for Solid 2. Use on as a JSX ref, configure native listener options fluently, handle custom events with typed payloads, and tie listener cleanup to Solid's owner lifecycle.
Install
npm install solid2-on solid-js@^2.0.0-rc.15Requires Solid 2 with onSettled() from solid-js. The package intentionally does not bundle Solid.
Usage
import { on } from "solid2-on";
function App() {
return (
<button
ref={on.click((event) => {
console.log("Clicked", event.clientX);
}).passive.stopOnCleanup}
>
Click me
</button>
);
}Both on.click(...) and on().click(...) create an independent listener chain:
<div ref={on.click(handleClick).once.stopOnCleanup} />
<div ref={on().keydown(handleKeydown).capture.stopOnCleanup} />Custom events
Use .event(name, handler) with any event name, including names containing : or -. The optional type argument describes the CustomEvent.detail payload:
import { on } from "solid2-on";
type Update = { id: number; name: string };
<div
ref={on.event<Update>("user:updated", (event) => {
console.log(event.detail.id, event.detail.name);
}).stopOnCleanup}
/>Dispatch with the standard DOM API:
el.dispatchEvent(new CustomEvent("user:updated", {
detail: { id: 123, name: "Ada" },
bubbles: true,
}));An HTMLElementEventMap augmentation provides inferred types for names you declare globally:
declare global {
interface HTMLElementEventMap {
"user:updated": CustomEvent<{ id: number; name: string }>;
}
}Register multiple events
Use .event({ name: handler, ... }) to register several events in one chain.
Both native and custom events work, and known native events have inferred TypeScript types:
<div ref={on.event({
click: (event) => console.log(event.clientX),
"custom-S": (event) => console.log(event.type),
"on-run": (event) => console.log(event.type),
}).stopOnCleanup} />For a typed custom event map, provide a generic object mapping each event name
to its detail payload (not to CustomEvent<...>):
type Details = {
"custom-S": { id: number };
"on-run": { status: "started" | "finished" };
};
<div ref={on.event<Details>({
"custom-S": (event) => console.log(event.detail.id),
"on-run": (event) => console.log(event.detail.status),
}).stopOnCleanup} />Event types also infer from HTMLElementEventMap augmentations, if defined.
The object form is equivalent to repeated .event(name, handler) calls and
can be mixed with .click(...) or additional .event(...) calls. Every call
appends registrations, including duplicate callbacks for the same event
name. All registrations share the same chain modifiers, abort signal, and
cleanup. With .once, each event listener gets its own one-time invocation.
const controller = new AbortController();
const handle = (event: Event) => console.log(event.type);
<div ref={on.event({
"custom-S": handle,
"on-run": handle,
}).once.signal(controller.signal).stopOnCleanup} />event({}) is a no-op. The map's own enumerable string keys are used; inherited
properties are ignored. If any value is not a function, .event(map) throws
without adding listeners from that map.
You can also use property syntax for event names that don't conflict with chain methods:
<div ref={on["user:updated"]((event: CustomEvent<Update>) => {
console.log(event.detail.name);
}).stopOnCleanup} />Use .event(...) for names that might conflict with methods such as signal, once, or stop.
Listener modifiers
.once, .passive, .capture, and .stopOnCleanup work as bare modifiers or callable modifiers. The last setting wins:
<div ref={on.click(handleClick)
.once
.once(false)
.passive
.capture(false)
.stopOnCleanup
} />All native listener options apply to the chain's registered listeners.
AbortSignal
const controller = new AbortController();
<div ref={on.click(handleClick)
.signal(controller.signal)
.stopOnCleanup
} />
// Later:
controller.abort();An aborted signal prevents subsequent event delivery. A signal aborted before attachment prevents listeners from being registered.
Settled and cleanup hooks
Listeners are attached after Solid's onSettled() phase, once the ref element is available. The .settled(...) callback receives the element, a stop function, and the effective options, and can return a cleanup function:
<div ref={on.click(handleClick)
.settled((element, stop, options) => {
console.log("listener attached", element, options);
return () => console.log("owner disposed");
})
.onCleanup((element, stop) => {
console.log("owner cleanup", element);
})
.stopOnCleanup
} />.onCleanup(...) and a function returned by .settled(...) run when the owning Solid scope is disposed. .stopOnCleanup additionally removes the event listeners; it is opt-in, so add it when you want owner-disposal to detach listeners.
For explicit removal, keep a reference to the chain and call chain.stop(). stop() is idempotent and prevents later listener attachment:
const chain = on.click(handleClick).stopOnCleanup;
<div ref={chain} />;
chain.stop();You can register multiple listeners in one chain, including repeated registrations of the same callback:
<div ref={on
.click(handleFirst)
.click(handleSecond)
.event("custom:ready", handleReady)
.stopOnCleanup
} />Note: Calling on() or accessing on.click should occur within a Solid owner (typically during component setup) so the lifecycle callback can be registered. These are native addEventListener handlers, not Solid's delegated onClick attributes.
Development
npm install
npm run typecheck
npm test
npm run pack:check
npm run bench:event-map # optional Node construction benchmark
npm packnpm test builds the package and runs its TypeScript and native Node tests.
npm run bench:event-map optionally measures just the construction cost of
three repeated .event(name, handler) calls versus .event({ ... }) in Node;
it does not measure DOM attachment, dispatch, Solid rendering, or cleanup.
Browser projects should also test mounting and unmounting with their installed Solid 2 runtime.
License
MIT
