@solid-primitives/event-listener
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -151,2 +151,51 @@ import { MaybeAccessor, Many, Directive } from '@solid-primitives/utils'; | ||
export { DocumentEventListener, DocumentEventProps, E, EventHandlersMap, EventListenerDirectiveProps, EventListenerOptions, EventListenerStackOn, EventMapOf, TargetWithEventMap, WindowEventListener, WindowEventProps, createEventListener, createEventListenerMap, createEventSignal, eventListener, makeEventListener, makeEventListenerStack }; | ||
/** | ||
* Calls `e.preventDefault()` on the `Event` and calls the {@link callback} | ||
* | ||
* @param callback Event handler | ||
* @returns Event handler matching {@link callback}'s type | ||
* @example | ||
* ```tsx | ||
* const handleClick = (e) => { | ||
* concole.log("Click!", e) | ||
* } | ||
* makeEventListener(window, "click", preventDefault(handleClick), true); | ||
* // or in jsx: | ||
* <div onClick={preventDefault(handleClick)} /> | ||
* ``` | ||
*/ | ||
declare const preventDefault: <E extends Event>(callback: (event: E) => void) => (event: E) => void; | ||
/** | ||
* Calls `e.stopPropagation()` on the `Event` and calls the {@link callback} | ||
* | ||
* @param callback Event handler | ||
* @returns Event handler matching {@link callback}'s type | ||
* @example | ||
* ```tsx | ||
* const handleClick = (e) => { | ||
* concole.log("Click!", e) | ||
* } | ||
* makeEventListener(window, "click", stopPropagation(handleClick), true); | ||
* // or in jsx: | ||
* <div onClick={stopPropagation(handleClick)} /> | ||
* ``` | ||
*/ | ||
declare const stopPropagation: <E extends Event>(callback: (event: E) => void) => (event: E) => void; | ||
/** | ||
* Calls `e.stopImmediatePropagation()` on the `Event` and calls the {@link callback} | ||
* | ||
* @param callback Event handler | ||
* @returns Event handler matching {@link callback}'s type | ||
* @example | ||
* ```tsx | ||
* const handleClick = (e) => { | ||
* concole.log("Click!", e) | ||
* } | ||
* makeEventListener(window, "click", stopImmediatePropagation(handleClick), true); | ||
* // or in jsx: | ||
* <div onClick={stopImmediatePropagation(handleClick)} /> | ||
* ``` | ||
*/ | ||
declare const stopImmediatePropagation: <E extends Event>(callback: (event: E) => void) => (event: E) => void; | ||
export { DocumentEventListener, DocumentEventProps, E, EventHandlersMap, EventListenerDirectiveProps, EventListenerOptions, EventListenerStackOn, EventMapOf, TargetWithEventMap, WindowEventListener, WindowEventProps, createEventListener, createEventListenerMap, createEventSignal, eventListener, makeEventListener, makeEventListenerStack, preventDefault, stopImmediatePropagation, stopPropagation }; |
@@ -76,2 +76,16 @@ // src/eventListener.ts | ||
} | ||
// src/callbackWrappers.ts | ||
var preventDefault = (callback) => (e) => { | ||
e.preventDefault(); | ||
callback(e); | ||
}; | ||
var stopPropagation = (callback) => (e) => { | ||
e.stopPropagation(); | ||
callback(e); | ||
}; | ||
var stopImmediatePropagation = (callback) => (e) => { | ||
e.stopImmediatePropagation(); | ||
callback(e); | ||
}; | ||
export { | ||
@@ -85,3 +99,6 @@ DocumentEventListener, | ||
makeEventListener, | ||
makeEventListenerStack | ||
makeEventListenerStack, | ||
preventDefault, | ||
stopImmediatePropagation, | ||
stopPropagation | ||
}; |
{ | ||
"name": "@solid-primitives/event-listener", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "SolidJS Primitives to manage creating event listeners.", | ||
@@ -67,2 +67,2 @@ "author": "David Di Biase <dave.dibiase@gmail.com>", | ||
} | ||
} | ||
} |
@@ -16,16 +16,22 @@ <p> | ||
- [`makeEventListener`](#makeEventListener) - Non-reactive primitive for adding event listeners that gets removed onCleanup. | ||
- [`makeEventListenerStack`](#makeEventListenerStack) - Creates a stack of event listeners, that will be automatically disposed on cleanup. | ||
- [`makeEventListener`](#makeEventListener) — Non-reactive primitive for adding event listeners that gets removed onCleanup. | ||
- [`makeEventListenerStack`](#makeEventListenerStack) — Creates a stack of event listeners, that will be automatically disposed on cleanup. | ||
##### Reactive primitives: | ||
- [`createEventListener`](#createEventListener) - Reactive version of [`makeEventListener`](#makeEventListener), that takes signal arguments to apply new listeners once changed. | ||
- [`createEventSignal`](#createEventListener) - Like [`createEventListener`](#createEventListener), but captured events are stored in a returned signal. | ||
- [`createEventListenerMap`](#createEventListenerMap) - A helpful primitive that listens to a map of events. Handle them by individual callbacks. | ||
- [`createEventListener`](#createEventListener) — Reactive version of [`makeEventListener`](#makeEventListener), that takes signal arguments to apply new listeners once changed. | ||
- [`createEventSignal`](#createEventListener) — Like [`createEventListener`](#createEventListener), but captured events are stored in a returned signal. | ||
- [`createEventListenerMap`](#createEventListenerMap) — A helpful primitive that listens to a map of events. Handle them by individual callbacks. | ||
##### Component global listeners: | ||
- [`WindowEventListener`](#WindowEventListener) - Listen to the `window` DOM Events, using a component. | ||
- [`DocumentEventListener`](#DocumentEventListener) - Listen to the `document` DOM Events, using a component. | ||
- [`WindowEventListener`](#WindowEventListener) — Listen to the `window` DOM Events, using a component. | ||
- [`DocumentEventListener`](#DocumentEventListener) — Listen to the `document` DOM Events, using a component. | ||
##### Callback Wrappers | ||
- [`preventDefault`](#preventDefault) — Wraps event handler with `e.preventDefault()` call. | ||
- [`stopPropagation`](#stopPropagation) — Wraps event handler with `e.stopPropagation()` call. | ||
- [`stopImmediatePropagation`](#stopImmediatePropagation) — Wraps event handler with `e.stopImmediatePropagation()` call. | ||
## Installation | ||
@@ -279,2 +285,52 @@ | ||
## Callback Wrappers | ||
### `preventDefault` | ||
Wraps event handler with `e.preventDefault()` call. | ||
```tsx | ||
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)} />; | ||
``` | ||
### `stopPropagation` | ||
Wraps event handler with `e.stopPropagation()` call. | ||
```tsx | ||
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)} />; | ||
``` | ||
### `stopImmediatePropagation` | ||
Wraps event handler with `e.stopImmediatePropagation()` call. | ||
```tsx | ||
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)} />; | ||
``` | ||
## Demo | ||
@@ -348,2 +404,6 @@ | ||
2.2.0 | ||
Add `preventDefault`, `stopPropagation` and `stopImmediatePropagation` callback wrappers. | ||
</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
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
32886
428
407