@solid-primitives/event-listener
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -1,2 +0,2 @@ | ||
import { Fn, MaybeAccessor, Many } from '@solid-primitives/utils'; | ||
import { Fn, MaybeAccessor, Many, Get, Clear } from '@solid-primitives/utils'; | ||
import { JSX, Accessor, Component } from 'solid-js'; | ||
@@ -10,7 +10,7 @@ import { Store } from 'solid-js/store'; | ||
declare type EventListenerDirectiveProps = [ | ||
name: string, | ||
type: string, | ||
handler: (e: any) => void, | ||
options?: AddEventListenerOptions | boolean | ||
options?: EventListenerOptions | ||
]; | ||
declare type EventListenerMapDirectiveProps = [handlerMap: Record<string, (e: any) => void>, options?: AddEventListenerOptions | boolean] | Record<string, (e: any) => void>; | ||
declare type EventListenerMapDirectiveProps = [handlerMap: Record<string, (e: any) => void>, options?: EventListenerOptions] | Record<string, (e: any) => void>; | ||
declare module "solid-js" { | ||
@@ -34,3 +34,3 @@ namespace JSX { | ||
* @param target - ref to HTMLElement, EventTarget or Array thereof | ||
* @param eventName - name of the handled event | ||
* @param type - name of the handled event | ||
* @param handler - event handler | ||
@@ -47,4 +47,4 @@ * @param options - addEventListener options | ||
*/ | ||
declare function createEventListener<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, EventName extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, eventName: MaybeAccessor<EventName>, handler: (event: EventMap[EventName]) => void, options?: MaybeAccessor<EventListenerOptions>): ClearListeners; | ||
declare function createEventListener<EventMap extends Record<string, Event>, EventName extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, eventName: MaybeAccessor<EventName>, handler: (event: EventMap[EventName]) => void, options?: MaybeAccessor<EventListenerOptions>): ClearListeners; | ||
declare function createEventListener<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, EventType extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, type: MaybeAccessor<Many<EventType>>, handler: (event: EventMap[EventType]) => void, options?: EventListenerOptions): ClearListeners; | ||
declare function createEventListener<EventMap extends Record<string, Event>, EventType extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, type: MaybeAccessor<Many<EventType>>, handler: (event: EventMap[EventType]) => void, options?: EventListenerOptions): ClearListeners; | ||
/** | ||
@@ -54,3 +54,3 @@ * Provides an reactive signal of last captured event. | ||
* @param target - ref to HTMLElement, EventTarget or Array thereof | ||
* @param eventName - name of the handled event | ||
* @param type - name of the handled event | ||
* @param options - addEventListener options | ||
@@ -70,8 +70,8 @@ * | ||
*/ | ||
declare function createEventSignal<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, EventName extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, eventName: MaybeAccessor<EventName>, options?: MaybeAccessor<boolean | AddEventListenerOptions>): EventListenerSignalReturns<EventMap[EventName]>; | ||
declare function createEventSignal<EventMap extends Record<string, Event>, EventName extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, eventName: MaybeAccessor<EventName>, options?: MaybeAccessor<boolean | AddEventListenerOptions>): EventListenerSignalReturns<EventMap[EventName]>; | ||
declare function createEventSignal<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, EventType extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, type: MaybeAccessor<Many<EventType>>, options?: EventListenerOptions): EventListenerSignalReturns<EventMap[EventType]>; | ||
declare function createEventSignal<EventMap extends Record<string, Event>, EventType extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, type: MaybeAccessor<Many<EventType>>, options?: EventListenerOptions): EventListenerSignalReturns<EventMap[EventType]>; | ||
/** | ||
* Directive Usage. Creates an event listener, that will be automatically disposed on cleanup. | ||
* | ||
* @param props [eventName, eventHandler, options] | ||
* @param props [eventType, handler, options] | ||
* | ||
@@ -86,3 +86,3 @@ * @example | ||
}; | ||
declare type EventListnenerStoreReturns<E> = [lastEvents: Store<Partial<E>>, clear: ClearListeners]; | ||
declare type EventListenerStoreReturns<E> = [lastEvents: Store<Partial<E>>, clear: ClearListeners]; | ||
/** | ||
@@ -107,4 +107,4 @@ * A helpful primitive that listens to a map of events. Handle them by individual callbacks. | ||
*/ | ||
declare function createEventListenerMap<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, handlersMap: Partial<Pick<EventHandlersMap<EventMap>, UsedEvents>>, options?: MaybeAccessor<EventListenerOptions>): ClearListeners; | ||
declare function createEventListenerMap<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, HandlersMap extends Partial<EventHandlersMap<EventMap>>>(target: MaybeAccessor<Many<Target>>, handlersMap: HandlersMap, options?: MaybeAccessor<EventListenerOptions>): ClearListeners; | ||
declare function createEventListenerMap<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, handlersMap: Partial<Pick<EventHandlersMap<EventMap>, UsedEvents>>, options?: EventListenerOptions): ClearListeners; | ||
declare function createEventListenerMap<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, HandlersMap extends Partial<EventHandlersMap<EventMap>>>(target: MaybeAccessor<Many<Target>>, handlersMap: HandlersMap, options?: EventListenerOptions): ClearListeners; | ||
/** | ||
@@ -114,3 +114,3 @@ * A helpful primitive that listens to target events and provides a reactive store with the latest captured events. | ||
* @param target accessor or variable of multiple or single event targets | ||
* @param options e.g. `{ passive: true }` *(can be omited)* | ||
* @param options e.g. `{ passive: true }` *(can be omitted)* | ||
* @param eventNames names of events you want to listen to, e.g. `"mousemove", "touchend", "click"` | ||
@@ -130,6 +130,6 @@ * | ||
*/ | ||
declare function createEventStore<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, UsedEvents extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, ...eventNames: UsedEvents[]): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, UsedEvents extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, options: MaybeAccessor<EventListenerOptions>, ...eventNames: UsedEvents[]): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, ...eventNames: UsedEvents[]): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, options: MaybeAccessor<EventListenerOptions>, ...eventNames: UsedEvents[]): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, UsedEvents extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, ...eventNames: UsedEvents[]): EventListenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<Target extends TargetWithEventMap, EventMap extends EventMapOf<Target>, UsedEvents extends keyof EventMap>(target: MaybeAccessor<Many<Target>>, options: EventListenerOptions, ...eventNames: UsedEvents[]): EventListenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, ...eventNames: UsedEvents[]): EventListenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
declare function createEventStore<EventMap extends Record<string, Event>, UsedEvents extends keyof EventMap = keyof EventMap>(target: MaybeAccessor<Many<EventTarget>>, options: EventListenerOptions, ...eventNames: UsedEvents[]): EventListenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
/** | ||
@@ -173,2 +173,26 @@ * Directive Usage. A helpful primitive that listens to provided events. Handle them by callbacks. | ||
export { ClearListeners, DocumentEventListener, E, EventHandlersMap, EventListenerDirectiveProps, EventListenerMapDirectiveProps, EventListenerOptions, EventListenerSignalReturns, EventListnenerStoreReturns, EventMapOf, TargetWithEventMap, WindowEventListener, createEventListener, createEventListenerMap, createEventSignal, createEventStore, eventListener, eventListenerMap }; | ||
declare type EventListenerBus<EventMap extends Record<string, any>> = Readonly<{ | ||
[K in `on${keyof EventMap extends string ? keyof EventMap : never}`]: (handler: Get<EventMap[K extends `on${infer T}` ? T : never]>) => Clear; | ||
} & { | ||
on: <T extends keyof EventMap>(type: MaybeAccessor<Many<T>>, handler: Get<EventMap[T]>) => Clear; | ||
}>; | ||
/** | ||
* Dynamically add and remove event listeners to an event target. The listeners will be automatically removed on cleanup. | ||
* @param target the event target, could be a `window`, `document`, `HTMLElement` or `MediaQueryList`. *Defaults to `window`* | ||
* @param options event listener options, such as `passive` or `capture` | ||
* @returns a Proxy object, which lets you create event listeners by calling appropriate property | ||
* @see https://github.com/davedbase/solid-primitives/tree/main/packages/event-listener#createEventListenerBus | ||
* @example | ||
* const bus = createEventListenerBus(document.body); | ||
* bus.onpointerenter(e => {...}); | ||
* // listeners return a function that removes them | ||
* const clear = bus.onpointermove(e => {...}); | ||
* clear(); | ||
* // or pass event type as an argument | ||
* const unsub = bus.on('click', e => {...}) | ||
*/ | ||
declare function createEventListenerBus(target?: undefined, options?: EventListenerOptions): EventListenerBus<WindowEventMap>; | ||
declare function createEventListenerBus<Target extends TargetWithEventMap>(target: MaybeAccessor<Many<Target>>, options?: EventListenerOptions): EventListenerBus<EventMapOf<Target>>; | ||
declare function createEventListenerBus<EventMap extends Record<string, Event>>(target?: MaybeAccessor<Many<EventTarget>>, options?: EventListenerOptions): EventListenerBus<EventMap>; | ||
export { ClearListeners, DocumentEventListener, E, EventHandlersMap, EventListenerBus, EventListenerDirectiveProps, EventListenerMapDirectiveProps, EventListenerOptions, EventListenerSignalReturns, EventListenerStoreReturns, EventMapOf, TargetWithEventMap, WindowEventListener, createEventListener, createEventListenerBus, createEventListenerMap, createEventSignal, createEventStore, eventListener, eventListenerMap }; |
@@ -7,8 +7,6 @@ // src/eventListener.ts | ||
noop, | ||
access, | ||
isServer | ||
} from "@solid-primitives/utils"; | ||
import { createRenderEffect, createSignal } from "solid-js"; | ||
import { createEffect, onCleanup } from "solid-js"; | ||
function createEventListener(targets, eventName, handler, options) { | ||
import { createEffect, onCleanup, createRenderEffect, createSignal } from "solid-js"; | ||
function createEventListener(targets, type, handler, options) { | ||
if (isServer) | ||
@@ -19,9 +17,9 @@ return noop; | ||
cleanup.execute(); | ||
const _eventName = access(eventName); | ||
const _options = access(options); | ||
forEach(targets, (el) => { | ||
if (!el) | ||
return; | ||
el.addEventListener(_eventName, handler, _options); | ||
cleanup.push(() => el.removeEventListener(_eventName, handler, _options)); | ||
forEach(type, (type2) => { | ||
forEach(targets, (el) => { | ||
if (!el) | ||
return; | ||
el.addEventListener(type2, handler, options); | ||
cleanup.push(() => el.removeEventListener(type2, handler, options)); | ||
}); | ||
}); | ||
@@ -36,5 +34,5 @@ }; | ||
} | ||
function createEventSignal(target, eventName, options) { | ||
function createEventSignal(target, type, options) { | ||
const [lastEvent, setLastEvent] = createSignal(); | ||
const clear = createEventListener(target, eventName, setLastEvent, options); | ||
const clear = createEventListener(target, type, setLastEvent, options); | ||
return [lastEvent, clear]; | ||
@@ -46,5 +44,5 @@ } | ||
toCleanup.execute(); | ||
const [eventName, handler, options] = props(); | ||
target.addEventListener(eventName, handler, options); | ||
toCleanup.push(() => target.removeEventListener(eventName, handler, options)); | ||
const [type, handler, options] = props(); | ||
target.addEventListener(type, handler, options); | ||
toCleanup.push(() => target.removeEventListener(type, handler, options)); | ||
}); | ||
@@ -55,7 +53,7 @@ onCleanup(toCleanup.execute); | ||
// src/eventListenerMap.ts | ||
import { entries, createCallbackStack as createCallbackStack2 } from "@solid-primitives/utils"; | ||
import { createCallbackStack as createCallbackStack2, forEachEntry } from "@solid-primitives/utils"; | ||
import { createEffect as createEffect2, createSignal as createSignal2, onCleanup as onCleanup2 } from "solid-js"; | ||
function createEventListenerMap(targets, handlersMap, options) { | ||
const { push, execute } = createCallbackStack2(); | ||
entries(handlersMap).forEach(([eventName, handler]) => { | ||
forEachEntry(handlersMap, (eventName, handler) => { | ||
push(createEventListener(targets, eventName, (e) => handler == null ? void 0 : handler(e), options)); | ||
@@ -77,6 +75,6 @@ }); | ||
const { push, execute } = createCallbackStack2(); | ||
names.forEach((eventName) => { | ||
names.forEach((type) => { | ||
const [accessor, setter] = createSignal2(); | ||
Object.defineProperty(store, eventName, { get: accessor, set: setter, enumerable: true }); | ||
push(createEventListener(targets, eventName, setter, options)); | ||
Object.defineProperty(store, type, { get: accessor, set: setter, enumerable: true }); | ||
push(createEventListener(targets, type, setter, options)); | ||
}); | ||
@@ -97,5 +95,5 @@ return [store, execute]; | ||
handlersMap = props; | ||
entries(handlersMap).forEach(([eventName, handler]) => { | ||
target.addEventListener(eventName, handler, options); | ||
toCleanup.push(() => target.removeEventListener(eventName, handler, options)); | ||
forEachEntry(handlersMap, (type, handler) => { | ||
target.addEventListener(type, handler, options); | ||
toCleanup.push(() => target.removeEventListener(type, handler, options)); | ||
}); | ||
@@ -118,4 +116,4 @@ }); | ||
if (isClient) | ||
forEachEventAttr(props, (eventName, attr) => { | ||
createEventListener(window, eventName, (e) => props[attr](e)); | ||
forEachEventAttr(props, (type, attr) => { | ||
createEventListener(window, type, (e) => props[attr](e)); | ||
}); | ||
@@ -131,2 +129,16 @@ return void 0; | ||
}; | ||
// src/bus.ts | ||
import { createProxy, isClient as isClient2 } from "@solid-primitives/utils"; | ||
import { runWithSubRoot } from "@solid-primitives/rootless"; | ||
import { getOwner } from "solid-js"; | ||
function createEventListenerBus(target = isClient2 ? window : [], options = {}) { | ||
const owner = getOwner(); | ||
const addListener = (type, handler) => runWithSubRoot(() => { | ||
createEventListener(target, type, handler, options); | ||
}, owner, getOwner()); | ||
return createProxy({ | ||
get: (key) => (...args) => args.length === 2 ? addListener(...args) : addListener(key.substring(2), args[0]) | ||
}); | ||
} | ||
export { | ||
@@ -136,2 +148,3 @@ DocumentEventListener, | ||
createEventListener, | ||
createEventListenerBus, | ||
createEventListenerMap, | ||
@@ -138,0 +151,0 @@ createEventSignal, |
{ | ||
"name": "@solid-primitives/event-listener", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Primitive to manage creating event listeners.", | ||
@@ -20,2 +20,3 @@ "author": "David Di Biase <dave.dibiase@gmail.com>", | ||
"createEventStore", | ||
"createEventListenerBus", | ||
"WindowEventListener", | ||
@@ -50,9 +51,9 @@ "DocumentEventListener" | ||
"prettier": "^2.5.0", | ||
"solid-register": "0.0.18", | ||
"solid-register": "0.1.5", | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.10.0", | ||
"unocss": "0.21.2", | ||
"unocss": "0.24.3", | ||
"uvu": "^0.5.2", | ||
"vite": "2.7.10", | ||
"vite-plugin-solid": "2.2.1", | ||
"vite": "2.8.1", | ||
"vite-plugin-solid": "2.2.5", | ||
"watchlist": "^0.3.1" | ||
@@ -64,4 +65,5 @@ }, | ||
"dependencies": { | ||
"@solid-primitives/utils": "^0.1.3" | ||
"@solid-primitives/utils": "^0.2.1", | ||
"@solid-primitives/rootless": "^0.0.101" | ||
} | ||
} |
183
README.md
@@ -14,2 +14,3 @@ # @solid-primitives/event-listener | ||
- [`createEventStore`](#createEventStore) - Similar to `createEventListenerMap`, but provides a reactive store with the latest captured events. | ||
- [`createEventListenerBus`](#createEventListenerBus) - Dynamically add and remove event listeners to an event target by calling appropriate property. | ||
- [`WindowEventListener`](#WindowEventListener) - Listen to the `window` DOM Events, using a component. | ||
@@ -45,8 +46,11 @@ - [`DocumentEventListener`](#DocumentEventListener) - The same as [`WindowEventListener`](#WindowEventListener), but listens to `document` events. | ||
// target element, event name and options can be reactive signals | ||
// target element and event name can be reactive signals | ||
const [ref, setRef] = createSignal<HTMLElement>(); | ||
const [name, setName] = createSignal("mousemove"); | ||
const [options, setOptions] = createSignal({ passive: true }); | ||
createEventListener(ref, name, e => {}, options); | ||
createEventListener(ref, name, e => {}, { passive: true }); | ||
``` | ||
#### Custom events | ||
```ts | ||
// you can provide your own event map type as well: | ||
@@ -62,2 +66,12 @@ // fill both type generics for the best type support | ||
#### Listening to multiple events | ||
###### Added in `@1.4.3` | ||
You can listen to multiple events with single `createEventListener` primitive. | ||
```ts | ||
createEventListener(el, ["mousemove", "mouseenter", "mouseleave"], e => {}); | ||
``` | ||
### Directive Usage | ||
@@ -75,28 +89,2 @@ | ||
### Types | ||
```ts | ||
function createEventListener< | ||
EventMap extends Record<string, Event>, | ||
EventName extends keyof EventMap | ||
>( | ||
target: MaybeAccessor<Many<EventTarget>>, | ||
eventName: MaybeAccessor<EventName>, | ||
handler: (event: EventMap[EventName]) => void, | ||
options?: MaybeAccessor<boolean | AddEventListenerOptions> | ||
): ClearListeners; | ||
// Directive | ||
function eventListener( | ||
target: Element, | ||
props: Accessor<EventListenerDirectiveProps> | ||
): EventListenerReturn; | ||
type EventListenerDirectiveProps = [ | ||
name: string, | ||
handler: (e: any) => void, | ||
options?: AddEventListenerOptions | boolean | ||
]; | ||
``` | ||
## `createEventSignal` | ||
@@ -122,20 +110,2 @@ | ||
### Types | ||
```ts | ||
function createEventSignal< | ||
EventMap extends Record<string, Event>, | ||
EventName extends keyof EventMap = keyof EventMap | ||
>( | ||
target: MaybeAccessor<Many<EventTarget>>, | ||
eventName: MaybeAccessor<EventName>, | ||
options?: MaybeAccessor<boolean | AddEventListenerOptions> | ||
): EventListenerSignalReturns<EventMap[EventName]>; | ||
type EventListenerSignalReturns<Event> = [ | ||
lastEvent: Accessor<Event | undefined>, | ||
clear: ClearListeners | ||
]; | ||
``` | ||
## `createEventListenerMap` | ||
@@ -159,5 +129,4 @@ | ||
// both target and options args can be reactive: | ||
// both target can be reactive: | ||
const [target, setTarget] = createSignal(document.getElementById("abc")); | ||
const [options, setOptions] = createSignal({ passive: true }); | ||
createEventListenerMap( | ||
@@ -169,3 +138,3 @@ target, | ||
}, | ||
options | ||
{ passive: true } | ||
); | ||
@@ -205,15 +174,2 @@ | ||
### Types | ||
```ts | ||
function createEventListenerMap< | ||
EventMap extends Record<string, Event>, | ||
UsedEvents extends keyof EventMap = keyof EventMap | ||
>( | ||
target: MaybeAccessor<Many<EventTarget>>, | ||
handlersMap: EventHandlersMap, | ||
options?: MaybeAccessor<boolean | AddEventListenerOptions> | ||
): ClearListeners; | ||
``` | ||
## `createEventStore` | ||
@@ -237,4 +193,3 @@ | ||
const [target, setTarget] = createSignal(document.getElementById("abc")); | ||
const [options, setOptions] = createSignal({ passive: true }); | ||
const [lastEvents] = createEventStore(target, options, "mousemove", "touchmove"); | ||
const [lastEvents] = createEventStore(target, "mousemove", "touchmove"); | ||
@@ -257,26 +212,80 @@ // createEventStore can be used to listen to custom events | ||
### types | ||
## `createEventListenerBus` | ||
###### Added in `@1.5.0` | ||
Dynamically add and remove event listeners to an event target by calling appropriate property. The listeners will be automatically removed on cleanup. | ||
### How to use it | ||
#### Import | ||
```ts | ||
function createEventStore< | ||
EventMap extends Record<string, Event>, | ||
UsedEvents extends keyof EventMap = keyof EventMap | ||
>( | ||
target: MaybeAccessor<Many<EventTarget>>, | ||
...eventNames: UsedEvents[] | ||
): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
import { createEventListenerBus } from "@solid-primitives/event-listener"; | ||
``` | ||
// with options: | ||
function createEventStore< | ||
EventMap extends Record<string, Event>, | ||
UsedEvents extends keyof EventMap = keyof EventMap | ||
>( | ||
target: MaybeAccessor<Many<EventTarget>>, | ||
options: MaybeAccessor<boolean | AddEventListenerOptions>, | ||
...eventNames: UsedEvents[] | ||
): EventListnenerStoreReturns<Pick<EventMap, UsedEvents>>; | ||
#### Basic usage | ||
type EventListnenerStoreReturns<E> = [lastEvents: Store<Partial<E>>, clear: ClearListeners]; | ||
`createEventListenerBus` takes two arguments: | ||
- `target` - the event target, could be a `window`, `document`, `HTMLElement` or `MediaQueryList`. _Defaults to `window`_ | ||
- `options` - event listener options, such as `passive` or `capture` | ||
```ts | ||
const bus = createEventListenerBus(document.body); | ||
bus.onpointerenter(e => {...}); | ||
// listeners return a function that removes them | ||
const clear = bus.onpointermove(e => {...}); | ||
clear(); | ||
``` | ||
#### Reactive target | ||
Target argument could be an **array**, and a **reactive signal**. | ||
```ts | ||
const [target, setTarget] = createSignal([document.body]); | ||
const bus = createEventListenerBus(target); | ||
setTarget(p => [...p, window]); // will change the targets of all active listeners. | ||
``` | ||
#### generic on() | ||
Using `bus.on(type, handler)` gives you more options in passing event type. | ||
```ts | ||
bus.on("click", e => {}); | ||
bus.on(["mousemove", "mousedown"], e => {}); | ||
const [types, setTypes] = createsignal(["focus", "blur"]); | ||
bus.on(types, e => {}); | ||
``` | ||
#### Custom Events | ||
The `createEventListenerBus` can be used to listen to Custom Events. | ||
```ts | ||
const bus = createEventListenerBus<{ | ||
foo: SomeEvent; | ||
bar: MyEvent; | ||
}>(); | ||
bus.onfoo(e => {}); | ||
bus.onbar(e => {}); | ||
``` | ||
#### Reactive Roots | ||
The EventListenerBus is designed in a way to let you add event listeners outside of reactive roots/ in different root then the one primitive was used in. | ||
```ts | ||
const bus = createRoot(dispose => { | ||
return createEventListenerBus(); | ||
}); | ||
// listeners can be added outside of the original root setup function. | ||
createRoot(dispose => { | ||
bus.onclick(e => {}); | ||
}); | ||
``` | ||
## `WindowEventListener` | ||
@@ -356,2 +365,10 @@ | ||
1.4.3 | ||
Allow listening to multiple event types with a single `createEventListener` | `createEventSignal`. Removed option to pass a reactive signal as options. | ||
1.5.0 | ||
Add `createEventListenerBus`. | ||
</details> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34656
506
364
3
1
+ Added@solid-primitives/rootless@0.0.101(transitive)
+ Added@solid-primitives/utils@0.2.2(transitive)
- Removed@solid-primitives/utils@0.1.3(transitive)