Socket
Socket
Sign inDemoInstall

bind-event-listener

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bind-event-listener - npm Package Compare versions

Comparing version 3.0.0-beta.1 to 3.0.0

2

package.json
{
"name": "bind-event-listener",
"version": "3.0.0-beta.1",
"version": "3.0.0",
"private": false,

@@ -5,0 +5,0 @@ "description": "Making binding and unbinding DOM events easier",

@@ -7,4 +7,3 @@ # bind-event-listener

> A utility to make binding and (**especially**) unbinding DOM events easier.
> I seem to write this again with every new project, so I made it a library
A well typed utility to make creating and removing DOM event listeners safer and more ergonomic.

@@ -219,5 +218,6 @@ ```ts

Thanks to the great work by [@Ayub-Begimkulov](https://github.com/Ayub-Begimkulov) `bind-event-listener` has fantastic TypeScript types
Thanks to the great work by [@Ayub-Begimkulov](https://github.com/Ayub-Begimkulov) and [@Andarist](https://github.com/Andarist) `bind-event-listener` has fantastic TypeScript types and auto complete.
> ⚠️ TypeScript 4.1+ is required
> ⚠️ TypeScript 4.1+ is required for types
> ⚠️ TypeScript 5.0+ is required for event name autocompletion

@@ -260,2 +260,78 @@ ```ts

`bind` and `bindAll` accept type arguments (generics), but it is generally best to let these be inferred
```ts
// with explicit type arguments
bind<HTMLElement, 'click'>(button, {
type: 'click',
listener: function onClick() {},
});
// ✨ types will automatically be inferred for you ✨
bind(button, {
type: 'click',
listener: function onClick() {},
});
// with explicit type arguments
bindAll<HTMLElement, ['click', 'keydown']>(button, [
{
type: 'click',
listener: function onClick() {},
},
{
type: 'keydown',
listener: function onKeyDown() {},
},
]);
// ✨ types will automatically be inferred for you ✨
bindAll(button, [
{
type: 'click',
listener: function onClick() {},
},
{
type: 'keydown',
listener: function onKeyDown() {},
},
]);
```
> Typescript built in DOM types: [raw view](https://raw.githubusercontent.com/microsoft/TypeScript/master/lib/lib.dom.d.ts), [pretty view](https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts) (warning: pretty view seems to crash Github!)
### Type helpers
```ts
import { Binding, Listener, UnbindFn } from 'bind-event-listener';
```
`Listener`: the `function` or `object` that you provide to the `listener` property of a `Binding`
```ts
bind(button, {
type: 'click',
listener: function onClick() {}, // ← `Listener`
});
```
`Binding`: the definition of an event binding.
```ts
bind(
button,
// ↓ `Binding`
{
type: 'click',
listener: function onClick() {},
},
);
```
`UnbindFn`: a named type for `() => void` to make it clearer that the function will unbind the added event listener(s):
```ts
const unbind: UnbindFn = bind(button, { type: 'click', listener: function onClick() {} });
```
## Recipe: [`react`](https://reactjs.org/) effect

@@ -287,28 +363,4 @@

## Types
```ts
function bind(target: EventTarget, binding: Binding): UnbindFn;
function bindAll(
target: EventTarget,
bindings: Binding[],
// AddEventListenerOptions is a built in TypeScript type
sharedOptions?: boolean | AddEventListenerOptions,
): UnbindFn;
type Binding = {
type: string;
// EventListenerOrEventListenerObject is a built in TypeScript type
listener: EventListenerOrEventListenerObject;
options?: boolean | AddEventListenerOptions;
};
type UnbindFn = () => void;
```
> Typescript built in DOM types: [raw view](https://raw.githubusercontent.com/microsoft/TypeScript/master/lib/lib.dom.d.ts), [pretty view](https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts) (warning: pretty view seems to crash Github!)
## Cheers 👋
Brought to you by [@alexandereardon](https://twitter.com/alexandereardon)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc