npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@frampton/events

v0.0.4

Published

Frampton-events is a library written in TypeScript for working with events in the browser.

Downloads

8

Readme

Frampton-Events

Frampton-events is a library written in TypeScript for working with events in the browser.

Installation

> npm install --save @frampton/core
> npm install --save @frampton/style
> npm install --save @frampton/events

Usage

Frampton-events is exposed as a commonJS module.

import * as Events from '@frampton/events';

onEvent

There are three methods for creating event streams exposed by frampton-events. The first on these is onEvent. It takes an event name and a HTMLElement to listen for events on. All events are delegated. The only listeners ever attached to the DOM are attached to the documentElement or the window.

The underlying event stream is a Frampton.Data.Signal. Checkout @frampton/core for more information.

import { onEvent } from '@frampton/events';


const clicks: Signal<Event> =
  onEvent('click', document.getElementById('my-id'));

onValue

From here we can listen for events.

clicks.onValue((evt: Event): void => {
  // do something
});

map

We can map events. There are a few utils provided for common transformations.

Get the value of the event target.

import { onEvent, eventValue } from '@frampton/events';


const inputValues: Signal<string> =
  onEvent('input', document.getElementById('my-input'))
    .map(eventValue);

filter

We can also filter events.

With this filter the event only continues on the stream if the event target contains an element matching the given selector.

import { onEvent, containsSelector } from '@frampton/events';


const inputValues: Signal<Event> =
  onEvent('input', document.getElementById('my-input'))
    .map(containsSelector('.active'));

debounce

A common need is debouncing events. One of many methods on Frampton.Data.Signal.

import { onEvent } from '@frampton/events';


const inputValues: Signal<Event> =
  onEvent('input', document.getElementById('my-input'))
    .debounce(100);

onSelector

We can also listen for events based on CSS selector.

This will be a stream of events on all elements matching the given selector.

import { onSelector } from '@frampton/events';


const clicks: Signal<Event> =
  onSelector('click', '.my-button');

onCustom

You can also listen for events on objects other than HTMLElements. This method will allow you to listen for events on any object that provides a method for listening to events, either through a method called 'on' or a method called 'addEventListener'. This will work with jQuery elements.

import { onCustom } from '@frampton/events';


const clicks: Signal<jQueryEvent> =
  onCustom('click', $('.my-button'));

Utilities

As previously seen there are provided utils for working with events.

closestToEvent

Gets the element closest to event target (closest parent) that matches given selector.

import { onSelector, closestToEvent } from '@frampton/events';


const elements: Signal<HTMLElement> =
  onSelector('click', '.my-element')
    .map(closestToEvent('.active'));

containsSelector

Returns a boolean indicating if the event target contains an element matching the given selector. Also returns true if the event target matches the selector.

import { onSelector, containsSelector } from '@frampton/events';


const clicks: Signal<Event> =
  onSelector('click', '.my-element')
    .filter(containsSelector('.active'));

contains

Returns a boolean indicating if the event target contains the given element.

import { onSelector, contains } from '@frampton/events';


cosnt testEl: HTMLElement =
  <HTMLElement>document.getElementById('my-id');


const clicks: Signal<Event> =
  onSelector('click', '.my-element')
    .filter(contains(testEl));

eventTarget

Returns the target element of event object.

import { onSelector, eventTarget } from '@frampton/events';


const elements: Signal<EventTarget> =
  onSelector('click', '.my-element')
    .map(eventTarget);

eventValue

Returns the value of the event target.

import { isSomething } from '@frampton/core';
import { onSelector, eventValue } from '@frampton/events';


const inputValues: Signal<string> =
  onSelector('input', '.my-element')
    .map(eventValue)
    .filter(isSomething);

hasSelector

Returns boolean indicating if event target matches given selector.

import { onSelector, hasSelector } from '@frampton/events';


const activeClicks: Signal<Event> =
  onSelector('click', '.my-element')
    .filter(hasSelector('.active'));

preventDefault

Calls preventDefault and stopPropagation on event object.

import { onSelector, preventDefault } from '@frampton/events';


const submitClicks: Signal<Event> =
  onSelector('click', '.submit-button')
    .map(preventDefault);

selectorContains

Returns a boolean indicating if the event target is contained inside an element matching given selector.

import { onSelector, selectorContains } from '@frampton/events';


const activeClicks: Signal<Event> =
  onSelector('clicks', '.my-element')
    .filter(selectorContains('.active'));

stopPropagation

Calls stopPropagation on event object.

import { onSelector, stopPropagation } from '@frampton/events';


const submitClicks: Signal<Event> =
  onSelector('click', '.submit-button')
    .map(stopPropagation);