@solid-primitives/event-listener
Primitive to manage creating event listeners.
Last updated 5 years ago by davedbase .
MIT · Repository · Original npm · Tarball · package.json
$ npm install @solid-primitives/event-listener 
SYNC missed versions from official npm registry.

@solid-primitives/event-listener

lerna size size

A helpful event listener primitive that binds window and any element supplied.

Installation

npm install @solid-primitives/event-listener
# or
yarn add @solid-primitives/event-listener

createEventListener

Can 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.

How to use it

import { createEventListener } from "@solid-primitives/event-listener";

createEventListener(
  document.getElementById("mybutton"),
  "mousemove",
  e => console.log("x:", e.pageX, "y:", e.pageY),
  { passive: true }
);

// target element, event name and options can be reactive signals
const [ref, setRef] = createSignal<HTMLElement>();
const [name, setName] = createSignal("mousemove");
const [options, setOptions] = createSignal({ passive: true });
createEventListener(ref, name, e => {}, options);

// you can provide your own event map type as well:
createEventListener<{ myCustomEvent: Event }>(window, "myCustomEvent", () => console.log("yup!"));
// just don't use interfaces as EventMaps!

Directive Usage

props passed to the directive are also reactive, so you can change handlers on the fly.

import { eventListener } from "@solid-primitives/event-listener";
// avoids tree-shaking the directive:
eventListener;

<button use:eventListener={["click", () => console.log("Click")]}>Click!</button>;

Types

function createEventListener<
  EventMap extends Record<string, Event>,
  EventName extends keyof EventMap
>(
  target: MaybeAccessor<Many<EventTarget>>,
  eventName: MaybeAccessor<EventName>,
  handler: (event: EventMap[EventName]) => void,
  options?: MaybeAccessor<boolean | AddEventListenerOptions>
): void;

// Directive
function eventListener(
  target: Element,
  props: Accessor<EventListenerDirectiveProps>
): EventListenerReturn;

type EventListenerDirectiveProps = [
  name: string,
  handler: (e: any) => void,
  options?: AddEventListenerOptions | boolean
];

createEventSignal

Like createEventListener, but events are handled with the returned signal, instead of with a callback.

How to use it

import { createEventSignal } from "@solid-primitives/event-listener";

// all arguments can be reactive signals
const lastEvent = createEventSignal(el, "mousemove", { passive: true });

createEffect(() => {
  console.log(lastEvent()?.x, lastEvent()?.y);
});

createEventListenerMap

A helpful primitive that listens to a map of events. Handle them by individual callbacks.

How to use it

import { createEventListenerMap } from "@solid-primitives/event-listener";

createEventListenerMap(element, {
  mousemove: mouseHandler,
  mouseenter: e => {},
  touchend: touchHandler
});

// both target and options args can be reactive:
const [target, setTarget] = createSignal(document.getElementById("abc"));
const [options, setOptions] = createSignal({ passive: true });
createEventListenerMap(
  target,
  {
    mousemove: e => {},
    touchstart: e => {}
  },
  options
);

// createEventListenerMap can be used to listen to custom events
createEventListenerMap<
  {
    myEvent: MyEvent;
    custom: Event;
    other: Event;
  },
  "myEvent" | "custom"
>(target, {
  myEvent: e => {},
  custom: e => {}
});

Directive usage

import { eventListenerMap } from "@solid-primitives/event-listener";
// prevent tree-shaking:
eventListenerMap;

<div
  use:eventListenerMap={{
    mousemove: e => {},
    click: clickHandler,
    touchstart: () => {},
    myCustomEvent: e => {}
  }}
></div>;

Types

function createEventListenerMap<
  EventMap extends Record<string, Event>,
  UsedEvents extends keyof EventMap = keyof EventMap
>(
  target: MaybeAccessor<Many<EventTarget>>,
  handlersMap: EventHandlersMap,
  options?: MaybeAccessor<boolean | AddEventListenerOptions>
): void;

createEventStore

Similar to createEventListenerMap, but provides a reactive store with the latest captured events.

How to use it

const lastEvents = createEventStore(el, "mousemove", "touchend", "click");

createEffect(() => {
  console.log(lastEvents?.mousemove.x);
});

// both target and options args can be reactive:
const [target, setTarget] = createSignal(document.getElementById("abc"));
const [options, setOptions] = createSignal({ passive: true });
const lastEvents = createEventStore(target, options, "mousemove", "touchmove");

// createEventStore can be used to listen to custom events
const lastEvents = createEventStore<
  {
    myEvent: MyEvent;
    custom: Event;
  },
  "myEvent" | "custom"
>(target, "myEvent", "custom");

// DON'T DO THIS:
const { mousemove } = createEventStore(target, "mousemove", ...);
// the store cannot be destructured

types

function createEventStore<
  EventMap extends Record<string, Event>,
  UsedEvents extends keyof EventMap = keyof EventMap
>(
  target: MaybeAccessor<Many<EventTarget>>,
  ...eventNames: UsedEvents[]
): Store<Partial<Pick<EventMap, UsedEvents>>>;

// with options:
function createEventStore<
  EventMap extends Record<string, Event>,
  UsedEvents extends keyof EventMap = keyof EventMap
>(
  target: MaybeAccessor<Many<EventTarget>>,
  options: MaybeAccessor<boolean | AddEventListenerOptions>,
  ...eventNames: UsedEvents[]
): Store<Partial<Pick<EventMap, UsedEvents>>>;

WindowEventListener

Listen to the window DOM Events, using a component.

You can use it with any Solid's Control-Flow components, e.g. <Show/> or <Switch/>.

The event handler prop is reactive, so you can use it with signals.

How to use it

import { WindowEventListener } from "@solid-primitives/event-listener";

<WindowEventListener onMouseMove={e => console.log(e.x, e.y)} />;

DocumentEventListener

The same as WindowEventListener, but listens to document events.

How to use it

import { DocumentEventListener } from "@solid-primitives/event-listener";

<DocumentEventListener onMouseMove={e => console.log(e.x, e.y)} />;

Demo

You may view a working example here: https://codesandbox.io/s/solid-primitives-event-listener-elti5

Changelog

<summary>Expand Changelog</summary>

0.0.100

First ported commit from react-use-event-listener.

1.1.4

Released a version with type mostly cleaned up.

1.2.3

Switched to a more idiomatic pattern: Warning: incompatible with the previous version!

1.2.5

Added CJS build.

1.2.6

Migrated to new build process.

1.3.0

(minor breaking changes to type generics and returned functions) Primitive rewritten to provide better types and more reliable usage. Added more primitives.

Current Tags

  • 2.2.8-beta.0                                ...           beta (4 years ago)
  • 2.4.6                                ...           latest (5 months ago)
  • 3.0.0-next.10                                ...           next (17 days ago)

56 Versions

  • 3.0.0-next.10                                ...           17 days ago
  • 3.0.0-next.9                                ...           a month ago
  • 3.0.0-next.8                                ...           a month ago
  • 3.0.0-next.5                                ...           3 months ago
  • 3.0.0-next.4                                ...           3 months ago
  • 3.0.0-next.3                                ...           3 months ago
  • 3.0.0-next.2                                ...           4 months ago
  • 2.4.6                                ...           5 months ago
  • 3.0.0-next.1                                ...           5 months ago
  • 3.0.0-next.0                                ...           5 months ago
  • 2.4.5                                ...           9 months ago
  • 2.4.4                                ...           9 months ago
  • 2.4.3                                ...           a year ago
  • 2.4.2                                ...           a year ago
  • 2.4.1                                ...           2 years ago
  • 2.4.0                                ...           2 years ago
  • 2.3.3                                ...           3 years ago
  • 2.3.2                                ...           3 years ago
  • 2.3.1                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.14                                ...           3 years ago
  • 2.2.13                                ...           4 years ago
  • 2.2.12                                ...           4 years ago
  • 2.2.11                                ...           4 years ago
  • 2.2.10                                ...           4 years ago
  • 2.2.9                                ...           4 years ago
  • 2.2.8                                ...           4 years ago
  • 2.2.8-beta.0                                ...           4 years ago
  • 2.2.7                                ...           4 years ago
  • 2.2.6                                ...           4 years ago
  • 2.2.5                                ...           4 years ago
  • 2.2.4                                ...           4 years ago
  • 2.2.3                                ...           4 years ago
  • 2.2.2                                ...           4 years ago
  • 2.2.1                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.0                                ...           5 years ago
  • 1.6.1                                ...           5 years ago
  • 1.6.0                                ...           5 years ago
  • 1.5.0                                ...           5 years ago
  • 1.4.2                                ...           5 years ago
  • 1.4.1                                ...           5 years ago
  • 1.3.8                                ...           5 years ago
  • 1.3.7                                ...           5 years ago
  • 1.3.6                                ...           5 years ago
  • 1.3.5                                ...           5 years ago
  • 1.3.4                                ...           5 years ago
  • 1.3.2                                ...           5 years ago
  • 1.2.6                                ...           5 years ago
  • 1.2.5                                ...           5 years ago
  • 1.2.4                                ...           5 years ago
  • 1.2.3                                ...           5 years ago
  • 1.1.4                                ...           5 years ago
  • 1.1.3                                ...           5 years ago
  • 0.0.100                                ...           5 years ago
Maintainers (2)
Downloads
Total 48
Today 0
This Week 1
This Month 5
Last Day 0
Last Week 0
Last Month 43
Dependencies (1)
Dev Dependencies (10)
Dependents (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE