1 | import { PointerType } from '../types'
|
---|
2 | import { DragConfig, InternalDragOptions, Vector2 } from '../types'
|
---|
3 | import { V } from '../utils/maths'
|
---|
4 | import { coordinatesConfigResolver } from './coordinatesConfigResolver'
|
---|
5 | import { SUPPORT } from './support'
|
---|
6 |
|
---|
7 | export const DEFAULT_PREVENT_SCROLL_DELAY = 250
|
---|
8 | export const DEFAULT_DRAG_DELAY = 180
|
---|
9 | export const DEFAULT_SWIPE_VELOCITY = 0.5
|
---|
10 | export const DEFAULT_SWIPE_DISTANCE = 50
|
---|
11 | export const DEFAULT_SWIPE_DURATION = 250
|
---|
12 | export const DEFAULT_KEYBOARD_DISPLACEMENT = 10
|
---|
13 |
|
---|
14 | const DEFAULT_DRAG_AXIS_THRESHOLD: Record<PointerType, number> = { mouse: 0, touch: 0, pen: 8 }
|
---|
15 |
|
---|
16 | export const dragConfigResolver = {
|
---|
17 | ...coordinatesConfigResolver,
|
---|
18 | device(
|
---|
19 | this: InternalDragOptions,
|
---|
20 | _v: any,
|
---|
21 | _k: string,
|
---|
22 | { pointer: { touch = false, lock = false, mouse = false } = {} }: DragConfig
|
---|
23 | ) {
|
---|
24 | this.pointerLock = lock && SUPPORT.pointerLock
|
---|
25 | if (SUPPORT.touch && touch) return 'touch'
|
---|
26 | if (this.pointerLock) return 'mouse'
|
---|
27 | if (SUPPORT.pointer && !mouse) return 'pointer'
|
---|
28 | if (SUPPORT.touch) return 'touch'
|
---|
29 | return 'mouse'
|
---|
30 | },
|
---|
31 | preventScrollAxis(this: InternalDragOptions, value: 'x' | 'y' | 'xy', _k: string, { preventScroll }: DragConfig) {
|
---|
32 | this.preventScrollDelay =
|
---|
33 | typeof preventScroll === 'number'
|
---|
34 | ? preventScroll
|
---|
35 | : preventScroll || (preventScroll === undefined && value)
|
---|
36 | ? DEFAULT_PREVENT_SCROLL_DELAY
|
---|
37 | : undefined
|
---|
38 | if (!SUPPORT.touchscreen || preventScroll === false) return undefined
|
---|
39 | return value ? value : preventScroll !== undefined ? 'y' : undefined
|
---|
40 | },
|
---|
41 | pointerCapture(
|
---|
42 | this: InternalDragOptions,
|
---|
43 | _v: any,
|
---|
44 | _k: string,
|
---|
45 | { pointer: { capture = true, buttons = 1, keys = true } = {} }
|
---|
46 | ) {
|
---|
47 | this.pointerButtons = buttons
|
---|
48 | this.keys = keys
|
---|
49 | return !this.pointerLock && this.device === 'pointer' && capture
|
---|
50 | },
|
---|
51 | threshold(
|
---|
52 | this: InternalDragOptions,
|
---|
53 | value: number | Vector2,
|
---|
54 | _k: string,
|
---|
55 | { filterTaps = false, tapsThreshold = 3, axis = undefined }
|
---|
56 | ) {
|
---|
57 | // TODO add warning when value is 0 and filterTaps or axis is set
|
---|
58 | const threshold = V.toVector(value, filterTaps ? tapsThreshold : axis ? 1 : 0)
|
---|
59 | this.filterTaps = filterTaps
|
---|
60 | this.tapsThreshold = tapsThreshold
|
---|
61 | return threshold
|
---|
62 | },
|
---|
63 | swipe(
|
---|
64 | this: InternalDragOptions,
|
---|
65 | { velocity = DEFAULT_SWIPE_VELOCITY, distance = DEFAULT_SWIPE_DISTANCE, duration = DEFAULT_SWIPE_DURATION } = {}
|
---|
66 | ) {
|
---|
67 | return {
|
---|
68 | velocity: this.transform(V.toVector(velocity)),
|
---|
69 | distance: this.transform(V.toVector(distance)),
|
---|
70 | duration
|
---|
71 | }
|
---|
72 | },
|
---|
73 | delay(value: number | boolean = 0) {
|
---|
74 | switch (value) {
|
---|
75 | case true:
|
---|
76 | return DEFAULT_DRAG_DELAY
|
---|
77 | case false:
|
---|
78 | return 0
|
---|
79 | default:
|
---|
80 | return value
|
---|
81 | }
|
---|
82 | },
|
---|
83 | axisThreshold(value: Record<PointerType, number>) {
|
---|
84 | if (!value) return DEFAULT_DRAG_AXIS_THRESHOLD
|
---|
85 | return { ...DEFAULT_DRAG_AXIS_THRESHOLD, ...value }
|
---|
86 | },
|
---|
87 | keyboardDisplacement(value: number = DEFAULT_KEYBOARD_DISPLACEMENT) {
|
---|
88 | return value
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (process.env.NODE_ENV === 'development') {
|
---|
93 | Object.assign(dragConfigResolver, {
|
---|
94 | useTouch(value: any) {
|
---|
95 | if (value !== undefined) {
|
---|
96 | throw Error(
|
---|
97 | `[@use-gesture]: \`useTouch\` option has been renamed to \`pointer.touch\`. Use it as in \`{ pointer: { touch: true } }\`.`
|
---|
98 | )
|
---|
99 | }
|
---|
100 | return NaN
|
---|
101 | },
|
---|
102 | experimental_preventWindowScrollY(value: any) {
|
---|
103 | if (value !== undefined) {
|
---|
104 | throw Error(
|
---|
105 | `[@use-gesture]: \`experimental_preventWindowScrollY\` option has been renamed to \`preventScroll\`.`
|
---|
106 | )
|
---|
107 | }
|
---|
108 | return NaN
|
---|
109 | },
|
---|
110 | swipeVelocity(value: any) {
|
---|
111 | if (value !== undefined) {
|
---|
112 | throw Error(
|
---|
113 | `[@use-gesture]: \`swipeVelocity\` option has been renamed to \`swipe.velocity\`. Use it as in \`{ swipe: { velocity: 0.5 } }\`.`
|
---|
114 | )
|
---|
115 | }
|
---|
116 | return NaN
|
---|
117 | },
|
---|
118 | swipeDistance(value: any) {
|
---|
119 | if (value !== undefined) {
|
---|
120 | throw Error(
|
---|
121 | `[@use-gesture]: \`swipeDistance\` option has been renamed to \`swipe.distance\`. Use it as in \`{ swipe: { distance: 50 } }\`.`
|
---|
122 | )
|
---|
123 | }
|
---|
124 | return NaN
|
---|
125 | },
|
---|
126 | swipeDuration(value: any) {
|
---|
127 | if (value !== undefined) {
|
---|
128 | throw Error(
|
---|
129 | `[@use-gesture]: \`swipeDuration\` option has been renamed to \`swipe.duration\`. Use it as in \`{ swipe: { duration: 250 } }\`.`
|
---|
130 | )
|
---|
131 | }
|
---|
132 | return NaN
|
---|
133 | }
|
---|
134 | })
|
---|
135 | }
|
---|