1 | import { InternalGestureOptions } from '../types'
|
---|
2 | import { Vector2, State, GenericOptions } from '../types'
|
---|
3 | import { V } from '../utils/maths'
|
---|
4 |
|
---|
5 | export const identity = (v: Vector2) => v
|
---|
6 | export const DEFAULT_RUBBERBAND = 0.15
|
---|
7 |
|
---|
8 | export const commonConfigResolver = {
|
---|
9 | enabled(value = true) {
|
---|
10 | return value
|
---|
11 | },
|
---|
12 | eventOptions(value: AddEventListenerOptions | undefined, _k: string, config: { shared: GenericOptions }) {
|
---|
13 | return { ...config.shared.eventOptions, ...value }
|
---|
14 | },
|
---|
15 | preventDefault(value = false) {
|
---|
16 | return value
|
---|
17 | },
|
---|
18 | triggerAllEvents(value = false) {
|
---|
19 | return value
|
---|
20 | },
|
---|
21 | rubberband(value: number | boolean | Vector2 = 0): Vector2 {
|
---|
22 | switch (value) {
|
---|
23 | case true:
|
---|
24 | return [DEFAULT_RUBBERBAND, DEFAULT_RUBBERBAND]
|
---|
25 | case false:
|
---|
26 | return [0, 0]
|
---|
27 | default:
|
---|
28 | return V.toVector(value)
|
---|
29 | }
|
---|
30 | },
|
---|
31 | from(value: number | Vector2 | ((s: State) => Vector2)) {
|
---|
32 | if (typeof value === 'function') return value
|
---|
33 | // eslint-disable-next-line eqeqeq
|
---|
34 | if (value != null) return V.toVector(value)
|
---|
35 | },
|
---|
36 | transform(this: InternalGestureOptions, value: any, _k: string, config: { shared: GenericOptions }) {
|
---|
37 | const transform = value || config.shared.transform
|
---|
38 | this.hasCustomTransform = !!transform
|
---|
39 |
|
---|
40 | if (process.env.NODE_ENV === 'development') {
|
---|
41 | const originalTransform = transform || identity
|
---|
42 | return (v: Vector2) => {
|
---|
43 | const r = originalTransform(v)
|
---|
44 | if (!isFinite(r[0]) || !isFinite(r[1])) {
|
---|
45 | // eslint-disable-next-line no-console
|
---|
46 | console.warn(`[@use-gesture]: config.transform() must produce a valid result, but it was: [${r[0]},${[1]}]`)
|
---|
47 | }
|
---|
48 | return r
|
---|
49 | }
|
---|
50 | }
|
---|
51 | return transform || identity
|
---|
52 | },
|
---|
53 | threshold(value: any) {
|
---|
54 | return V.toVector(value, 0)
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (process.env.NODE_ENV === 'development') {
|
---|
59 | Object.assign(commonConfigResolver, {
|
---|
60 | domTarget(value: any) {
|
---|
61 | if (value !== undefined) {
|
---|
62 | throw Error(`[@use-gesture]: \`domTarget\` option has been renamed to \`target\`.`)
|
---|
63 | }
|
---|
64 | return NaN
|
---|
65 | },
|
---|
66 | lockDirection(value: any) {
|
---|
67 | if (value !== undefined) {
|
---|
68 | throw Error(
|
---|
69 | `[@use-gesture]: \`lockDirection\` option has been merged with \`axis\`. Use it as in \`{ axis: 'lock' }\``
|
---|
70 | )
|
---|
71 | }
|
---|
72 | return NaN
|
---|
73 | },
|
---|
74 | initial(value: any) {
|
---|
75 | if (value !== undefined) {
|
---|
76 | throw Error(`[@use-gesture]: \`initial\` option has been renamed to \`from\`.`)
|
---|
77 | }
|
---|
78 | return NaN
|
---|
79 | }
|
---|
80 | })
|
---|
81 | }
|
---|