Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/vast-peaches-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@use-gesture/core': minor
'@use-gesture/react': minor
'@use-gesture/vanilla': minor
---

feat(core): Add `gestureKey` to the `state`
1 change: 1 addition & 0 deletions documentation/pages/docs/state.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const bind = useXXXX(state => {
buttons, // number of buttons pressed
touches, // number of fingers touching the screen
args, // arguments you passed to bind (React only)
gestureKey, // identifier for the gesture type currently active
ctrlKey, // true when control key is pressed
altKey, // " " alt " "
shiftKey, // " " shift " "
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/engines/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Controller } from '../Controller'
import { GestureKey, IngKey, NonUndefined, State, Vector2 } from '../types'
import { getEventDetails } from '../utils/events'
import { call } from '../utils/fn'
import { V, computeRubberband } from '../utils/maths'
import { GestureKey, IngKey, State, Vector2 } from '../types'
import { NonUndefined } from '../types'

/**
* The lib doesn't compute the kinematics on the last event of the gesture
Expand Down Expand Up @@ -74,7 +73,9 @@ export abstract class Engine<Key extends GestureKey> {
this.key = key

if (!this.state) {
this.state = {} as any
this.state = {
gestureKey: key
} as any
this.computeValues([0, 0])
this.computeInitial()

Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export type CommonGestureState = {
args?: any
}

export type CommonGestureKey = {
/**
* The gesture type.
*/
gestureKey: 'wheel' | 'scroll' | 'move' | 'hover'
}

export type CoordinatesState = CommonGestureState & {
/**
* The initial axis (x or y) of the gesture.
Expand Down Expand Up @@ -215,6 +222,10 @@ export type DragState = CoordinatesState & {
* [swipeX, swipeY] is [0, 0] if no swipe detected, -1 or 1 otherwise.
*/
swipe: Vector2
/**
* The gesture type.
*/
gestureKey: 'drag'
}

export interface PinchState extends CommonGestureState {
Expand Down Expand Up @@ -245,6 +256,10 @@ export interface PinchState extends CommonGestureState {
* Function that can be called to cancel the pinch.
*/
cancel(): void
/**
* The gesture type.
*/
gestureKey: 'pinch'
}

export type EventTypes = {
Expand All @@ -259,10 +274,10 @@ export type EventTypes = {
export interface State {
shared: SharedGestureState
drag?: DragState & { event: EventTypes['drag'] }
wheel?: CoordinatesState & { event: EventTypes['wheel'] }
scroll?: CoordinatesState & { event: EventTypes['scroll'] }
move?: CoordinatesState & { event: EventTypes['move'] }
hover?: CoordinatesState & { event: EventTypes['hover'] }
wheel?: CoordinatesState & CommonGestureKey & { event: EventTypes['wheel'] }
scroll?: CoordinatesState & CommonGestureKey & { event: EventTypes['scroll'] }
move?: CoordinatesState & CommonGestureKey & { event: EventTypes['move'] }
hover?: CoordinatesState & CommonGestureKey & { event: EventTypes['hover'] }
pinch?: PinchState & { event: EventTypes['pinch'] }
}

Expand Down