@use-gesture

用于 React 和 Vanilla Javascript 中与组件绑定的鼠标/触摸手势的 "面包和黄油" 实用程序。「👇Bread n butter utility for component-tied mouse/touch gestures in React and Vanilla Javascript.」

Github星跟蹤圖

@use-gesture

npm (tag) npm bundle size NPM Discord Shield

@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.

Installation

React

#Yarn
yarn add @use-gesture/react

#NPM
npm install @use-gesture/react

Vanilla javascript

#Yarn
yarn add @use-gesture/vanilla

#NPM
npm install @use-gesture/vanilla

Full documentation website

Simple example

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.

Available hooks

@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

More on the full documentation website...

主要指標

概覽
名稱與所有者pmndrs/use-gesture
主編程語言TypeScript
編程語言TypeScript (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2018-03-23 15:37:58
推送於2024-07-15 14:21:52
最后一次提交
發布數162
最新版本名稱@use-gesture/vanilla@10.3.1 (發布於 2024-03-21 16:16:22)
第一版名稱v1.0.7 (發布於 2018-03-25 10:49:51)
用户参与
星數9.3k
關注者數42
派生數315
提交數1.4k
已啟用問題?
問題數334
打開的問題數37
拉請求數171
打開的拉請求數7
關閉的拉請求數30
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?