@onekeyfe/react-native-bottom-tabs 6.8.0-3 → 6.8.0-4

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.
@@ -150,10 +150,19 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
150
150
  private var downProgress = 0f
151
151
  private var gestureReturnOpen = false
152
152
  private var eligible = false
153
+ // The touch started inside the open menu, where only a leftward drag moves it.
154
+ private var downInMenu = false
155
+ // The state a user's release or tap has already confirmed with a haptic,
156
+ // so the transition plays it once, when it is decided rather than landed.
157
+ private var hapticTarget: Boolean? = null
153
158
  private var touchedView: WeakReference<View>? = null
154
159
  private val touchMatrix = Matrix()
155
160
  private val touchPoint = FloatArray(2)
156
161
  private var dragging = false
162
+ // A native gesture start sent to React (by a drag, or by a rightward swipe
163
+ // in the menu that only cancels the row's touch), owed its end however the
164
+ // touch stops. React's pointer dispatcher drops input until it gets one.
165
+ private var nativeGestureStarted = false
157
166
  private var tracker: VelocityTracker? = null
158
167
  private var lastNativeEvent: MotionEvent? = null
159
168
  private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
@@ -205,6 +214,7 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
205
214
  stopAnimation()
206
215
  drawerEnabled = value
207
216
  stableOpen = false
217
+ hapticTarget = null
208
218
  setProgress(0f)
209
219
  if (value && requestedOpen) settle(true, 0f, false)
210
220
  }
@@ -287,8 +297,11 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
287
297
  touchedView = touchedViewAt(this, event.x, event.y)?.let(::WeakReference)
288
298
  val systemLeft = ViewCompat.getRootWindowInsets(this)
289
299
  ?.getInsets(WindowInsetsCompat.Type.systemGestures())?.left ?: 0
300
+ downInMenu = progress > 0f && event.x < menuWidth * progress
301
+ // While open, the page beside the menu and the menu itself both take
302
+ // the drag; controls inside the menu keep theirs.
290
303
  eligible = drawerEnabled && event.y < height - bottomBarHeight * density &&
291
- (if (progress > 0f) event.x >= menuWidth * progress
304
+ (if (progress > 0f) !downInMenu || !touchPathHasExclusiveControl()
292
305
  else swipeEnabled && event.x >= systemLeft && !touchPathHasExclusiveControl())
293
306
  if (eligible) tracker = VelocityTracker.obtain().also { it.addMovement(event) }
294
307
  }
@@ -313,14 +326,20 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
313
326
  eligible = false
314
327
  return false
315
328
  }
329
+ // Inside the open menu only a leftward drag moves it, which closes it.
330
+ // A rightward one is still a swipe, not a tap on the row under it.
331
+ if (abs(dx) > touchSlop && abs(dx) > abs(dy) && downInMenu && dx > 0f) {
332
+ eligible = false
333
+ startNativeGesture(event)
334
+ return false
335
+ }
316
336
  if (abs(dx) > touchSlop && abs(dx) > abs(dy) && (progress > 0f || dx > 0f)) {
317
337
  gestureReturnOpen = if (animator != null && !animationUser) animationTarget else stableOpen
318
338
  stopAnimation()
319
339
  downProgress = progress
320
340
  dragging = true
321
341
  parent?.requestDisallowInterceptTouchEvent(true)
322
- NativeGestureUtil.notifyNativeGestureStarted(this, event)
323
- rememberNativeEvent(event)
342
+ startNativeGesture(event)
324
343
  return true
325
344
  }
326
345
  }
@@ -433,11 +452,18 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
433
452
  lastNativeEvent = MotionEvent.obtain(event)
434
453
  }
435
454
 
455
+ private fun startNativeGesture(event: MotionEvent) {
456
+ NativeGestureUtil.notifyNativeGestureStarted(this, event)
457
+ nativeGestureStarted = true
458
+ rememberNativeEvent(event)
459
+ }
460
+
436
461
  private fun cancelTouch() {
437
- if (dragging) {
462
+ if (nativeGestureStarted) {
438
463
  lastNativeEvent?.let { NativeGestureUtil.notifyNativeGestureEnded(this, it) }
439
- parent?.requestDisallowInterceptTouchEvent(false)
440
464
  }
465
+ if (dragging) parent?.requestDisallowInterceptTouchEvent(false)
466
+ nativeGestureStarted = false
441
467
  dragging = false
442
468
  eligible = false
443
469
  touchedView = null
@@ -572,6 +598,12 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
572
598
  // keeps its finger velocity through a slightly underdamped spring.
573
599
  private fun settle(open: Boolean, velocity: Float, user: Boolean, tween: Boolean = true) {
574
600
  val target = drawerEnabled && open
601
+ // The haptic marks the decision: it plays as a user's release or tap
602
+ // commits to the other state, not when the drawer lands there.
603
+ if (user && target != stableOpen && hapticTarget != target) {
604
+ hapticTarget = target
605
+ if (drawerHapticFeedbackEnabled) performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
606
+ }
575
607
  // An animation already heading there keeps running; the newer request
576
608
  // still decides whether its completion is a user event.
577
609
  if (animator != null && animationTarget == target) {
@@ -586,6 +618,7 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
586
618
  stopAnimation()
587
619
  if (width == 0 || !isAttachedToWindow) {
588
620
  stableOpen = target
621
+ hapticTarget = null
589
622
  setProgress(if (target) 1f else 0f)
590
623
  return
591
624
  }
@@ -650,9 +683,9 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
650
683
  private fun finish(target: Boolean, user: Boolean) {
651
684
  val changed = stableOpen != target
652
685
  stableOpen = target
686
+ hapticTarget = null
653
687
  setProgress(if (target) 1f else 0f)
654
688
  if (user && (changed || target != requestedOpen)) {
655
- if (drawerHapticFeedbackEnabled && changed) performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
656
689
  onDrawerOpenChange?.invoke(target)
657
690
  }
658
691
  }
@@ -711,6 +744,7 @@ class BottomTabsDrawerView(context: Context) : FrameLayout(context) {
711
744
  detached = true
712
745
  cancelTouch()
713
746
  stopAnimation()
747
+ hapticTarget = null
714
748
  backCallback.remove()
715
749
  backRegistered = false
716
750
  setProgress(if (stableOpen && drawerEnabled) 1f else 0f)
package/docs/SPEC.md CHANGED
@@ -87,8 +87,11 @@ initial layout and cancellation emit no event. A user transition returning to
87
87
  the previous stable state emits only when it overrides an in-flight external
88
88
  target, so the consumer can reconcile that target. `drawerSwipeEnabled?: boolean`
89
89
  defaults to false; callers MUST enable it only where drawer navigation is appropriate (normally a root
90
- screen). Existing `hapticFeedbackEnabled` enables one light native haptic after
91
- a completed user transition changes the stable state. Programmatic updates and
90
+ screen). Existing `hapticFeedbackEnabled` enables one light native haptic when a
91
+ user's release, dim tap or Back/escape commits to the other stable state: it
92
+ plays at that decision, not when the drawer lands, and once per transition, so
93
+ a drag that takes over and releases toward the same target does not repeat it.
94
+ Releases that return to the current stable state, programmatic updates and
92
95
  cancelled gestures do not vibrate.
93
96
 
94
97
  `onDrawerProgress?: (event) => void` receives `{ progress }`, the displayed
@@ -179,9 +182,17 @@ pending direction even when a nested pager initially disallows interception,
179
182
  but stops overriding that request after the child wins. Sliders and editing controls,
180
183
  web/graphics surfaces, system back gestures and starts in the bottom tab-bar
181
184
  footprint take priority. When open, closing gestures
182
- remain enabled even if opening swipes are disabled. Only the exposed
183
- foreground accepts a closing pan; menu scrolling remains the consumer's own.
184
- Vertical gestures fail before taking ownership. Active touches interrupted by
185
+ remain enabled even if opening swipes are disabled. The exposed foreground
186
+ accepts a closing pan in either direction; inside the menu a leftward
187
+ horizontal pan closes it too, while sliders and editing controls keep their
188
+ touches and vertical menu scrolling remains the consumer's own. Inside the
189
+ menu the direction is read from the distance moved as well as the current
190
+ speed, and a horizontal swipe in either direction cancels React touches under
191
+ it, so the row under the finger never fires its press. Whenever a drawer pan
192
+ takes ownership it cancels React touches the same way (iOS cycles the surface
193
+ touch handler, which a descendant recognizer cannot prevent; Android notifies
194
+ the native gesture start, and its end once the touch stops however it stops,
195
+ since React's pointer dispatcher drops input until it does). Vertical gestures fail before taking ownership. Active touches interrupted by
185
196
  a second finger, system cancellation, disabling, detach or a newer prop update
186
197
  cannot later emit an event. Cancellation returns to the previous stable state,
187
198
  or resumes an external target whose programmatic animation the gesture interrupted.
@@ -207,8 +218,8 @@ close or prop change from rest then cross-fades from a snapshot to the final
207
218
  state over 200 ms instead of moving, and a released drag settles critically
208
219
  damped (damping ratio 1) without travelling past its target. A cross-fade is
209
220
  an in-flight transition like an animation: its final state shows at once under
210
- the snapshot, but the stable state, `onDrawerOpenChange` and the haptic follow
211
- only when the fade ends. A newer cross-fade replaces the previous snapshot
221
+ the snapshot, but the stable state and `onDrawerOpenChange` follow only when
222
+ the fade ends. A newer cross-fade replaces the previous snapshot
212
223
  (capturing it as currently shown). A request for the target already being
213
224
  faded to leaves the fade running and only replaces whether its completion is a
214
225
  user transition. A touch, an animated settle, a newer cross-fade, disabling or
@@ -10,6 +10,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
10
10
  stopAnimation()
11
11
  removeFadeSnapshot()
12
12
  stableOpen = false
13
+ hapticTarget = nil
13
14
  setProgress(0)
14
15
  if enabled && open { settle(to: true, velocity: 0, user: false) }
15
16
  }
@@ -84,10 +85,14 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
84
85
  private var fadeSnapshot: UIView?
85
86
  private var fadeTarget = false
86
87
  private var fadeUser = false
87
- // Hooks for native tests: they set Reduce Motion, and run without an app
88
- // host, where UIKit cannot snapshot a view.
88
+ // The state a user's release or tap has already confirmed with a haptic,
89
+ // so the transition plays it once, when it is decided rather than landed.
90
+ private var hapticTarget: Bool?
91
+ // Hooks for native tests: they set Reduce Motion, run without an app host,
92
+ // where UIKit cannot snapshot a view, and count haptics.
89
93
  var isReduceMotionEnabled: () -> Bool = { UIAccessibility.isReduceMotionEnabled }
90
94
  var makeFadeSnapshot: (UIView) -> UIView? = { $0.snapshotView(afterScreenUpdates: false) }
95
+ var playHaptic: () -> Void = { UIImpactFeedbackGenerator(style: .light).impactOccurred() }
91
96
 
92
97
  // Measured frame by frame from the reference recording: the page travels
93
98
  // 80% of the width, keeps a fixed display-like corner, washes toward the
@@ -203,6 +208,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
203
208
  interruptGesture()
204
209
  stopAnimation()
205
210
  removeFadeSnapshot()
211
+ hapticTarget = nil
206
212
  setProgress(stableOpen && enabled ? 1 : 0)
207
213
  } else if enabled && stableOpen != open {
208
214
  settle(to: open, velocity: 0, user: false)
@@ -231,21 +237,30 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
231
237
  guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
232
238
  guard !isInsideNativeTabBar(touch.view) else { return false }
233
239
  touchTarget = touch.view
234
- if progress > 0 { return start.x >= menuWidth * progress }
240
+ // While open, the page beside the menu and the menu itself both take the
241
+ // drag; controls inside the menu keep theirs.
242
+ if progress > 0 { return start.x >= menuWidth * progress || !touchPathContainsExclusiveControl() }
235
243
  return swipeEnabled && !touchPathContainsExclusiveControl()
236
244
  }
237
245
 
238
246
  override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
239
247
  guard enabled, bounds.width > 0 else { return false }
240
248
  let velocity = pan.velocity(in: self)
241
- guard abs(velocity.x) > abs(velocity.y) else { return false }
242
249
  let location = pan.location(in: self)
243
250
  let translation = pan.translation(in: self)
244
251
  let start = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
245
252
  guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
246
- if progress > 0 {
247
- return start.x >= menuWidth * progress
253
+ if progress > 0 && start.x < menuWidth * progress {
254
+ // Inside the menu a sideways swipe is never a tap on the row under it,
255
+ // so its press is cancelled either way; only a leftward one closes.
256
+ // The distance so far reads the direction more steadily than the speed
257
+ // at this instant.
258
+ guard abs(translation.x) > abs(translation.y) || abs(velocity.x) > abs(velocity.y) else { return false }
259
+ cancelReactTouches()
260
+ return translation.x < 0 || velocity.x < 0
248
261
  }
262
+ guard abs(velocity.x) > abs(velocity.y) else { return false }
263
+ if progress > 0 { return true }
249
264
  return swipeEnabled && velocity.x > 0 && !touchPathCanScrollRight()
250
265
  }
251
266
 
@@ -256,7 +271,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
256
271
  guard gestureRecognizer === pan,
257
272
  let scroll = other.view as? UIScrollView,
258
273
  other === scroll.panGestureRecognizer else { return false }
259
- return scroll.isDescendant(of: foregroundContent)
274
+ return scroll.isDescendant(of: foregroundContent) || scroll.isDescendant(of: menu)
260
275
  }
261
276
 
262
277
  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
@@ -338,6 +353,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
338
353
  @objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
339
354
  switch recognizer.state {
340
355
  case .began:
356
+ cancelReactTouches()
341
357
  // The drag takes over a settle or cross-fade in flight and drops its
342
358
  // completion. Cancelling returns to a programmatic target, else to the
343
359
  // stable state.
@@ -367,6 +383,24 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
367
383
  }
368
384
  }
369
385
 
386
+ // React Native's touch handler sits on the surface above this view, so the
387
+ // drag cannot prevent it, and a row the drag started on would still fire
388
+ // its press on release. Toggling the handler cancels its touches, as a
389
+ // native scroll taking over does.
390
+ private func cancelReactTouches() {
391
+ let handlerClasses = ["RCTSurfaceTouchHandler", "RCTTouchHandler"].compactMap(NSClassFromString)
392
+ guard !handlerClasses.isEmpty else { return }
393
+ var view = superview
394
+ while let current = view {
395
+ for recognizer in current.gestureRecognizers ?? []
396
+ where handlerClasses.contains(where: { recognizer.isKind(of: $0) }) {
397
+ recognizer.isEnabled = false
398
+ recognizer.isEnabled = true
399
+ }
400
+ view = current.superview
401
+ }
402
+ }
403
+
370
404
  private func interruptGesture() {
371
405
  guard pan != nil else { return }
372
406
  suppressCancellation = true
@@ -453,6 +487,12 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
453
487
  // keeps its finger velocity through a slightly underdamped spring.
454
488
  private func settle(to requested: Bool, velocity: CGFloat, user: Bool, tween: Bool = true) {
455
489
  let target = enabled && requested
490
+ // The haptic marks the decision: it plays as a user's release or tap
491
+ // commits to the other state, not when the drawer lands there.
492
+ if user && target != stableOpen && hapticTarget != target {
493
+ hapticTarget = target
494
+ if hapticFeedbackEnabled { playHaptic() }
495
+ }
456
496
  // An animation already heading there keeps running; the newer request
457
497
  // still decides whether its completion is a user event.
458
498
  if displayLink != nil && animationTarget == target {
@@ -468,6 +508,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
468
508
  guard bounds.width > 0, window != nil else {
469
509
  removeFadeSnapshot()
470
510
  stableOpen = target
511
+ hapticTarget = nil
471
512
  setProgress(target ? 1 : 0)
472
513
  return
473
514
  }
@@ -515,7 +556,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
515
556
  fadeTarget = target
516
557
  fadeUser = user
517
558
  // The final state shows now, under the snapshot. The transition completes
518
- // (stable state, callback and haptic) only when the fade does.
559
+ // (stable state and callback) only when the fade does.
519
560
  setProgress(target ? 1 : 0)
520
561
  UIView.animate(withDuration: 0.2, delay: 0, options: [.curveEaseOut], animations: {
521
562
  snapshot.alpha = 0
@@ -593,13 +634,13 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
593
634
  private func finish(target: Bool, user: Bool) {
594
635
  let changed = stableOpen != target
595
636
  stableOpen = target
637
+ hapticTarget = nil
596
638
  setProgress(target ? 1 : 0)
597
639
  if user { reportCompletion(target: target, changed: changed) }
598
640
  }
599
641
 
600
642
  private func reportCompletion(target: Bool, changed: Bool) {
601
643
  guard changed || target != open else { return }
602
- if hapticFeedbackEnabled && changed { UIImpactFeedbackGenerator(style: .light).impactOccurred() }
603
644
  onDrawerOpenChange?(target)
604
645
  }
605
646
 
@@ -145,6 +145,63 @@ final class BottomTabsDrawerReduceMotionTests: XCTestCase {
145
145
  XCTAssertEqual(detached.progress, 1)
146
146
  XCTAssertEqual(detached.events, [])
147
147
  }
148
+
149
+ // The haptic marks a user's decision; the faded close reports only later.
150
+ func testHapticPlaysWhenAUserCloseIsDecidedNotWhenItLands() throws {
151
+ let fixture = Fixture(open: true, haptics: true)
152
+ defer { fixture.close() }
153
+
154
+ XCTAssertTrue(fixture.drawer.accessibilityPerformEscape())
155
+ try fixture.requireFade()
156
+ XCTAssertEqual(fixture.hapticCount, 1, "the close is decided, so it plays now")
157
+ XCTAssertEqual(fixture.events, [])
158
+
159
+ XCTAssertTrue(fixture.waitUntil { !fixture.isFading })
160
+ XCTAssertEqual(fixture.events, [false])
161
+ XCTAssertEqual(fixture.hapticCount, 1, "landing does not play it again")
162
+ }
163
+
164
+ func testDragReleasedTowardTheSameTargetDoesNotRepeatTheHaptic() throws {
165
+ let fixture = Fixture(open: true, haptics: true)
166
+ defer { fixture.close() }
167
+ XCTAssertTrue(fixture.drawer.accessibilityPerformEscape())
168
+ try fixture.requireFade()
169
+
170
+ fixture.pan(.began)
171
+ fixture.pan(.changed, dx: fixture.menuWidth * 0.2)
172
+ fixture.pan(.ended)
173
+ XCTAssertTrue(fixture.waitUntil { !fixture.isAnimating })
174
+
175
+ XCTAssertEqual(fixture.events, [false])
176
+ XCTAssertEqual(fixture.hapticCount, 1)
177
+ }
178
+
179
+ func testReleaseBackToTheStableStateDoesNotVibrate() throws {
180
+ let fixture = Fixture(open: true, haptics: true)
181
+ defer { fixture.close() }
182
+
183
+ fixture.pan(.began)
184
+ fixture.pan(.changed, dx: -fixture.menuWidth * 0.1)
185
+ fixture.pan(.ended)
186
+ XCTAssertTrue(fixture.waitUntil { !fixture.isAnimating })
187
+
188
+ XCTAssertEqual(fixture.progress, 1)
189
+ XCTAssertEqual(fixture.events, [])
190
+ XCTAssertEqual(fixture.hapticCount, 0)
191
+ }
192
+
193
+ func testDetachingAfterADecidedCloseLetsTheNextCloseVibrate() throws {
194
+ let fixture = Fixture(open: true, haptics: true)
195
+ defer { fixture.close() }
196
+ XCTAssertTrue(fixture.drawer.accessibilityPerformEscape())
197
+ try fixture.requireFade()
198
+ fixture.drawer.removeFromSuperview()
199
+ fixture.window.addSubview(fixture.drawer)
200
+ XCTAssertEqual(fixture.progress, 1)
201
+
202
+ XCTAssertTrue(fixture.drawer.accessibilityPerformEscape())
203
+ XCTAssertEqual(fixture.hapticCount, 2, "the dropped close does not mute the next one")
204
+ }
148
205
  }
149
206
 
150
207
  @MainActor
@@ -155,12 +212,15 @@ private final class Fixture {
155
212
  let page = UIView()
156
213
  private(set) var events: [Bool] = []
157
214
  private(set) var fadingAtEvent: [Bool] = []
215
+ private(set) var hapticCount = 0
158
216
  private let recognizer = FakePan(target: nil, action: nil)
159
217
 
160
218
  /// An enabled drawer that already rests at `open`, as the prop echo leaves it.
161
- init(open: Bool) {
219
+ init(open: Bool, haptics: Bool = false) {
162
220
  drawer.isReduceMotionEnabled = { true }
163
221
  drawer.makeFadeSnapshot = { _ in UIView() }
222
+ drawer.hapticFeedbackEnabled = haptics
223
+ drawer.playHaptic = { [unowned self] in self.hapticCount += 1 }
164
224
  drawer.insertChild(menu, atIndex: 0)
165
225
  drawer.insertChild(page, atIndex: 1)
166
226
  drawer.swipeEnabled = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-bottom-tabs",
3
- "version": "6.8.0-3",
3
+ "version": "6.8.0-4",
4
4
  "description": "Native bottom tabs (tab bar + scene container) for React Native on iOS and Android",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/module/index.js",