@tamagui/react-native-use-pressable 2.7.7 → 3.0.0-beta.637.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,34 @@
1
1
  function _class_call_check(instance, Constructor) {
2
- if (!(instance instanceof Constructor)) {
3
- throw new TypeError("Cannot call a class as a function");
4
- }
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
5
  }
6
6
  function _defineProperties(target, props) {
7
- for (var i = 0; i < props.length; i++) {
8
- var descriptor = props[i];
9
- descriptor.enumerable = descriptor.enumerable || false;
10
- descriptor.configurable = true;
11
- if ("value" in descriptor) descriptor.writable = true;
12
- Object.defineProperty(target, descriptor.key, descriptor);
13
- }
7
+ for (var i = 0; i < props.length; i++) {
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
13
+ }
14
14
  }
15
15
  function _create_class(Constructor, protoProps, staticProps) {
16
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
- if (staticProps) _defineProperties(Constructor, staticProps);
18
- return Constructor;
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
19
  }
20
20
  function _define_property(obj, key, value) {
21
- if (key in obj) {
22
- Object.defineProperty(obj, key, {
23
- value,
24
- enumerable: true,
25
- configurable: true,
26
- writable: true
27
- });
28
- } else {
29
- obj[key] = value;
30
- }
31
- return obj;
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
32
  }
33
33
  var DELAY = "DELAY";
34
34
  var ERROR = "ERROR";
@@ -41,421 +41,424 @@ var RESPONDER_GRANT = "RESPONDER_GRANT";
41
41
  var RESPONDER_RELEASE = "RESPONDER_RELEASE";
42
42
  var RESPONDER_TERMINATED = "RESPONDER_TERMINATED";
43
43
  var Transitions = Object.freeze({
44
- NOT_RESPONDER: {
45
- DELAY: ERROR,
46
- RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
47
- RESPONDER_RELEASE: ERROR,
48
- RESPONDER_TERMINATED: ERROR,
49
- LONG_PRESS_DETECTED: ERROR
50
- },
51
- RESPONDER_INACTIVE_PRESS_START: {
52
- DELAY: RESPONDER_ACTIVE_PRESS_START,
53
- RESPONDER_GRANT: ERROR,
54
- RESPONDER_RELEASE: NOT_RESPONDER,
55
- RESPONDER_TERMINATED: NOT_RESPONDER,
56
- LONG_PRESS_DETECTED: ERROR
57
- },
58
- RESPONDER_ACTIVE_PRESS_START: {
59
- DELAY: ERROR,
60
- RESPONDER_GRANT: ERROR,
61
- RESPONDER_RELEASE: NOT_RESPONDER,
62
- RESPONDER_TERMINATED: NOT_RESPONDER,
63
- LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
64
- },
65
- RESPONDER_ACTIVE_LONG_PRESS_START: {
66
- DELAY: ERROR,
67
- RESPONDER_GRANT: ERROR,
68
- RESPONDER_RELEASE: NOT_RESPONDER,
69
- RESPONDER_TERMINATED: NOT_RESPONDER,
70
- LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
71
- },
72
- ERROR: {
73
- DELAY: NOT_RESPONDER,
74
- RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
75
- RESPONDER_RELEASE: NOT_RESPONDER,
76
- RESPONDER_TERMINATED: NOT_RESPONDER,
77
- LONG_PRESS_DETECTED: NOT_RESPONDER
78
- }
44
+ NOT_RESPONDER: {
45
+ DELAY: ERROR,
46
+ RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
47
+ RESPONDER_RELEASE: ERROR,
48
+ RESPONDER_TERMINATED: ERROR,
49
+ LONG_PRESS_DETECTED: ERROR
50
+ },
51
+ RESPONDER_INACTIVE_PRESS_START: {
52
+ DELAY: RESPONDER_ACTIVE_PRESS_START,
53
+ RESPONDER_GRANT: ERROR,
54
+ RESPONDER_RELEASE: NOT_RESPONDER,
55
+ RESPONDER_TERMINATED: NOT_RESPONDER,
56
+ LONG_PRESS_DETECTED: ERROR
57
+ },
58
+ RESPONDER_ACTIVE_PRESS_START: {
59
+ DELAY: ERROR,
60
+ RESPONDER_GRANT: ERROR,
61
+ RESPONDER_RELEASE: NOT_RESPONDER,
62
+ RESPONDER_TERMINATED: NOT_RESPONDER,
63
+ LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
64
+ },
65
+ RESPONDER_ACTIVE_LONG_PRESS_START: {
66
+ DELAY: ERROR,
67
+ RESPONDER_GRANT: ERROR,
68
+ RESPONDER_RELEASE: NOT_RESPONDER,
69
+ RESPONDER_TERMINATED: NOT_RESPONDER,
70
+ LONG_PRESS_DETECTED: RESPONDER_ACTIVE_LONG_PRESS_START
71
+ },
72
+ ERROR: {
73
+ DELAY: NOT_RESPONDER,
74
+ RESPONDER_GRANT: RESPONDER_INACTIVE_PRESS_START,
75
+ RESPONDER_RELEASE: NOT_RESPONDER,
76
+ RESPONDER_TERMINATED: NOT_RESPONDER,
77
+ LONG_PRESS_DETECTED: NOT_RESPONDER
78
+ }
79
79
  });
80
- var isActiveSignal = function (signal) {
81
- return signal === RESPONDER_ACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_LONG_PRESS_START;
80
+ var isActiveSignal = function(signal) {
81
+ return signal === RESPONDER_ACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_LONG_PRESS_START;
82
82
  };
83
- var isButtonRole = function (element) {
84
- return element.getAttribute("role") === "button";
83
+ var isButtonRole = function(element) {
84
+ return element.getAttribute("role") === "button";
85
85
  };
86
- var isPressStartSignal = function (signal) {
87
- return signal === RESPONDER_INACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_LONG_PRESS_START;
86
+ var isPressStartSignal = function(signal) {
87
+ return signal === RESPONDER_INACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_PRESS_START || signal === RESPONDER_ACTIVE_LONG_PRESS_START;
88
88
  };
89
- var isTerminalSignal = function (signal) {
90
- return signal === RESPONDER_TERMINATED || signal === RESPONDER_RELEASE;
89
+ var isTerminalSignal = function(signal) {
90
+ return signal === RESPONDER_TERMINATED || signal === RESPONDER_RELEASE;
91
91
  };
92
- var isValidKeyPress = function (event) {
93
- var key = event.key;
94
- var target = event.target;
95
- var role = target.getAttribute("role");
96
- var isSpacebar = key === " " || key === "Spacebar";
97
- return key === "Enter" || isSpacebar && role === "button";
92
+ var isValidKeyPress = function(event) {
93
+ var key = event.key;
94
+ var target = event.target;
95
+ var role = target.getAttribute("role");
96
+ var isSpacebar = key === " " || key === "Spacebar";
97
+ return key === "Enter" || isSpacebar && role === "button";
98
98
  };
99
99
  var DEFAULT_LONG_PRESS_DELAY_MS = 450;
100
100
  var DEFAULT_PRESS_DELAY_MS = 50;
101
- var PressResponder = /* @__PURE__ */function () {
102
- "use strict";
103
-
104
- function PressResponder2(config) {
105
- _class_call_check(this, PressResponder2);
106
- _define_property(this, "_touchActivatePosition", null);
107
- _define_property(this, "_pressDelayTimeout", 0);
108
- _define_property(this, "_selectionTerminated", false);
109
- _define_property(this, "_isPointerTouch", false);
110
- _define_property(this, "_longPressDelayTimeout", 0);
111
- _define_property(this, "_longPressDispatched", false);
112
- _define_property(this, "_pressOutDelayTimeout", 0);
113
- _define_property(this, "_touchState", NOT_RESPONDER);
114
- _define_property(this, "_config", null);
115
- _define_property(this, "_eventHandlers", null);
116
- this.configure(config);
117
- }
118
- _create_class(PressResponder2, [{
119
- key: "configure",
120
- value: function configure(config) {
121
- this._config = config;
122
- }
123
- }, {
124
- /**
125
- * Resets any pending timers. This should be called on unmount.
126
- */
127
- key: "reset",
128
- value: function reset() {
129
- this._cancelLongPressDelayTimeout();
130
- this._cancelPressDelayTimeout();
131
- this._cancelPressOutDelayTimeout();
132
- }
133
- }, {
134
- /**
135
- * Returns a set of props to spread into the interactive element.
136
- */
137
- key: "getEventHandlers",
138
- value: function getEventHandlers() {
139
- if (this._eventHandlers == null) {
140
- this._eventHandlers = this._createEventHandlers();
141
- }
142
- return this._eventHandlers;
143
- }
144
- }, {
145
- key: "_createEventHandlers",
146
- value: function _createEventHandlers() {
147
- var _this = this;
148
- var start = function (event, shouldDelay) {
149
- event.persist();
150
- _this._cancelPressOutDelayTimeout();
151
- _this._longPressDispatched = false;
152
- _this._selectionTerminated = false;
153
- _this._touchState = NOT_RESPONDER;
154
- _this._isPointerTouch = event.nativeEvent.type === "touchstart";
155
- _this._receiveSignal(RESPONDER_GRANT, event);
156
- var delayPressStart = normalizeDelay(_this._config.delayPressStart, 0, DEFAULT_PRESS_DELAY_MS);
157
- if (shouldDelay !== false && delayPressStart > 0) {
158
- _this._pressDelayTimeout = setTimeout(function () {
159
- _this._receiveSignal(DELAY, event);
160
- }, delayPressStart);
161
- } else {
162
- _this._receiveSignal(DELAY, event);
163
- }
164
- var delayLongPress = normalizeDelay(_this._config.delayLongPress, 10, DEFAULT_LONG_PRESS_DELAY_MS);
165
- _this._longPressDelayTimeout = setTimeout(function () {
166
- _this._handleLongPress(event);
167
- }, delayLongPress + delayPressStart);
168
- };
169
- var end = function (event) {
170
- _this._receiveSignal(RESPONDER_RELEASE, event);
171
- };
172
- var keyupHandler = function (event) {
173
- var onPress = _this._config.onPress;
174
- var target = event.target;
175
- if (_this._touchState !== NOT_RESPONDER && isValidKeyPress(event)) {
176
- end(event);
177
- document.removeEventListener("keyup", keyupHandler);
178
- var role = target.getAttribute("role");
179
- var elementType = target.tagName.toLowerCase();
180
- var isNativeInteractiveElement = role === "link" || elementType === "a" || elementType === "button" || elementType === "input" || elementType === "select" || elementType === "textarea";
181
- if (onPress != null && !isNativeInteractiveElement) {
182
- onPress(event);
183
- }
184
- }
185
- };
186
- return {
187
- onStartShouldSetResponder: function (event) {
188
- var disabled = _this._config.disabled;
189
- if (disabled && isButtonRole(event.currentTarget)) {
190
- event.stopPropagation();
191
- }
192
- if (disabled == null) {
193
- return true;
194
- }
195
- return !disabled;
196
- },
197
- onKeyDown: function (event) {
198
- var disabled = _this._config.disabled;
199
- var key = event.key;
200
- var target = event.target;
201
- if (!disabled && isValidKeyPress(event)) {
202
- if (_this._touchState === NOT_RESPONDER) {
203
- start(event, false);
204
- document.addEventListener("keyup", keyupHandler);
205
- }
206
- var role = target.getAttribute("role");
207
- var isSpacebarKey = key === " " || key === "Spacebar";
208
- var _isButtonRole = role === "button" || role === "menuitem";
209
- if (isSpacebarKey && _isButtonRole) {
210
- event.preventDefault();
211
- }
212
- event.stopPropagation();
213
- }
214
- },
215
- onResponderGrant: function (event) {
216
- return start(event);
217
- },
218
- onResponderMove: function (event) {
219
- if (_this._config.onPressMove != null) {
220
- _this._config.onPressMove(event);
221
- }
222
- var touch = getTouchFromResponderEvent(event);
223
- if (_this._touchActivatePosition != null) {
224
- var deltaX = _this._touchActivatePosition.pageX - touch.pageX;
225
- var deltaY = _this._touchActivatePosition.pageY - touch.pageY;
226
- if (Math.hypot(deltaX, deltaY) > 10) {
227
- _this._cancelLongPressDelayTimeout();
228
- }
229
- }
230
- },
231
- onResponderRelease: function (event) {
232
- return end(event);
233
- },
234
- onResponderTerminate: function (event) {
235
- if (event.nativeEvent.type === "selectionchange") {
236
- _this._selectionTerminated = true;
237
- }
238
- _this._receiveSignal(RESPONDER_TERMINATED, event);
239
- },
240
- onResponderTerminationRequest: function (event) {
241
- var _this$_config = _this._config;
242
- var cancelable = _this$_config.cancelable;
243
- var disabled = _this$_config.disabled;
244
- var onLongPress = _this$_config.onLongPress;
245
- if (!disabled && onLongPress != null && _this._isPointerTouch && event.nativeEvent.type === "contextmenu") {
246
- return false;
247
- }
248
- if (cancelable == null) {
249
- return true;
250
- }
251
- return cancelable;
252
- },
253
- // NOTE: this diverges from react-native in 3 significant ways:
254
- // * The `onPress` callback is not connected to the responder system (the native
255
- // `click` event must be used but is dispatched in many scenarios where no pointers
256
- // are on the screen.) Therefore, it's possible for `onPress` to be called without
257
- // `onPress{Start,End}` being called first.
258
- // * The `onPress` callback is only be called on the first ancestor of the native
259
- // `click` target that is using the PressResponder.
260
- // * The event's `nativeEvent` is a `MouseEvent` not a `TouchEvent`.
261
- onClick: function (event) {
262
- var _this$_config2 = _this._config;
263
- var disabled = _this$_config2.disabled;
264
- var onPress = _this$_config2.onPress;
265
- if (!disabled) {
266
- event.stopPropagation();
267
- if (_this._longPressDispatched || _this._selectionTerminated) {
268
- event.preventDefault();
269
- } else if (onPress != null && event.altKey === false) {
270
- onPress(event);
271
- }
272
- } else {
273
- if (isButtonRole(event.currentTarget)) {
274
- event.stopPropagation();
275
- }
276
- }
277
- },
278
- // If `onLongPress` is provided and a touch pointer is being used, prevent the
279
- // default context menu from opening.
280
- onContextMenu: function (event) {
281
- var _this$_config3 = _this._config;
282
- var disabled = _this$_config3.disabled;
283
- var onLongPress = _this$_config3.onLongPress;
284
- if (!disabled) {
285
- if (onLongPress != null && _this._isPointerTouch && !event.defaultPrevented) {
286
- event.preventDefault();
287
- event.stopPropagation();
288
- }
289
- } else {
290
- if (isButtonRole(event.currentTarget)) {
291
- event.stopPropagation();
292
- }
293
- }
294
- }
295
- };
296
- }
297
- }, {
298
- /**
299
- * Receives a state machine signal, performs side effects of the transition
300
- * and stores the new state. Validates the transition as well.
301
- */
302
- key: "_receiveSignal",
303
- value: function _receiveSignal(signal, event) {
304
- var prevState = this._touchState;
305
- var nextState = null;
306
- if (Transitions[prevState] != null) {
307
- nextState = Transitions[prevState][signal];
308
- }
309
- if (this._touchState === NOT_RESPONDER && signal === RESPONDER_RELEASE) {
310
- return;
311
- }
312
- if (nextState == null || nextState === ERROR) {
313
- console.error(`PressResponder: Invalid signal ${signal} for state ${prevState} on responder`);
314
- } else if (prevState !== nextState) {
315
- this._performTransitionSideEffects(prevState, nextState, signal, event);
316
- this._touchState = nextState;
317
- }
318
- }
319
- }, {
320
- /**
321
- * Performs a transition between touchable states and identify any activations
322
- * or deactivations (and callback invocations).
323
- */
324
- key: "_performTransitionSideEffects",
325
- value: function _performTransitionSideEffects(prevState, nextState, signal, event) {
326
- var _this = this;
327
- if (isTerminalSignal(signal)) {
328
- setTimeout(function () {
329
- _this._isPointerTouch = false;
330
- }, 0);
331
- this._touchActivatePosition = null;
332
- this._cancelLongPressDelayTimeout();
333
- }
334
- if (isPressStartSignal(prevState) && signal === LONG_PRESS_DETECTED) {
335
- var onLongPress = this._config.onLongPress;
336
- if (onLongPress != null && event.nativeEvent.key == null) {
337
- onLongPress(event);
338
- this._longPressDispatched = true;
339
- }
340
- }
341
- var isPrevActive = isActiveSignal(prevState);
342
- var isNextActive = isActiveSignal(nextState);
343
- if (!isPrevActive && isNextActive) {
344
- this._activate(event);
345
- } else if (isPrevActive && !isNextActive) {
346
- this._deactivate(event);
347
- }
348
- if (isPressStartSignal(prevState) && signal === RESPONDER_RELEASE) {
349
- var _this$_config4 = this._config;
350
- var _onLongPress = _this$_config4.onLongPress;
351
- var onPress = _this$_config4.onPress;
352
- if (onPress != null) {
353
- var isPressCanceledByLongPress = _onLongPress != null && prevState === RESPONDER_ACTIVE_LONG_PRESS_START;
354
- if (!isPressCanceledByLongPress) {
355
- if (!(isNextActive || isPrevActive)) {
356
- this._activate(event);
357
- this._deactivate(event);
358
- }
359
- }
360
- }
361
- }
362
- this._cancelPressDelayTimeout();
363
- }
364
- }, {
365
- key: "_activate",
366
- value: function _activate(event) {
367
- var _this$_config5 = this._config;
368
- var onPressChange = _this$_config5.onPressChange;
369
- var onPressStart = _this$_config5.onPressStart;
370
- var touch = getTouchFromResponderEvent(event);
371
- this._touchActivatePosition = {
372
- pageX: touch.pageX,
373
- pageY: touch.pageY
374
- };
375
- if (onPressStart != null) {
376
- onPressStart(event);
377
- }
378
- if (onPressChange != null) {
379
- onPressChange(true);
380
- }
381
- }
382
- }, {
383
- key: "_deactivate",
384
- value: function _deactivate(event) {
385
- var _this$_config6 = this._config;
386
- var onPressChange = _this$_config6.onPressChange;
387
- var onPressEnd = _this$_config6.onPressEnd;
388
- function end() {
389
- if (onPressEnd != null) {
390
- onPressEnd(event);
391
- }
392
- if (onPressChange != null) {
393
- onPressChange(false);
394
- }
395
- }
396
- var delayPressEnd = normalizeDelay(this._config.delayPressEnd);
397
- if (delayPressEnd > 0) {
398
- this._pressOutDelayTimeout = setTimeout(function () {
399
- end();
400
- }, delayPressEnd);
401
- } else {
402
- end();
403
- }
404
- }
405
- }, {
406
- key: "_handleLongPress",
407
- value: function _handleLongPress(event) {
408
- if (this._touchState === RESPONDER_ACTIVE_PRESS_START || this._touchState === RESPONDER_ACTIVE_LONG_PRESS_START) {
409
- this._receiveSignal(LONG_PRESS_DETECTED, event);
410
- }
411
- }
412
- }, {
413
- key: "_cancelLongPressDelayTimeout",
414
- value: function _cancelLongPressDelayTimeout() {
415
- if (this._longPressDelayTimeout != null) {
416
- clearTimeout(this._longPressDelayTimeout);
417
- this._longPressDelayTimeout = null;
418
- }
419
- }
420
- }, {
421
- key: "_cancelPressDelayTimeout",
422
- value: function _cancelPressDelayTimeout() {
423
- if (this._pressDelayTimeout != null) {
424
- clearTimeout(this._pressDelayTimeout);
425
- this._pressDelayTimeout = null;
426
- }
427
- }
428
- }, {
429
- key: "_cancelPressOutDelayTimeout",
430
- value: function _cancelPressOutDelayTimeout() {
431
- if (this._pressOutDelayTimeout != null) {
432
- clearTimeout(this._pressOutDelayTimeout);
433
- this._pressOutDelayTimeout = null;
434
- }
435
- }
436
- }]);
437
- return PressResponder2;
438
- }();
101
+ var PressResponder = /* @__PURE__ */ (function() {
102
+ "use strict";
103
+ function PressResponder2(config) {
104
+ _class_call_check(this, PressResponder2);
105
+ _define_property(this, "_touchActivatePosition", null);
106
+ _define_property(this, "_pressDelayTimeout", 0);
107
+ _define_property(this, "_selectionTerminated", false);
108
+ _define_property(this, "_isPointerTouch", false);
109
+ _define_property(this, "_longPressDelayTimeout", 0);
110
+ _define_property(this, "_longPressDispatched", false);
111
+ _define_property(this, "_pressOutDelayTimeout", 0);
112
+ _define_property(this, "_touchState", NOT_RESPONDER);
113
+ _define_property(this, "_config", null);
114
+ _define_property(this, "_eventHandlers", null);
115
+ this.configure(config);
116
+ }
117
+ _create_class(PressResponder2, [
118
+ {
119
+ key: "configure",
120
+ value: function configure(config) {
121
+ this._config = config;
122
+ }
123
+ },
124
+ {
125
+ /**
126
+ * Resets any pending timers. This should be called on unmount.
127
+ */
128
+ key: "reset",
129
+ value: function reset() {
130
+ this._cancelLongPressDelayTimeout();
131
+ this._cancelPressDelayTimeout();
132
+ this._cancelPressOutDelayTimeout();
133
+ }
134
+ },
135
+ {
136
+ /**
137
+ * Returns a set of props to spread into the interactive element.
138
+ */
139
+ key: "getEventHandlers",
140
+ value: function getEventHandlers() {
141
+ if (this._eventHandlers == null) {
142
+ this._eventHandlers = this._createEventHandlers();
143
+ }
144
+ return this._eventHandlers;
145
+ }
146
+ },
147
+ {
148
+ key: "_createEventHandlers",
149
+ value: function _createEventHandlers() {
150
+ var _this = this;
151
+ var start = function(event, shouldDelay) {
152
+ event.persist();
153
+ _this._cancelPressOutDelayTimeout();
154
+ _this._longPressDispatched = false;
155
+ _this._selectionTerminated = false;
156
+ _this._touchState = NOT_RESPONDER;
157
+ _this._isPointerTouch = event.nativeEvent.type === "touchstart";
158
+ _this._receiveSignal(RESPONDER_GRANT, event);
159
+ var delayPressStart = normalizeDelay(_this._config.delayPressStart, 0, DEFAULT_PRESS_DELAY_MS);
160
+ if (shouldDelay !== false && delayPressStart > 0) {
161
+ _this._pressDelayTimeout = setTimeout(function() {
162
+ _this._receiveSignal(DELAY, event);
163
+ }, delayPressStart);
164
+ } else {
165
+ _this._receiveSignal(DELAY, event);
166
+ }
167
+ var delayLongPress = normalizeDelay(_this._config.delayLongPress, 10, DEFAULT_LONG_PRESS_DELAY_MS);
168
+ _this._longPressDelayTimeout = setTimeout(function() {
169
+ _this._handleLongPress(event);
170
+ }, delayLongPress + delayPressStart);
171
+ };
172
+ var end = function(event) {
173
+ _this._receiveSignal(RESPONDER_RELEASE, event);
174
+ };
175
+ var keyupHandler = function(event) {
176
+ var onPress = _this._config.onPress;
177
+ var target = event.target;
178
+ if (_this._touchState !== NOT_RESPONDER && isValidKeyPress(event)) {
179
+ end(event);
180
+ document.removeEventListener("keyup", keyupHandler);
181
+ var role = target.getAttribute("role");
182
+ var elementType = target.tagName.toLowerCase();
183
+ var isNativeInteractiveElement = role === "link" || elementType === "a" || elementType === "button" || elementType === "input" || elementType === "select" || elementType === "textarea";
184
+ if (onPress != null && !isNativeInteractiveElement) {
185
+ onPress(event);
186
+ }
187
+ }
188
+ };
189
+ return {
190
+ onStartShouldSetResponder: function(event) {
191
+ var disabled = _this._config.disabled;
192
+ if (disabled && isButtonRole(event.currentTarget)) {
193
+ event.stopPropagation();
194
+ }
195
+ if (disabled == null) {
196
+ return true;
197
+ }
198
+ return !disabled;
199
+ },
200
+ onKeyDown: function(event) {
201
+ var disabled = _this._config.disabled;
202
+ var key = event.key;
203
+ var target = event.target;
204
+ if (!disabled && isValidKeyPress(event)) {
205
+ if (_this._touchState === NOT_RESPONDER) {
206
+ start(event, false);
207
+ document.addEventListener("keyup", keyupHandler);
208
+ }
209
+ var role = target.getAttribute("role");
210
+ var isSpacebarKey = key === " " || key === "Spacebar";
211
+ var _isButtonRole = role === "button" || role === "menuitem";
212
+ if (isSpacebarKey && _isButtonRole) {
213
+ event.preventDefault();
214
+ }
215
+ event.stopPropagation();
216
+ }
217
+ },
218
+ onResponderGrant: function(event) {
219
+ return start(event);
220
+ },
221
+ onResponderMove: function(event) {
222
+ if (_this._config.onPressMove != null) {
223
+ _this._config.onPressMove(event);
224
+ }
225
+ var touch = getTouchFromResponderEvent(event);
226
+ if (_this._touchActivatePosition != null) {
227
+ var deltaX = _this._touchActivatePosition.pageX - touch.pageX;
228
+ var deltaY = _this._touchActivatePosition.pageY - touch.pageY;
229
+ if (Math.hypot(deltaX, deltaY) > 10) {
230
+ _this._cancelLongPressDelayTimeout();
231
+ }
232
+ }
233
+ },
234
+ onResponderRelease: function(event) {
235
+ return end(event);
236
+ },
237
+ onResponderTerminate: function(event) {
238
+ if (event.nativeEvent.type === "selectionchange") {
239
+ _this._selectionTerminated = true;
240
+ }
241
+ _this._receiveSignal(RESPONDER_TERMINATED, event);
242
+ },
243
+ onResponderTerminationRequest: function(event) {
244
+ var _this$_config = _this._config;
245
+ var cancelable = _this$_config.cancelable;
246
+ var disabled = _this$_config.disabled;
247
+ var onLongPress = _this$_config.onLongPress;
248
+ if (!disabled && onLongPress != null && _this._isPointerTouch && event.nativeEvent.type === "contextmenu") {
249
+ return false;
250
+ }
251
+ if (cancelable == null) {
252
+ return true;
253
+ }
254
+ return cancelable;
255
+ },
256
+ onClick: function(event) {
257
+ var _this$_config2 = _this._config;
258
+ var disabled = _this$_config2.disabled;
259
+ var onPress = _this$_config2.onPress;
260
+ if (!disabled) {
261
+ event.stopPropagation();
262
+ if (_this._longPressDispatched || _this._selectionTerminated) {
263
+ event.preventDefault();
264
+ } else if (onPress != null && event.altKey === false) {
265
+ onPress(event);
266
+ }
267
+ } else {
268
+ if (isButtonRole(event.currentTarget)) {
269
+ event.stopPropagation();
270
+ }
271
+ }
272
+ },
273
+ onContextMenu: function(event) {
274
+ var _this$_config3 = _this._config;
275
+ var disabled = _this$_config3.disabled;
276
+ var onLongPress = _this$_config3.onLongPress;
277
+ if (!disabled) {
278
+ if (onLongPress != null && _this._isPointerTouch && !event.defaultPrevented) {
279
+ event.preventDefault();
280
+ event.stopPropagation();
281
+ }
282
+ } else {
283
+ if (isButtonRole(event.currentTarget)) {
284
+ event.stopPropagation();
285
+ }
286
+ }
287
+ }
288
+ };
289
+ }
290
+ },
291
+ {
292
+ /**
293
+ * Receives a state machine signal, performs side effects of the transition
294
+ * and stores the new state. Validates the transition as well.
295
+ */
296
+ key: "_receiveSignal",
297
+ value: function _receiveSignal(signal, event) {
298
+ var prevState = this._touchState;
299
+ var nextState = null;
300
+ if (Transitions[prevState] != null) {
301
+ nextState = Transitions[prevState][signal];
302
+ }
303
+ if (this._touchState === NOT_RESPONDER && signal === RESPONDER_RELEASE) {
304
+ return;
305
+ }
306
+ if (nextState == null || nextState === ERROR) {
307
+ console.error(`PressResponder: Invalid signal ${signal} for state ${prevState} on responder`);
308
+ } else if (prevState !== nextState) {
309
+ this._performTransitionSideEffects(prevState, nextState, signal, event);
310
+ this._touchState = nextState;
311
+ }
312
+ }
313
+ },
314
+ {
315
+ /**
316
+ * Performs a transition between touchable states and identify any activations
317
+ * or deactivations (and callback invocations).
318
+ */
319
+ key: "_performTransitionSideEffects",
320
+ value: function _performTransitionSideEffects(prevState, nextState, signal, event) {
321
+ var _this = this;
322
+ if (isTerminalSignal(signal)) {
323
+ setTimeout(function() {
324
+ _this._isPointerTouch = false;
325
+ }, 0);
326
+ this._touchActivatePosition = null;
327
+ this._cancelLongPressDelayTimeout();
328
+ }
329
+ if (isPressStartSignal(prevState) && signal === LONG_PRESS_DETECTED) {
330
+ var onLongPress = this._config.onLongPress;
331
+ if (onLongPress != null && event.nativeEvent.key == null) {
332
+ onLongPress(event);
333
+ this._longPressDispatched = true;
334
+ }
335
+ }
336
+ var isPrevActive = isActiveSignal(prevState);
337
+ var isNextActive = isActiveSignal(nextState);
338
+ if (!isPrevActive && isNextActive) {
339
+ this._activate(event);
340
+ } else if (isPrevActive && !isNextActive) {
341
+ this._deactivate(event);
342
+ }
343
+ if (isPressStartSignal(prevState) && signal === RESPONDER_RELEASE) {
344
+ var _this$_config4 = this._config;
345
+ var _onLongPress = _this$_config4.onLongPress;
346
+ var onPress = _this$_config4.onPress;
347
+ if (onPress != null) {
348
+ var isPressCanceledByLongPress = _onLongPress != null && prevState === RESPONDER_ACTIVE_LONG_PRESS_START;
349
+ if (!isPressCanceledByLongPress) {
350
+ if (!(isNextActive || isPrevActive)) {
351
+ this._activate(event);
352
+ this._deactivate(event);
353
+ }
354
+ }
355
+ }
356
+ }
357
+ this._cancelPressDelayTimeout();
358
+ }
359
+ },
360
+ {
361
+ key: "_activate",
362
+ value: function _activate(event) {
363
+ var _this$_config5 = this._config;
364
+ var onPressChange = _this$_config5.onPressChange;
365
+ var onPressStart = _this$_config5.onPressStart;
366
+ var touch = getTouchFromResponderEvent(event);
367
+ this._touchActivatePosition = {
368
+ pageX: touch.pageX,
369
+ pageY: touch.pageY
370
+ };
371
+ if (onPressStart != null) {
372
+ onPressStart(event);
373
+ }
374
+ if (onPressChange != null) {
375
+ onPressChange(true);
376
+ }
377
+ }
378
+ },
379
+ {
380
+ key: "_deactivate",
381
+ value: function _deactivate(event) {
382
+ var _this$_config6 = this._config;
383
+ var onPressChange = _this$_config6.onPressChange;
384
+ var onPressEnd = _this$_config6.onPressEnd;
385
+ function end() {
386
+ if (onPressEnd != null) {
387
+ onPressEnd(event);
388
+ }
389
+ if (onPressChange != null) {
390
+ onPressChange(false);
391
+ }
392
+ }
393
+ var delayPressEnd = normalizeDelay(this._config.delayPressEnd);
394
+ if (delayPressEnd > 0) {
395
+ this._pressOutDelayTimeout = setTimeout(function() {
396
+ end();
397
+ }, delayPressEnd);
398
+ } else {
399
+ end();
400
+ }
401
+ }
402
+ },
403
+ {
404
+ key: "_handleLongPress",
405
+ value: function _handleLongPress(event) {
406
+ if (this._touchState === RESPONDER_ACTIVE_PRESS_START || this._touchState === RESPONDER_ACTIVE_LONG_PRESS_START) {
407
+ this._receiveSignal(LONG_PRESS_DETECTED, event);
408
+ }
409
+ }
410
+ },
411
+ {
412
+ key: "_cancelLongPressDelayTimeout",
413
+ value: function _cancelLongPressDelayTimeout() {
414
+ if (this._longPressDelayTimeout != null) {
415
+ clearTimeout(this._longPressDelayTimeout);
416
+ this._longPressDelayTimeout = null;
417
+ }
418
+ }
419
+ },
420
+ {
421
+ key: "_cancelPressDelayTimeout",
422
+ value: function _cancelPressDelayTimeout() {
423
+ if (this._pressDelayTimeout != null) {
424
+ clearTimeout(this._pressDelayTimeout);
425
+ this._pressDelayTimeout = null;
426
+ }
427
+ }
428
+ },
429
+ {
430
+ key: "_cancelPressOutDelayTimeout",
431
+ value: function _cancelPressOutDelayTimeout() {
432
+ if (this._pressOutDelayTimeout != null) {
433
+ clearTimeout(this._pressOutDelayTimeout);
434
+ this._pressOutDelayTimeout = null;
435
+ }
436
+ }
437
+ }
438
+ ]);
439
+ return PressResponder2;
440
+ })();
439
441
  function normalizeDelay(delay, min, fallback) {
440
- if (min === void 0) {
441
- min = 0;
442
- }
443
- if (fallback === void 0) {
444
- fallback = 0;
445
- }
446
- return Math.max(min, delay !== null && delay !== void 0 ? delay : fallback);
442
+ if (min === void 0) {
443
+ min = 0;
444
+ }
445
+ if (fallback === void 0) {
446
+ fallback = 0;
447
+ }
448
+ return Math.max(min, delay !== null && delay !== void 0 ? delay : fallback);
447
449
  }
448
450
  function getTouchFromResponderEvent(event) {
449
- var _event$nativeEvent = event.nativeEvent;
450
- var changedTouches = _event$nativeEvent.changedTouches;
451
- var touches = _event$nativeEvent.touches;
452
- if (touches != null && touches.length > 0) {
453
- return touches[0];
454
- }
455
- if (changedTouches != null && changedTouches.length > 0) {
456
- return changedTouches[0];
457
- }
458
- return event.nativeEvent;
451
+ var _event$nativeEvent = event.nativeEvent;
452
+ var changedTouches = _event$nativeEvent.changedTouches;
453
+ var touches = _event$nativeEvent.touches;
454
+ if (touches != null && touches.length > 0) {
455
+ return touches[0];
456
+ }
457
+ if (changedTouches != null && changedTouches.length > 0) {
458
+ return changedTouches[0];
459
+ }
460
+ return event.nativeEvent;
459
461
  }
462
+
460
463
  export { PressResponder };
461
464
  //# sourceMappingURL=PressResponder.native.js.map