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

@tencoder/longpressreact

v1.1.1

Published

A React wrapper with context provider for click and long press events that also conditionally render children.

Downloads

2

Readme

Long Press for React

For both desktop and touch devices on the web. This wrapper component is only compatible with projects that use React 16.8.0 and up since it uses hooks. The LongPress component allows you to create a long press on any children you give it. Want a simple button? Cool, pass it as a child. Want a whole card on mobile? It'll do that too. If the press time doesn't exceed the specified pressTime in props, it will behave as if the item had a normal click.

Conditionally Render another child based on the long press

This only excepts a single element
This can be helpful if you want to render an element based on if the press meets the long press threshold (that you specify with pressTime). For example, my use case was a vertical slider input that appears next to the button if the press/click is long enough (for dimming lights on my home network). If you look at the API you'll notice it accepts this conditional element as elementOnInteraction. You can specify if you want the element to hide if its not interacted with by setting inactiveHide and specify the length of time it takes before it hides with inactiveHideTime. If you want the conditional element to hide after the specified time, but want it to stay visible if it's interacted with before the time is up, then you can use the LongPress context in the element you passed in with:

import { LongPressContext } from '@tencoder/longpressreact';
const { setHasInteraction, setShowInteractionElements } = useContext(LongPressContext);

Call setHasInteraction(false) on its first touch/click event to have it remain visible. And if you want to hide the elementOnInteraction after its been engaged with, call setShowInteractionElements(false).

API

Import

import { LongPress } from '@tencoder/longpressreact';
import { LongPressContext } from '@tencoder/longpressreact';

Props

pressTime: Number in milliseconds || default = 300,
callbackStart: Function || default = null - triggers on start of a touch/click,
callbackRelease: Function || default = null - triggers on release of a touch/click,
onClickDefault: Function || default = null - triggers if normal touch/click (no long press),
id: String || default = '',
classNames: String || default = '',
elementOnInteraction: JSX || default = null,
inactiveHide: Boolean || default = false,
inactiveHideTime: Number in milliseconds || default = 3000

Examples

Simple element wrapped with LongPress

<LongPress 
  pressTime={300}
  inactiveHide={true}
  inactiveHideTime={2000}
  onClickDefault={onClickDefault}
  id={item.name}
  classNames={'card__command on_button'}
  elementOnInteraction={longPressInteractionElements()}
>
  <div>
    Some Button text or container that you want clickable.
  </div>
</LongPress>

My personal usage. Simple on/off button, that shows input element after long press.

const longPressInteractionElements = () => {
  return (
    <div className={`vertical-slider-container vertical-slider-container__${index}`}>
      <VerticalSlider
        update={updateValue}
        state={state}
      />
    </div>
  )
}
<LongPress 
  pressTime={300}
  inactiveHide={true}
  inactiveHideTime={2000}
  onClickDefault={onClickDefault}
  id={item.name}
  classNames={'card__command on_button'}
  elementOnInteraction={longPressInteractionElements()}
>
  <div>
    {(state > 0 || state === 'ON') ? state: 'Off'}
  </div>
</LongPress>

As mentioned above, here is the input element that is passed in as elementOnInteraction. When I long press the Off button, this vertical input appears. Based on my code snipped above this paragraph, the element will hide after 2 seconds unless I interact with the slider. I should note, clicking on input element isn't enough for me, it will still close. This is because it depends on what elements you attach the setHasInteraction and setShowInteractionElements to. image