@solid-primitives/event-listener
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -11,6 +11,3 @@ import { Accessor } from "solid-js"; | ||
interface Directives { | ||
createEventListener: (ref: HTMLElement, props: Accessor<EventListenerProps<{}>>) => [ | ||
add: (target: EventTarget) => void, | ||
remove: (target: EventTarget) => void | ||
]; | ||
createEventListener: (ref: HTMLElement, props: Accessor<EventListenerProps<{}>>) => [add: (target: EventTarget) => void, remove: (target: EventTarget) => void]; | ||
} | ||
@@ -17,0 +14,0 @@ } |
import { createEffect, onCleanup } from "solid-js"; | ||
export function createEventListener(target, nameOrProps, handler, options) { | ||
const targets = Array.isArray(target) ? target : [target]; | ||
const props = typeof nameOrProps === 'function' ? nameOrProps : () => [nameOrProps ?? '', handler ?? null, options]; | ||
const props = typeof nameOrProps === "function" | ||
? nameOrProps | ||
: () => [nameOrProps ?? "", handler ?? null, options]; | ||
const add = (target) => { | ||
@@ -18,3 +20,4 @@ targets.includes(target) || targets.push(target); | ||
if (previousProps !== currentProps) { | ||
previousProps && targets.forEach((target) => target.removeEventListener.apply(target, previousProps)); | ||
previousProps && | ||
targets.forEach(target => target.removeEventListener.apply(target, previousProps)); | ||
targets.forEach(add); | ||
@@ -29,3 +32,2 @@ } | ||
} | ||
; | ||
export default createEventListener; |
{ | ||
"name": "@solid-primitives/event-listener", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Primitive to manage creating event listeners.", | ||
@@ -15,2 +15,7 @@ "author": "David Di Biase <dave.dibiase@gmail.com>", | ||
], | ||
"exports": { | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.js" | ||
}, | ||
"sideEffects": "false", | ||
@@ -20,3 +25,3 @@ "scripts": { | ||
"clean": "rimraf dist/", | ||
"build": "tsc", | ||
"build": "tsc && tsc --target es5 --module commonjs --declaration false --outDir ./dist/cjs", | ||
"test": "jest" | ||
@@ -23,0 +28,0 @@ }, |
@@ -0,4 +1,14 @@ | ||
--- | ||
Name: event-listener | ||
Stage: 3 | ||
Package: "@solid-primitives/event-listener" | ||
Primitives: createEventListener | ||
Category: Browser APIs | ||
--- | ||
# @solid-primitives/event-listener | ||
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/) | ||
[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/event-listener)](https://bundlephobia.com/package/@solid-primitives/event-listener) | ||
[![size](https://img.shields.io/npm/v/@solid-primitives/event-listener)](https://www.npmjs.com/package/@solid-primitives/event-listener) | ||
@@ -9,2 +19,10 @@ A helpful event listener primitive that binds window and any element supplied. | ||
## Installation | ||
```bash | ||
npm install @solid-primitives/event-listener | ||
# or | ||
yarn add @solid-primitives/event-listener | ||
``` | ||
## How to use it | ||
@@ -16,5 +34,9 @@ | ||
); | ||
// or as a directive | ||
``` | ||
or as a directive | ||
```ts | ||
<MyButton use:createEventListener={() => ["click", () => console.log("Click")]}>Click!</MyButton>; | ||
// you can provide your own event map type: | ||
// you can provide your own event map type as well: | ||
createEventListener<{ myCustomEvent: Event }>(window, "myCustomEvent", () => console.log("yup!")); | ||
@@ -44,2 +66,6 @@ ``` | ||
1.2.4 | ||
Added CJS build. | ||
</details> |
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
9494
6
106
69