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

fling-swipe

v0.10.2

Published

Self-contained fling/swipe gesture implementation written in TypeScript.

Downloads

12

Readme

General

fling-swipe is a small and self-contained typescript implementation of the 'swipe' gesture. It also features 'fling' support, which triggers registered callbacks when the finger movement meets a speed threshold (rather than distance).

Note that this package doesn't provide ready-to-use components, but a logical building block to create your own.

Installation

npm i fling-swipe

Usage

There are 2 functions provided in this library, addFlingSwipe sets up gesture options and callbacks on an HTML element, while removeFlingSwipe deletes them. The examples below show their basic usage, but for better understanding it is recommended to take a look at the type definitions at the start of index.ts as well.

Standalone example

example.ts

import { 
  addFlingSwipe, 
  SwipeableHTMLElement, 
  Movement, 
  Direction, 
  Gesture
} from 'fling-swipe';

// NOTE: in the real world you would keep track of and take into account
// the index of the currently displayed element so you can shuffle around
// displayed data/elements to allow for endless swiping and virtualization

const target = document.getElementById('swipeable') as SwipeableHTMLElement;
const left = target.children[0] as HTMLElement;
const middle = target.children[1] as HTMLElement;
const right = target.children[2] as HTMLElement;

function init(src: SwipeableHTMLElement, movement: Movement) {
  // 'locked' class smoothes element movement -> remove during user interaction
  middle.classList.remove('locked');
  left.classList.remove('locked');
  right.classList.remove('locked');
}

function move(src: SwipeableHTMLElement, distance: number) {
  const percent = distance*100;
  left.style['transform'] = `translateX(${-100+percent}%)`;
  middle.style['transform'] = `translateX(${0+percent}%)`;
  right.style['transform'] = `translateX(${100+percent}%)`;
}

function swipe(src: SwipeableHTMLElement, touchDirection: Direction, gesture: Gesture) {
  // Add 'locked' classes to ensure elements smoothly move to their new positions
  middle.classList.add('locked');
  // Note that the 'touchDirection' is opposite to the element we want to switch to
  // -> e.g. if the thumb moves left, we scroll to the right hand side
  switch (touchDirection) {
    case Direction.LEFT:
      middle.style['transform'] = 'translateX(-100%)';
      right.classList.add('locked');
      right.style['transform'] = 'translateX(0%)';
      left.style.removeProperty('transform');
      break;
    case Direction.RIGHT:
      middle.style['transform'] = 'translateX(100%)';
      left.classList.add('locked');
      left.style['transform'] = 'translateX(0%)';
      right.style.removeProperty('transform');
      break;
    case Direction.NONE:
      left.classList.add('locked');
      right.classList.add('locked');
      middle.style.removeProperty('transform');
      left.style.removeProperty('transform');
      right.style.removeProperty('transform');
      break;
  }
}

addFlingSwipe(target, init, move, swipe);

index.html

<link rel="stylesheet" href="example.css">
<script src="example.js" defer></script>
<div id="swipeable">
  <div></div>
  <div></div>
  <div></div>
</div>

example.css

html, body {
  margin: 0;
}

#swipeable {
  position: relative;
  overflow-x: hidden;
  width: 100vw;
  height: 200px;
}
#swipeable > div {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: auto;
}
#swipeable > div:nth-child(1) {
  background-color: red;
  transform: translateX(-100%);
}
#swipeable > div:nth-child(2) {
  background-color: green;
  transform: translateX(0%);
}
#swipeable > div:nth-child(3) {
  background-color: blue;
  transform: translateX(100%);
}

.locked {
  transition: transform 200ms linear;
}

A more complicated example with 2-way scrolling & endless swiping can be found here.

SPA support

Since this package doesn't use any dependencies it is framework-agnostic. Nothing stops you from doing something like this in react for example:

import { addFlingSwipe, removeFlingSwipe } from 'fling-swipe';

function MyComponent() {
  const swipeable = useRef(null);

  useEffect(() => {
    addFlingSwipe(swipeable.current, /* your callbacks and options */);
    return () => {
      removeFlingSwipe(swipeable.current);
    };
  }, []);

  return (
    <div ref={swipeable}>
      {/* your swipeable content */}
    </div>
  );
}