Security News
cURL Project and Go Security Teams Reject CVSS as Broken
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
@use-gesture/react
Advanced tools
@use-gesture/react is a library that provides a set of hooks to handle gestures in React applications. It allows developers to easily add touch and pointer event handling to their components, supporting gestures like dragging, pinching, scrolling, and more.
Drag
This code demonstrates how to use the `useDrag` hook to make a div element draggable. The position of the element is updated based on the drag state.
```jsx
import { useDrag } from '@use-gesture/react';
import { useState } from 'react';
function Draggable() {
const [position, setPosition] = useState({ x: 0, y: 0 });
const bind = useDrag((state) => {
setPosition({ x: state.offset[0], y: state.offset[1] });
});
return (
<div
{...bind()}
style={{
transform: `translate3d(${position.x}px, ${position.y}px, 0)`,
width: 100,
height: 100,
background: 'lightblue',
}}
/>
);
}
```
Pinch
This code demonstrates how to use the `usePinch` hook to make a div element pinchable. The scale of the element is updated based on the pinch state.
```jsx
import { usePinch } from '@use-gesture/react';
import { useState } from 'react';
function Pinchable() {
const [scale, setScale] = useState(1);
const bind = usePinch((state) => {
setScale(state.offset[0]);
});
return (
<div
{...bind()}
style={{
transform: `scale(${scale})`,
width: 100,
height: 100,
background: 'lightcoral',
}}
/>
);
}
```
Scroll
This code demonstrates how to use the `useScroll` hook to handle scroll events. The scroll position is updated based on the scroll state.
```jsx
import { useScroll } from '@use-gesture/react';
import { useState } from 'react';
function Scrollable() {
const [scroll, setScroll] = useState({ x: 0, y: 0 });
const bind = useScroll((state) => {
setScroll({ x: state.offset[0], y: state.offset[1] });
});
return (
<div
{...bind()}
style={{
width: 200,
height: 200,
overflow: 'auto',
background: 'lightgreen',
}}
>
<div style={{ width: 400, height: 400 }}>
Scroll me!
</div>
</div>
);
}
```
react-use-gesture is a library that provides hooks for handling gestures in React applications. It is similar to @use-gesture/react and offers a similar API for handling gestures like dragging, pinching, and scrolling.
react-spring is a library for creating animations in React applications. While it is primarily focused on animations, it also provides hooks for handling gestures, making it a versatile choice for developers who need both animations and gesture handling.
react-draggable is a library specifically for making elements draggable in React applications. It provides a simple API for adding drag-and-drop functionality to components, but it does not support other gestures like pinching or scrolling.
@use-gesture is a library that lets you bind richer mouse and touch events to any component or view. With the data you receive, it becomes trivial to set up gestures, and often takes no more than a few lines of code.
You can use it stand-alone, but to make the most of it you should combine it with an animation library like react-spring, though you can most certainly use any other.
The demos are real click them!
#Yarn
yarn add @use-gesture/react
#NPM
npm install @use-gesture/react
#Yarn
yarn add @use-gesture/vanilla
#NPM
npm install @use-gesture/vanilla
import { useSpring, animated } from '@react-spring/web'
import { useDrag } from '@use-gesture/react'
function Example() {
const [{ x, y }, api] = useSpring(() => ({ x: 0, y: 0 }))
// Set the drag hook and define component movement based on gesture data.
const bind = useDrag(({ down, movement: [mx, my] }) => {
api.start({ x: down ? mx : 0, y: down ? my : 0 })
})
// Bind it to a component.
return <animated.div {...bind()} style={{ x, y, touchAction: 'none' }} />
}
<!-- index.html -->
<div id="drag" />
// script.js
const el = document.getElementById('drag')
const gesture = new DragGesture(el, ({ active, movement: [mx, my] }) => {
setActive(active)
anime({
targets: el,
translateX: active ? mx : 0,
translateY: active ? my : 0,
duration: active ? 0 : 1000
})
})
// when you want to remove the listener
gesture.destroy()
The example above makes a div
draggable so that it follows your mouse on drag, and returns to its initial position on release.
Make sure you always set touchAction
on a draggable element to prevent glitches with the browser native scrolling on touch devices.
@use-gesture/react exports several hooks that can handle different gestures:
Hook | Description |
---|---|
useDrag | Handles the drag gesture |
useMove | Handles mouse move events |
useHover | Handles mouse enter and mouse leave events |
useScroll | Handles scroll events |
useWheel | Handles wheel events |
usePinch | Handles the pinch gesture |
useGesture | Handles multiple gestures in one hook |
FAQs
React target for @use-gesture
The npm package @use-gesture/react receives a total of 548,600 weekly downloads. As such, @use-gesture/react popularity was classified as popular.
We found that @use-gesture/react demonstrated a healthy version release cadence and project activity because the last version was released less than 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
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.