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

react-proximity-feedback

v2.0.0

Published

React render-prop component to get the proximity of the cursor to a DOM node.

Downloads

17

Readme

react-proximity-feedback

Based on Codrops article: Ideas for Proximity Feedback with Progressive Hover Effects

Codrops GitHub repository

A small render prop component to provide proximity feedback of the mouse cursor and a DOM node (e.g. a button).

Demo

Demo

Edit react-proximity-feedback sandbox

Version 2.0.0

These features are new with version 2.0.0

  • Rewritten in TypeScript
  • Only visible elements will be considered. Thanks to @eastkorzh for the inspiration.
  • There's also a hook now useProximityFeedback({ threshold, throttleInMs})

Important With version 2.0.0 we use the IntersectionObserver API which does not work in IE11. You'll need a polyfill for that

Installation

Using npm

npm install --save react-proximity-feedback

Constraints: No mobile support

Since you need access to the cursor this component this does not work on touch only devices.

Usage

You want to calculate the distance between the mouse cursor and a button.

With hooks

Use the hook and attach the returned ref to the component you want to calculate the distance to.

import React from 'react';
import { useProximityFeedback } from 'react-proximity-feedback';

const DemoComponent = () => {
  const { ref, distance } = useProximityFeedback();

  return <button ref={ref}>The cursor is {distance}px away</button>;
};

Arguments

You can optionally pass two arguments to the useProximityFeedback hook:

  • threshold: When the mouse is between 0 and this threshold in px the proximity feedback will be triggered and calculated. (Demo)

  • throttleInMs: The time in milliseconds the proximity will be calculated. The lower the number the higher is the frequency the proximity will be calculated. Defaults to 250.

Returned values

ref

It is important that you pass-through this ref to the DOM node you want to calculate the proximity of.

Example (Demo)

const Demo = () => {
  const { ref } = useProximityFeedback();
  return <button ref={ref}>Hello World</button>;
};

distance

The distance between the "refed" component and the mouse cursor in px.

Example (Demo)

const Demo = () => {
  const { ref, distance } = useProximityFeedback();

  return <button ref={ref}>The mouse cursor is {distance}px away</button>;
};

isNearby

A boolean value to represent if the cursor is 0 <= distance <= props.threshold.

Example (Demo)

const Demo = () => {
  const { ref, isNearby } = useProximityFeedback();

  return (
    <button ref={ref}>The cursor is {isNearby ? 'nearby' : 'far away'}</button>
  );
};

proximity

A float value from 0 to 1 rounded to two decimal places. When the distance of the mouse cursor is >= props.threshold the proximity value is 0. The proximity is 1 if the cursor is right on top of the "refed" component. It represents the value from 0% proximity to 100% proximity.

Example (Demo)

const Demo = () => {
  const { ref, proximity } = useProximityFeedback();

  const outlineStyle = `3x solid rgba(255,0,0, ${proximity})`;
  return (
    <button ref={ref} style={outlineStyle}>
      Come closer
    </button>
  );
};

With the render prop component

Wrap the button inside the <ProximityFeedback> component and provide the ref attribute.

import React from 'react';
import ProximityFeedback from 'react-proximity-feedback';

const DemoComponent = () => (
  <ProximityFeedback>
    {({ ref, distance }) => (
      <button ref={ref}>The cursor is {distance}px away</button>
    )}
  </ProximityFeedback>
);

export default DemoComponent;

Try it out on CodeSandbox

Props

You can pass two props to the ProximityFeedback component:

  • threshold: When the mouse is between 0 and this threshold in px the proximity feedback will be triggered and calculated. (Demo)

  • throttleInMs: The time in milliseconds the proximity will be calculated. The lower the number the higher is the frequency the proximity will be calculated. Defaults to 250.

Render Props

You have access to these render props. More information on render-props

ref

It is important that you pass-through this ref to the DOM node you want to calculate the proximity of.

Example (Demo)

<ProximityFeedback>
  {({ ref }) => <button ref={ref}>Hello World</button>}
</ProximityFeedback>

distance

The distance between the "refed" component and the mouse cursor in px.

Example (Demo)

<ProximityFeedback>
  {({ ref, distance }) => (
    <button ref={ref}>The mouse cursor is {distance}px away</button>
  )}
</ProximityFeedback>

isNearby

A boolean value to represent if the cursor is 0 <= distance <= props.threshold.

Example (Demo)

<ProximityFeedback>
  {({ ref, isNearby }) => (
    <button ref={ref}>The cursor is {isNearby ? 'nearby' : 'far away'}</button>
  )}
</ProximityFeedback>

proximity

A float value from 0 to 1 rounded to two decimal places. When the distance of the mouse cursor is >= props.threshold the proximity value is 0. The proximity is 1 if the cursor is right on top of the "refed" component. It represents the value from 0% proximity to 100% proximity.

Example (Demo)

<ProximityFeedback>
  {({ ref, proximity }) => {
    const outlineStyle = `3x solid rgba(255,0,0, ${proximity})`;
    return (
      <button ref={ref} style={outlineStyle}>
        Come closer
      </button>
    );
  }}
</ProximityFeedback>

Development

Testing

Testing is done via cypress.

Commands

  • npm run test Run the jest test in tests/ProximityFeedback.test.js once
  • npm run test:headless Run the cypress test in headless mode

Build

Run npm run build to build the project and copy the demo files from demo/dist/ to docs/.


This project is using tsdx