use-gesture icon indicating copy to clipboard operation
use-gesture copied to clipboard

feat: add Vue and Svelte target

Open wobsoriano opened this issue 2 years ago • 23 comments

Initial PR that adds Svelte target package. It can be used together with Svelte's built-in spring function:

<script lang="ts">
  import { spring } from 'svelte/motion'
  import { drag } from '@use-gesture/svelte'

  let coords = spring({ x: 0, y: 0 });

  function handler({ detail }) {
    const {
      active,
      movement: [mx, my]
    } = detail

    coords.set({
      x: active ? mx : 0,
      y: active ? my : 0
    })
  }
</script>

<div
  use:drag
  on:drag={handler}
  style:transform="translate({$coords.x}px, {$coords.y}px)"
/>

I have no experience with changesets so I just copy-pasted some configs in package.json

wobsoriano avatar Jun 29 '22 22:06 wobsoriano

🦋 Changeset detected

Latest commit: 5d022fa14ba106be7d88951948b58db5ff824518

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@use-gesture/svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jun 29 '22 22:06 changeset-bot[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 5d022fa14ba106be7d88951948b58db5ff824518:

Sandbox Source
gesture-drag Configuration
gesture-drag-target Configuration
gesture-nested Configuration
gesture-drag-vanilla Configuration
gesture-move Configuration
gesture-pinch Configuration
gesture-multiple-pinch Configuration
gesture-three Configuration
gesture-card-zoom Configuration
gesture-viewpager Configuration

codesandbox-ci[bot] avatar Jun 29 '22 23:06 codesandbox-ci[bot]

Thanks! I'll have a look over the week-end :)

David

dbismut avatar Jul 01 '22 06:07 dbismut

Hey @wobsoriano, thanks a lot for this! I haven't looked through all the details but I guess exporting type GestureEvent is mandatory?

I've managed to integrate a svelte demo inside the main demo package using https://github.com/pngwn/svelte-adapter. You can try this locally and let me know if all of this works!

dbismut avatar Jul 02 '22 21:07 dbismut

Hi @dbismut !

but I guess exporting type GestureEvent is mandatory?

Yeah according to the doc, we need to create .d.ts to extend HTMLAttributes. What I did for now in this package is to export svelte-gesture/globals:

{
  "compilerOptions": {
    "types": ["svelte-gesture/globals"]
  }
}

I've managed to integrate a svelte demo inside the main demo package using https://github.com/pngwn/svelte-adapter. You can try this locally and let me know if all of this works!

I'll play around and update you!

wobsoriano avatar Jul 02 '22 22:07 wobsoriano

Hey @wobsoriano did you have the chance to look at this? Thanks!

dbismut avatar Jul 16 '22 13:07 dbismut

Hey @dbismut , sorry just now. Yes, I was able to test it locally and it works as expected!

wobsoriano avatar Jul 16 '22 15:07 wobsoriano

Demo when running pnpm demo:dev:

removed video

wobsoriano avatar Jul 28 '22 17:07 wobsoriano

@wobsoriano sorry I'm late on this. All good then right? Not sure if the screen recording is supposed to show an error 😅

dbismut avatar Aug 03 '22 17:08 dbismut

All good @dbismut ! Screen recording is just to show my local dev haha - it works

wobsoriano avatar Aug 03 '22 18:08 wobsoriano

Oh right :D Cool. While we're at it, we might as well release bindings for Vue. Let's see how easy that is. We were supposed to do this with @koca at some point, shouldn't be too hard.

dbismut avatar Aug 03 '22 18:08 dbismut

I have a working port of Vue as well https://github.com/wobsoriano/vuse-gesture

… and Solid https://github.com/wobsoriano/solid-gesture

wobsoriano avatar Aug 03 '22 18:08 wobsoriano

❤️ how hard would it be to port both? Not sure about Solid but I think I could embed Vue in the demo the same way I did for Svelte.

I would need to refactor some of the docs to make the docs either framework agnostic or find a way to have all APIs available.

dbismut avatar Aug 04 '22 15:08 dbismut

I already have a working port of Vue and can PR it. Do you want me to create a new PR or use this one?

wobsoriano avatar Aug 04 '22 18:08 wobsoriano

Let's do this one :) thanks!

dbismut avatar Aug 04 '22 19:08 dbismut

Added vue target @dbismut . Sample usage:

<script setup>
import { useSpring } from 'vue-use-spring'
import { normalizeProps, useDrag } from '@use-gesture/vue'

const position = useSpring({ x: 0, y: 0 })

const bind = useDrag(({ down, movement: [mx, my] }) => {
  position.x = down ? mx : 0
  position.y = down ? my : 0
})
</script>

<template>
  <div
    id="box"
    v-bind="normalizeProps(bind())"
    :style="{
      touchAction: 'none',
      transform: `translate(${position.x}px, ${position.y}px)`,
    }"
  />
</template>

wobsoriano avatar Aug 05 '22 17:08 wobsoriano

Hey @wobsoriano sorry I'm a bit drowning with work atm. Just a quick question, do we have to use normalizeProps with Vue?

dbismut avatar Aug 24 '22 08:08 dbismut

Hey @wobsoriano I've successfully managed to integrate Vue inside the Vite demo. However, it's throwing a type error on movement: image

I'll investigate later.

dbismut avatar Aug 28 '22 10:08 dbismut

@wobsoriano ok all good, this is now fixed.

dbismut avatar Aug 28 '22 16:08 dbismut

About normalizeProps, I'll try to integrate a way to have this inside core.

dbismut avatar Aug 28 '22 16:08 dbismut

Hey @wobsoriano sorry I'm a bit drowning with work atm. Just a quick question, do we have to use normalizeProps with Vue?

Yeah we have to, or integrate it internally because of this

const eventMap: Dict = {
  onKeyDown: 'onKeydown',
  onKeyup: 'onKeyup',
  onPointerCancel: 'onPointercancel',
  onPointerDown: 'onPointerdown',
  onPointerMove: 'onPointermove',
  onPointerEnter: 'onPointerenter',
  onPointerLeave: 'onPointerleave',
  onPointerUp: 'onPointerup',
  onWheel: 'onWheel',
  onScroll: 'onScroll'
}

wobsoriano avatar Aug 28 '22 16:08 wobsoriano

@wobsoriano I've added a way to pass normalize function to the gesture controller to make this cleaner for the end user. I don't know much about Vue, but it looks like it has a "v:on" bind function for events, that accepts handlers in the form of { mousedown: doStuff() }.

<script setup>
import { useSpring } from 'vue-use-spring'
import { useDrag } from '@use-gesture/vue'

const position = useSpring({ x: 0, y: 0 })

const bind = useDrag(({ down, movement: [mx, my] }) => {
  position.x = down ? mx : 0
  position.y = down ? my : 0
})
</script>

<template>
  <div
    id="box"
    v-on="bind()"
    :style="{
      touchAction: 'none',
      transform: `translate(${position.x}px, ${position.y}px)`,
    }"
  />
</template>

dbismut avatar Sep 13 '22 08:09 dbismut

Looks good to me @dbismut. I forgot about the v-on bind function! That looks clean.

wobsoriano avatar Sep 24 '22 15:09 wobsoriano