Code Monkey home page Code Monkey logo

gesture's Introduction

VueUse - Collection of essential Vue Composition Utilities VueUse - Collection of essential Vue Composition Utilities
Collection of essential Vue Composition Utilities

NPM version NPM Downloads Docs & Demos Function Count
GitHub stars

๐Ÿš€ Features

  • ๐ŸŽช Interactive docs & demos
  • ๐Ÿ•ถ Seamless migration: Works for both Vue 3 and 2
  • โšก Fully tree shakeable: Only take what you want, bundle size
  • ๐Ÿฆพ Type Strong: Written in TypeScript, with TS Docs
  • ๐Ÿ”‹ SSR Friendly
  • ๐ŸŒŽ No bundler required: Usable via CDN
  • ๐Ÿ”ฉ Flexible: Configurable event filters and targets
  • ๐Ÿ”Œ Optional Add-ons: Router, Firebase, RxJS, etc.

๐Ÿฆ„ Usage

import { useLocalStorage, useMouse, usePreferredDark } from '@vueuse/core'

export default {
  setup() {
    // tracks mouse position
    const { x, y } = useMouse()

    // if user prefers dark theme
    const isDark = usePreferredDark()

    // persist state in localStorage
    const store = useLocalStorage(
      'my-storage',
      {
        name: 'Apple',
        color: 'red',
      },
    )

    return { x, y, isDark, store }
  },
}

Refer to functions list or documentations for more details.

๐Ÿ“ฆ Install

๐ŸŽฉ From v4.0, it works for Vue 2 & 3 within a single package by the power of vue-demi!

npm i @vueuse/core

Add ons | Nuxt Module

From v6.0, VueUse requires vue >= v3.2 or @vue/composition-api >= v1.1

Demos

CDN

<script src="https://unpkg.com/@vueuse/shared"></script>

<script src="https://unpkg.com/@vueuse/core"></script>

It will be exposed to global as window.VueUse

๐Ÿชด Project Activity

Alt

๐Ÿงฑ Contribute

See the Contributing Guide

๐ŸŒธ Thanks

This project is heavily inspired by the following awesome projects.

And thanks to all the contributors on GitHub!

๐Ÿ‘จโ€๐Ÿš€ Contributors

Financial Contributors on Open Collective

๐Ÿ“„ License

MIT License ยฉ 2019-PRESENT Anthony Fu

gesture's People

Contributors

ch99q avatar danielroe avatar fiadone avatar tahul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gesture's Issues

Drag continues when pointer is up

Hello,

I am using useDrag.
So far only been testing on desktop.

I notice quite frequently that if i lift the mouse button (well trackpad non pressed) the drag continues to function.
Further to that this can happen on multiple drag elements where if they get in this state moving pointer from one to another each will still continue their drag without the pointer being down.

I log the state event.down and state.down, and the down is still true in the state so even tho I am handling a "cancel" bassed on state.down it doesnt work because its the down that is getting stuck

console.log('pan-item drag', state.event.type, state.down, state);

result:
pan-item drag pointermove true {hovering: false, scrolling: false, wheeling: false, dragging: true, moving: false,ย โ€ฆ}

Other info: I have the axis locked to 'x'

Drag config option does not work

Hi I follow the doc using drag configuration like below

useGesture(
  {
    onDrag: (ctx) => handleDrag(ctx),
    onDragEnd: (ctx) => handleDragEnd(ctx),
  },
  {
    domTarget: sheetRef,
    drag: {
      filterTaps: true,
      preventWindowScrollY: true,
      useTouch: true,
      delay: 1000, // test
    },
  }
);

however, it seems to not apply at all. I test the delay and preventWindowScrollY it still does not work. I follow the doc here

Am I missing anything?

Add "types" to "exports" in package.json

VS Code is complaining about a "wrong" configuration when using this package in a TypeScript project:

Could not find a declaration file for module '@vueuse/gesture'. '[โ€ฆ]/node_modules/.pnpm/@vueuse[email protected][email protected]/node_modules/@vueuse/gesture/dist/index.mjs' implicitly has an 'any' type.
There are types at '[โ€ฆ]/node_modules/@vueuse/gesture/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@vueuse/gesture' library may need to update its package.json or typings.ts(7016)

It can be easily fixed by adding a reference to the types to the exports section:

"exports": {
  ".": {
    "import": "./dist/index.mjs",
    "require": "./dist/index.cjs",
    "types": "./dist/index.d.ts"
  }
},

Defining pinch bounds

Tried several ways of defining pinch bounds but none seem to work.

usePinch(pinchHandler, {
  domTarget: rootNode,
  eventOptions: {
    passive: false,
    distanceBounds: {max: 1000, min: -900}
  },
})

Is this supposed to constrain the value inside of da ?

Simple drag and drop list

Request: Open discussions to talk without open issues

After hours I cant manage to work with this lib. I dont know why and I dont understand how works properly. This lib is awesome but pure documentation, it need more examples.

I think drag directive have problem and it didnt work at all. Maybe I will give a try another time.
I use useGesture but didnt work.

<template>
  <div ref="container" class="h-screen flex flex-col justify-center items-center space-y-10">
    <span v-for="n in 3" :key="n" class="flex justify-center items-center size-12 bg-emerald-600 rounded text-lg font-bold select-none cursor-grab">{{ n }}</span>
  </div>
</template>

<script setup>
import { useSpring, useMotionProperties } from '@vueuse/motion'
import { useDrag } from '@vueuse/gesture'
import clamp from 'lodash/clamp'

const container = ref()
const boxes = computed(() => Array.from(container.value.children).map((box) => ({
  element: box,
  y: box.getBoundingClientRect().y,
  x: box.getBoundingClientRect().x,
})))

onMounted(() => {
  boxes.value.map((box, index) => {
    const { motionProperties } = useMotionProperties(box.element, {
      cursor: 'grab',
      x: 0,
      y: 0
    })

    const { set } = useSpring(motionProperties, {
      damping: 50,
      stiffness: 1000,
    })

    useDrag(
      ({ movement: [x, y], dragging }) => {
        console.log(x, y, dragging);
        if (!dragging) {
          set({ y: 0, cursor: 'grab' })
          
          return
        }

        const currentY = clamp(Math.round((index * 100 + y) / 100), 0, child.length - 1)
        console.log(currentY)
        set({ cursor: 'grabbing', currentY })
      },
      { domTarget: box.element }
    )
  })
})
</script>

multiGesturemexample js version

hi,

would be great if you could provide a js version of the multigesture ts version. I couldnโ€˜t get the ts version to work.

thank you.

FR: resize directive

would be great if we could resize a Dom element by dragging on any side of the rendered Dom element

Dragging problem / Minimal drag example

I'm trying to reproduce a minimal drag example, where the user can drag a component somewhere, and it stays there. Problem is: the first time the user drags the component, it stays there, but as soon as he tries to re-drag the component, it goes back to the (0,0) position and begins the next drag from there.

gifgesture

My code is the following:

<template>
  <div ref="widgetRef" class="widget">
    <span>Widget</span>
  </div>
</template>

<script setup lang="ts">
import { useGesture } from '@vueuse/gesture'
import { type PermissiveMotionProperties, useMotionProperties, useSpring } from '@vueuse/motion'
import { ref } from 'vue'

const widgetRef = ref()

const { motionProperties } = useMotionProperties(widgetRef, {
  cursor: 'grab',
})
const { set } = useSpring(motionProperties as Partial<PermissiveMotionProperties>)

useGesture(
  {
    onDrag: ({ movement: [x, y] }) => set({ x, y, scale: 2 }),
    onDragEnd: () => set({ scale: 1 }),
  },
  {
    domTarget: widgetRef,
  }
)
</script>

<style scoped>
.widget {
  padding: 10px;
  margin: 5px;
  background-color: antiquewhite;
}
</style>

Expose domElement in options

I would like to be able to access the original domElement that the handler has been attached to inside gesture callbacks.

The use case would be (simplified code):

<script setup>
// Quick drag-to-scroll implementation
function handleDrag({ domElement, delta: [x, y] }) {
  domElement.scrollBy(-x, -y)
}
</script>

<template>
  <div v-drag="handleDrag">
    <div>child</div>
    <div>child</div>
  </div>
</template>

I realize I can work around this with an additional ref or even with a custom directive (as I did), my point here being reducing boilerplate for simpler ad-hoc code.

"mapper()" missing in docu

image this is what's written in scroll feature example. it says user can find more about `set()` and `mapper()` on the "Motion Integration" page.

image
set is only available, mapper docu is missing. tried searching in vueuse/motion but no mapper syntax was found... or did i just looked past through it

How to make a Gallery?

I'm trying to make a swipe gallery with multiple images:

  <div class="gallery">
    <div ref="anchor" class="gallery-images" :style="{width: `${screenWidth * 6}px`}">
      <img v-for="image in images" :key="image" :src="require(`@/assets/images/${image}`)" :style="{width: `${screenWidth}px`}">
    </div>
  </div>

JS:

  setup() {
    // TODO dynamically change
    const screenWidth = ref(375);

    const anchor = ref();

    const { motionProperties } = useMotionProperties(anchor, {
      cursor: 'grab',
      x: 0,
      y: 0,
    });

    const { set } = useSpring(motionProperties);

    const dragHandler = ({ movement: [x], dragging }) => {
      if (!dragging) {
        set({ x: 0, y: 0, cursor: 'grab' });
        return;
      }

      set({ x, y: 0, cursor: 'grabbing' });
    };

    useDrag(dragHandler, {
      domTarget: anchor,
    });

    return {
      anchor,
      screenWidth,
    };
  },

CSS

.gallery {
  @apply overflow-x-hidden;
}
.gallery-images {
  @apply flex flex-row justify-start items-start;
}

It works fine on mobile but on desktop, I can see it changing the cursor and attempting to change the translateZ (it changes for 1ms and changes back to zero), from there it just stays zero and on desktop it wont drag.

If I use incognito device emulation and make the device very large, same as regular desktop, it DOES work, so maybe it has to do with gestures / mouse not being 1:1 consistent?

does not rebind when domTarget changes

I use if/else on the domTarget and when it toggled, the gesture event stopped working. Is there a way to make it reactive? I try to call useGesture() again in watcher but it still does not solve my issue

onDrag (in useGesture) fires just a few times and lags

When I use onDrag like following:

useGesture({
  onDrag: dragHandler
}, {
  domTarget: image
})

with following handler:

const dragHandler = ({ movement: [x, y], dragging }) => {
  if(dragging) {
    console.log("moving")
    console.log({ x, y })
    imageConfiguration.value.top += y
    imageConfiguration.value.left += x
  }
}

the dragging event fires only about 2 to 4 times and doesnt continue.

For example when i use standart eventListener to the same element, the event fires for each pixel that I dragged. But here it completely lags and I cant use it.

Can someone help me with what am I doing wrong? Or do I misunderstand the usage?

For the whole picture, I use then the imageConfiguration to create style for the element I want to drag ->

const imageStyle = computed(() => {
  return {
    'top': imageConfiguration.value.top.toString() + 'px',
    'left': imageConfiguration.value.left.toString() + 'px',
  }
})

prevent scroll on drag doesn't work

I have an app that uses drag to adjust the volume of oscillators. I use drag handler to get the x and y difference. It works fine on desktop, but I can't prevent my ipad safari from scrolling.
Here are my directives

  v-drag="dragHandler",
  :drag-options="dragOptions",

the options

const dragOptions = reactive({
  filterTaps: true,
  preventWindowScrollY: true,
  delay: 0,
  eventOptions: {
    passive: false
  }
})

the handler

const dragHandler = (dragEvent) => {
  dragEvent.event.preventDefault()
...

But still it scrolls vertically when a cell is dragged.

The actual component in the repo https://github.com/davay42/pitch-table/blob/master/src/components/table/cell.vue

ipad-demo-s.mov

Use in Nuxt3

How to use the vueuse/gesture component in Nuxt3?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.