{"name":"@solid-primitives/event-listener","version":"1.3.2","description":"Primitive to manage creating event listeners.","author":{"name":"David Di Biase","email":"dave.dibiase@gmail.com"},"license":"MIT","homepage":"https://github.com/davedbase/solid-primitives/tree/main/packages/event-listener","repository":{"type":"git","url":"git+https://github.com/davedbase/solid-primitives.git"},"primitive":{"name":"event-listener","stage":3,"list":["createEventListener","GlobalEventListener"],"category":"Browser APIs"},"private":false,"sideEffects":false,"type":"module","main":"dist/index.cjs","module":"dist/index.js","types":"dist/index.d.ts","scripts":{"start":"vite -r ./dev/ -c ./dev/vite.config.ts","build":"tsup","test":"uvu -r solid-register","watch-test":"watchlist src test -- npm test"},"keywords":["event","listener","solid","primitives"],"devDependencies":{"jsdom":"^19.0.0","prettier":"^2.5.0","solid-register":"0.0.18","tslib":"^2.3.1","tsup":"^5.10.0","unocss":"^0.12.4","uvu":"^0.5.2","vite":"2.6.14","vite-plugin-solid":"2.1.2","watchlist":"^0.3.1"},"peerDependencies":{"solid-js":"^1.2.5"},"dependencies":{"@solid-primitives/utils":"^0.0.255"},"readmeFilename":"README.md","readme":"# @solid-primitives/event-listener\n\n[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg?style=for-the-badge)](https://lerna.js.org/)\n[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/event-listener?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/event-listener)\n[![size](https://img.shields.io/npm/v/@solid-primitives/event-listener?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/event-listener)\n\nA helpful event listener primitive that binds window and any element supplied.\n\n- [`createEventListener`](#createEventListener) - Very basic and straightforward primitive that handles multiple elements according to a single event binding.\n- [`createEventSignal`](#createEventListener) - Like `createEventListener`, but events are handled with the returned signal, instead of with a callback.\n- [`createEventListenerMap`](#createEventListenerMap) - A helpful primitive that listens to a map of events. Handle them by individual callbacks.\n- [`createEventStore`](#createEventStore) - Similar to `createEventListenerMap`, but provides a reactive store with the latest captured events.\n- [`WindowEventListener`](#WindowEventListener) - Listen to the `window` DOM Events, using a component.\n- [`DocumentEventListener`](#DocumentEventListener) - The same as [`WindowEventListener`](#WindowEventListener), but listens to `document` events.\n\n## Installation\n\n```bash\nnpm install @solid-primitives/event-listener\n# or\nyarn add @solid-primitives/event-listener\n```\n\n## `createEventListener`\n\nCan be used to listen to DOM or Custom Events on window, document, list of HTML elements or any EventTarget. The target prop can be reactive.\n\n### How to use it\n\n```ts\nimport { createEventListener } from \"@solid-primitives/event-listener\";\n\ncreateEventListener(\n  document.getElementById(\"mybutton\"),\n  \"mousemove\",\n  e => console.log(\"x:\", e.pageX, \"y:\", e.pageY),\n  { passive: true }\n);\n\n// target element, event name and options can be reactive signals\nconst [ref, setRef] = createSignal<HTMLElement>();\nconst [name, setName] = createSignal(\"mousemove\");\nconst [options, setOptions] = createSignal({ passive: true });\ncreateEventListener(ref, name, e => {}, options);\n\n// you can provide your own event map type as well:\ncreateEventListener<{ myCustomEvent: Event }>(window, \"myCustomEvent\", () => console.log(\"yup!\"));\n// just don't use interfaces as EventMaps!\n```\n\n### Directive Usage\n\nprops passed to the directive are also reactive, so you can change handlers on the fly.\n\n```tsx\nimport { eventListener } from \"@solid-primitives/event-listener\";\n// avoids tree-shaking the directive:\neventListener;\n\n<button use:eventListener={[\"click\", () => console.log(\"Click\")]}>Click!</button>;\n```\n\n### Types\n\n```ts\nfunction createEventListener<\n  EventMap extends Record<string, Event>,\n  EventName extends keyof EventMap\n>(\n  target: MaybeAccessor<Many<EventTarget>>,\n  eventName: MaybeAccessor<EventName>,\n  handler: (event: EventMap[EventName]) => void,\n  options?: MaybeAccessor<boolean | AddEventListenerOptions>\n): void;\n\n// Directive\nfunction eventListener(\n  target: Element,\n  props: Accessor<EventListenerDirectiveProps>\n): EventListenerReturn;\n\ntype EventListenerDirectiveProps = [\n  name: string,\n  handler: (e: any) => void,\n  options?: AddEventListenerOptions | boolean\n];\n```\n\n## `createEventSignal`\n\nLike [`createEventListener`](#createEventListener), but events are handled with the returned signal, instead of with a callback.\n\n### How to use it\n\n```ts\nimport { createEventSignal } from \"@solid-primitives/event-listener\";\n\n// all arguments can be reactive signals\nconst lastEvent = createEventSignal(el, \"mousemove\", { passive: true });\n\ncreateEffect(() => {\n  console.log(lastEvent()?.x, lastEvent()?.y);\n});\n```\n\n## `createEventListenerMap`\n\nA helpful primitive that listens to a map of events. Handle them by individual callbacks.\n\n### How to use it\n\n```ts\nimport { createEventListenerMap } from \"@solid-primitives/event-listener\";\n\ncreateEventListenerMap(element, {\n  mousemove: mouseHandler,\n  mouseenter: e => {},\n  touchend: touchHandler\n});\n\n// both target and options args can be reactive:\nconst [target, setTarget] = createSignal(document.getElementById(\"abc\"));\nconst [options, setOptions] = createSignal({ passive: true });\ncreateEventListenerMap(\n  target,\n  {\n    mousemove: e => {},\n    touchstart: e => {}\n  },\n  options\n);\n\n// createEventListenerMap can be used to listen to custom events\ncreateEventListenerMap<\n  {\n    myEvent: MyEvent;\n    custom: Event;\n    other: Event;\n  },\n  \"myEvent\" | \"custom\"\n>(target, {\n  myEvent: e => {},\n  custom: e => {}\n});\n```\n\n### Directive usage\n\n```tsx\nimport { eventListenerMap } from \"@solid-primitives/event-listener\";\n// prevent tree-shaking:\neventListenerMap;\n\n<div\n  use:eventListenerMap={{\n    mousemove: e => {},\n    click: clickHandler,\n    touchstart: () => {},\n    myCustomEvent: e => {}\n  }}\n></div>;\n```\n\n### Types\n\n```ts\nfunction createEventListenerMap<\n  EventMap extends Record<string, Event>,\n  UsedEvents extends keyof EventMap = keyof EventMap\n>(\n  target: MaybeAccessor<Many<EventTarget>>,\n  handlersMap: EventHandlersMap,\n  options?: MaybeAccessor<boolean | AddEventListenerOptions>\n): void;\n```\n\n## `createEventStore`\n\nSimilar to [`createEventListenerMap`](#createEventListenerMap), but provides a reactive store with the latest captured events.\n\n### How to use it\n\n```ts\nconst lastEvents = createEventStore(el, \"mousemove\", \"touchend\", \"click\");\n\ncreateEffect(() => {\n  console.log(lastEvents?.mousemove.x);\n});\n\n// both target and options args can be reactive:\nconst [target, setTarget] = createSignal(document.getElementById(\"abc\"));\nconst [options, setOptions] = createSignal({ passive: true });\nconst lastEvents = createEventStore(target, options, \"mousemove\", \"touchmove\");\n\n// createEventStore can be used to listen to custom events\nconst lastEvents = createEventStore<\n  {\n    myEvent: MyEvent;\n    custom: Event;\n  },\n  \"myEvent\" | \"custom\"\n>(target, \"myEvent\", \"custom\");\n\n// DON'T DO THIS:\nconst { mousemove } = createEventStore(target, \"mousemove\", ...);\n// the store cannot be destructured\n```\n\n### types\n\n```ts\nfunction createEventStore<\n  EventMap extends Record<string, Event>,\n  UsedEvents extends keyof EventMap = keyof EventMap\n>(\n  target: MaybeAccessor<Many<EventTarget>>,\n  ...eventNames: UsedEvents[]\n): Store<Partial<Pick<EventMap, UsedEvents>>>;\n\n// with options:\nfunction createEventStore<\n  EventMap extends Record<string, Event>,\n  UsedEvents extends keyof EventMap = keyof EventMap\n>(\n  target: MaybeAccessor<Many<EventTarget>>,\n  options: MaybeAccessor<boolean | AddEventListenerOptions>,\n  ...eventNames: UsedEvents[]\n): Store<Partial<Pick<EventMap, UsedEvents>>>;\n```\n\n## `WindowEventListener`\n\nListen to the `window` DOM Events, using a component.\n\nYou can use it with any Solid's Control-Flow components, e.g. `<Show/>` or `<Switch/>`.\n\nThe event handler prop is reactive, so you can use it with signals.\n\n### How to use it\n\n```tsx\nimport { WindowEventListener } from \"@solid-primitives/event-listener\";\n\n<WindowEventListener onMouseMove={e => console.log(e.x, e.y)} />;\n```\n\n## `DocumentEventListener`\n\nThe same as [`WindowEventListener`](#WindowEventListener), but listens to `document` events.\n\n### How to use it\n\n```tsx\nimport { DocumentEventListener } from \"@solid-primitives/event-listener\";\n\n<DocumentEventListener onMouseMove={e => console.log(e.x, e.y)} />;\n```\n\n## Demo\n\nYou may view a working example here: https://codesandbox.io/s/solid-primitives-event-listener-elti5\n\n## Changelog\n\n<details>\n<summary><b>Expand Changelog</b></summary>\n\n0.0.100\n\nFirst ported commit from react-use-event-listener.\n\n1.1.4\n\nReleased a version with type mostly cleaned up.\n\n1.2.3\n\nSwitched to a more idiomatic pattern: Warning: incompatible with the previous version!\n\n1.2.5\n\nAdded CJS build.\n\n1.2.6\n\nMigrated to new build process.\n\n1.3.0\n\n**(minor breaking changes to type generics and returned functions)**\nPrimitive rewritten to provide better types and more reliable usage. Added more primitives.\n\n</details>\n","licenseText":"MIT License\n\nCopyright (c) 2021 Solid Primitives Working Group\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","_id":"@solid-primitives/event-listener@1.3.2","dist":{"shasum":"eee581ddfea27a410fc6f8f0028f338e8a8dcdb5","integrity":"sha512-KIK8AXkTgoZydXjxODzAxUPPotExU2w8R5xJD095FpLQsfELRjo3Ai8EJrFNSdi+uQBXWVGcKX4/ysJWcFwrGw==","tarball":"http://registry-npm.ywwl.com/@solid-primitives/event-listener/download/@solid-primitives/event-listener-1.3.2.tgz","fileCount":8,"unpackedSize":28754,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhsQx/CRA9TVsSAnZWagAAfN8P/i7CuXiWxRU4cJTBRGTr\nuIfx979JvBwi9TnjeEtzhAQoEer/kxA6Mji4G0e5QohgqazFd9bvB7czsM4O\neNost/LNq0fDLlgyXn9zDqmNhktr4CIJQ3dIaAN47yp8KmLhMtN8wxZAQuoK\nI0Ber446yI7DXbx/co+2R5kJn4bmAL3HKQbxtl9xch10pNBI4D2N6h94gNxn\nQQ/lhMVwKoTePb80/rRujZOfStKGCCKsIUgvJLsl8A9BehVF79oVO1alcY1n\nIzDUbH02j7uRC35rmB+SH6p98jnwc9PnYtmv2Fx0xMJlpktC8VPYPo17rNhK\nOzZJQEHh2KSl1ccyfIUi9geU150SI8G5X/YVSjnqS8cc2+ix5dXkweRzdiRF\nBLwHtvPjGDpmIUOGcpMEm8GM/E9Q0OZuM0wOazTHODHghNVi+FwjpTHXpTA1\n0mvFEdTXSsSfir0yVGxl9yfqNIVfbmF75uKSLP80SPQ6dAjvTHoGzlGPj9RW\nt3LxrP2rLJdfwvARHdDO8hdQc0V69PZv9ayy5LnFQVEFGJRmMUd7fbdwCBNb\nkXn0Jd19TgJzs5MzR2s1lrS4VtOkyj8D/sMt9RpwKj4/R8bFicKwXCqoQ+nM\nKg5J7j1CFWFclfenwcP9I1XrTGgSntLB2f45tat+6fr4TdCRgBjFmBUCm4pl\nfFTf\r\n=DJSH\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDu0aN+8wZ6z4lseXj8TwsZK9/K94TVSvMwFN1Zp7xpywIgSHhxjYURVy8IccsBl6lSsIrsV/9v40t2TUyPxc4C0F4="}],"size":6685,"noattachment":false,"key":"/@solid-primitives/event-listener/-/@solid-primitives/event-listener-1.3.2.tgz"},"_npmUser":{"name":"davedbase","email":"dave.dibiase@gmail.com"},"directories":{},"maintainers":[{"name":"lexlohr","email":"alex.th.kloss.77@googlemail.com"},{"name":"davedbase","email":"dave.dibiase@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/event-listener_1.3.2_1638993023224_0.07637140937795284"},"_hasShrinkwrap":false,"publish_time":1638993023378,"_cnpm_publish_time":1638993023378,"dist-tags":{"beta":"2.2.8-beta.0","latest":"2.4.6","next":"3.0.0-next.10"}}