![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
use-swipe-hook
Advanced tools
A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens
A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens & non-touch screens via mouse events
npm i use-swipe-hook
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import './styles.css';
import { useSwipe } from 'use-swipe-hook';
function App() {
const swipeContainerRef = useRef<HTMLDivElement>(null);
const direction = useSwipe({ ref: swipeContainerRef, thresholdPX: 5 });
return (
<div className="App">
<h1>use-swipe-hook demo</h1>
<h2>Works on both touch & non touch devices (by dragging mouse over the container)</h2>
<div className="swipeContainer" ref={swipeContainerRef}>
{direction ? `You have swiped ${direction}` : 'Swipe here to see swipe direction'}
</div>
</div>
);
}
const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);
Version 2 introduces 2 new hooks for advanced usage.
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import './styles.css';
import { useSwipeVector } from 'use-swipe-hook';
function UseSwipeVector() {
const swipeContainerRef = useRef<HTMLDivElement>(null);
const {
magnitude,
direction,
origin: { x, y },
} = useSwipeVector({
ref: swipeContainerRef,
thresholdPX: 5,
unit: 'deg',
useRelativeUnits: true,
});
return (
<div className="swipeContainer vector axis" ref={swipeContainerRef}>
{magnitude ? (
<>
You have swiped {magnitude.toFixed(2)}px towards {direction.toFixed(0)}° starting from ({x.toFixed(0)},{' '}
{y.toFixed(0)})
</>
) : (
'Swipe here to see swipe direction'
)}
</div>
);
}
const rootElement = document.getElementById('root');
ReactDOM.render(<UseSwipeVector />, rootElement);
import React, { useRef } from 'react';
import ReactDOM from 'react-dom';
import './styles.css';
import { useSwipePosition } from 'use-swipe-hook';
function UseSwipePosition() {
const swipeContainerRef = useRef<HTMLDivElement>(null);
const { x1, y1, x2, y2 } = useSwipePosition({
ref: swipeContainerRef,
thresholdPX: 5,
useRelativeUnits: true,
});
return (
<div className="swipeContainer position axis" ref={swipeContainerRef}>
{x1 ? (
<>
You have swiped from ({x1.toFixed(0)}, {y1.toFixed(0)}) to ({x2.toFixed(0)}, {y2.toFixed(0)})
</>
) : (
'Swipe here to see swipe direction'
)}
</div>
);
}
const rootElement = document.getElementById('root');
ReactDOM.render(<UseSwipePosition />, rootElement);
The library uses code splitting, so even though 2 new hooks are introduced in version 2 and the overall bundle size is slightly increased, the individual hook bundle size remains small when you use a single hook on you application. This means more features but still leaner & tiny bundle size.
// SWIPE_DIRECTION can be imported & used for comparison
enum SWIPE_DIRECTION {
RIGHT = 'right',
LEFT = 'left',
UP = 'up',
DOWN = 'down',
}
interface UseSwipeOptions {
// ref of the container where you want to attach swipe event
ref: RefObject<HTMLElement>;
// (optional) no of pixels to move your finger to trigger a swipe event.
// Larger this value means less sensitivity. Default value is 5 (5px)
thresholdPX?: number;
}
const useSwipe: ({ ref, thresholdPX }: UseSwipeOptions) => SWIPE_DIRECTION | null;
interface UseSwipeVectorOptions {
// ref of the container where you want to attach swipe event
ref: RefObject<HTMLElement>;
// (optional) no of pixels to move your finger to trigger a swipe event.
// Larger this value means less sensitivity. Default value is 5 (5px)
thresholdPX?: number;
unit?: 'rad' | 'deg'; // unit of direction for useSwipeVector hook
// whether to use position units based relative to canvas rather than with respect to window
useRelativeUnits?: boolean;
}
const useSwipeVector: ({ ref, thresholdPX, unit, useRelativeUnits }: UseSwipeVectorOptions) => SWIPE_DIRECTION | null;
interface UseSwipePositionOptions {
// ref of the container where you want to attach swipe event
ref: RefObject<HTMLElement>;
// (optional) no of pixels to move your finger to trigger a swipe event.
// Larger this value means less sensitivity. Default value is 5 (5px)
thresholdPX?: number;
// whether to use position units based relative to canvas rather than with respect to window
useRelativeUnits?: boolean;
}
const useSwipePosition: ({ ref, thresholdPX, useRelativeUnits }: UseSwipePositionOptions) => SWIPE_DIRECTION | null;
This repo uses github actions to publish a package when:
[publish]
keyword otherwise it will not publish on merge & just do build time checks.npm publish
command will fail.On any other branch commits it will run type-check & build checks if src folder files are changed.
FAQs
A simple and easy to use tiny library that provides useSwipe hook to use with React that enables swipe gestures for touch screens
The npm package use-swipe-hook receives a total of 0 weekly downloads. As such, use-swipe-hook popularity was classified as not popular.
We found that use-swipe-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.