# emmy

> Events binder.

Latest version **7.1.1** (published 2024-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install emmy
pnpm add emmy
yarn add emmy
bun add emmy
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.1.1 |
| Published | 2024-10-18 |
| First published | 2014-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Dmitry Ivanov |
| Maintainers | dfcreative, dy |
| Keywords | nanoemitter, emitter, event, event emitter, jquery, browser, on, one, once, off, listeners, throttle, later, delay, delegate, defer, async, after, emit, emmy, mitt, emmitt |

## Links

- npm: https://www.npmjs.com/package/emmy
- Repository: https://github.com/dy/emmy
- Issues: https://github.com/dy/emmy/issues
- npm.io page: https://npm.io/package/emmy

## Dependencies (4)

- [icicle](https://npm.io/package/icicle.md) ^0.1.3
- [weak-map](https://npm.io/package/weak-map.md) ^1.0.5
- [is-browser](https://npm.io/package/is-browser.md) ^2.1.0
- [is-plain-obj](https://npm.io/package/is-plain-obj.md) ^1.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 7.1.1 (latest) — 2024-10-18
- 7.1.0 — 2024-10-18
- 7.0.0 — 2019-10-15
- 6.0.2 — 2019-01-27
- 6.0.1 — 2019-01-27
- 6.0.0 — 2019-01-27
- 5.4.0 — 2016-10-04
- 5.3.5 — 2015-10-21
- 5.3.4 — 2015-10-21
- 5.3.3 — 2015-10-21
- 5.3.2 — 2015-06-30
- 5.3.1 — 2015-06-29
- 5.3.0 — 2015-06-29
- 5.2.9 — 2015-06-29
- 5.2.8 — 2015-05-21
- … 55 more at https://npm.io/package/emmy/versions

## README

# Emmy [![test](https://github.com/dy/emmy/actions/workflows/test.js.yml/badge.svg)](https://github.com/dy/emmy/actions/workflows/test.js.yml)

Universal events binder.

* [x] Multiple events `on(el, 'click touchstart', cb)`
* [x] Event classes `on(el, 'click.x', cb), off(el, '.x')`
* [x] Uses native events, if available.
* [x] Delegate `on('.subel', 'click', cb, { target: container })`


## Usage

[![npm install emmy](https://nodei.co/npm/emmy.png?mini=true)](https://npmjs.org/package/emmy)

```js
import {on, off, emit} from 'emmy'

on(el, 'evt.foo', e => {})
emit(el, 'evt', {x: 1})
off(el, '.foo')
```

## API

### `off = on(target, event, handler, opts?)`

Bind `event` handler to `target` events.

* `event` can be a string or an array, optionally with class suffixes `click.tag1.tag2`
* `target` can be an element, list, or a string to delegate events.
* `opts` can provide `opts.target` for delegate target and listener props.

```js
// dragging
on(el, 'mousedown touchstart', () => {
	// ...init drag

	on(el, 'mousemove.drag touchmove.drag', () => {
		// ...handle drag
	})

	on(el, 'mouseup.drag touchend.drag', () => {
		off(el, '.drag')
		// ...end drag
	})
})

// delegate
let off = on('.selector', 'click', handler, { target: container })

// remove listener
off()
```

---

### `off(target, event?, callback?)`

Remove `event` handler from a `target`. If `callback` isn't passed - all registered listeners are removed. If `event` isn't passed - all registered callbacks for all known events are removed (useful on destroying the target).

```js
// remove handler the standard way
off(target, 'click', handler)

// remove handler for all registered events
off(target, handler)

// remove all events with provided suffix[es]
off(target, '.special')
```

---

### `emit(target, event, data?, options?)`

Emit an `event` on a `target`. `event` can be a string or an _Event_ instance. If `target` is an element then `data` is placed to `e.details`. `options` can define triggering params, eg. `{bubbles: true}`.



## License

MIT © Dmitry Ivanov.

---
_Source: https://npm.io/package/emmy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
