@onekeyfe/react-native-bottom-tabs 6.7.0-alpha.475 → 6.8.0-11

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.
Files changed (27) hide show
  1. package/README.md +139 -48
  2. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerManager.kt +15 -0
  3. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerView.kt +315 -59
  4. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsView.kt +426 -53
  5. package/android/src/main/java/com/onekey/bottomtabs/BottomTabsViewManager.kt +6 -5
  6. package/android/src/main/java/com/onekey/bottomtabs/FloatingTabBarStyle.kt +22 -6
  7. package/android/src/main/java/com/onekey/bottomtabs/events/TabDoublePressEvent.kt +7 -1
  8. package/docs/SPEC.md +228 -30
  9. package/ios/RCTBottomTabsComponentView.mm +4 -1
  10. package/ios/RCTBottomTabsContainerView.swift +263 -15
  11. package/ios/RCTBottomTabsDrawerComponentView.mm +24 -0
  12. package/ios/RCTBottomTabsDrawerContainerView.swift +358 -51
  13. package/ios/tests/BottomTabsDrawerReduceMotionTests.swift +291 -0
  14. package/lib/module/BottomTabs.js +85 -58
  15. package/lib/module/BottomTabsDrawerNativeComponent.ts +3 -0
  16. package/lib/module/BottomTabsNativeComponent.ts +6 -1
  17. package/lib/module/index.js +1 -0
  18. package/lib/typescript/src/BottomTabs.d.ts +23 -2
  19. package/lib/typescript/src/BottomTabsDrawerNativeComponent.d.ts +5 -0
  20. package/lib/typescript/src/BottomTabsNativeComponent.d.ts +5 -1
  21. package/lib/typescript/src/index.d.ts +1 -0
  22. package/package.json +1 -1
  23. package/react-native-bottom-tabs.podspec +6 -0
  24. package/src/BottomTabs.tsx +94 -44
  25. package/src/BottomTabsDrawerNativeComponent.ts +3 -0
  26. package/src/BottomTabsNativeComponent.ts +6 -1
  27. package/src/index.tsx +1 -0
@@ -8,7 +8,9 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
8
8
  guard enabled != oldValue else { return }
9
9
  interruptGesture()
10
10
  stopAnimation()
11
+ removeFadeSnapshot()
11
12
  stableOpen = false
13
+ hapticTarget = nil
12
14
  setProgress(0)
13
15
  if enabled && open { settle(to: true, velocity: 0, user: false) }
14
16
  }
@@ -23,11 +25,29 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
23
25
  @objc var swipeEnabled = false
24
26
  @objc var hapticFeedbackEnabled = false
25
27
  @objc var bottomBarHeight: Double = 0
28
+ /// "light" or "dark" forces that palette; anything else follows the trait collection.
29
+ @objc var colorScheme = "system" {
30
+ didSet {
31
+ guard colorScheme != oldValue else { return }
32
+ applyAppearance()
33
+ setProgress(appliedValue, overshoot: appliedOvershoot)
34
+ }
35
+ }
36
+ @objc var progressEventsEnabled = false {
37
+ didSet {
38
+ guard progressEventsEnabled != oldValue else { return }
39
+ // A new subscriber starts from the current position.
40
+ reportedProgress = -1
41
+ if progressEventsEnabled { reportProgress() }
42
+ }
43
+ }
26
44
  @objc var onDrawerOpenChange: ((Bool) -> Void)?
45
+ @objc var onDrawerProgress: ((Double) -> Void)?
27
46
 
28
47
  private let menu = UIView()
29
- private let menuShade = UIView()
30
48
  private let foreground = UIView()
49
+ // Clips the page to its rounded corners; the unclipped foreground casts the shadow.
50
+ private let pageClip = UIView()
31
51
  private let foregroundContent = UIView()
32
52
  private let dim = UIButton(type: .custom)
33
53
  private var slots: [UIView] = []
@@ -45,30 +65,95 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
45
65
  private var animationTarget = false
46
66
  private var animationUser = false
47
67
  private var previousSize = CGSize.zero
68
+ private var animationDuration: CFTimeInterval = 0
69
+ private var scrimMaxAlpha: CGFloat = 0.58
70
+ private var pageShadowOpacity: Float = 0
71
+ private var pageHairline = false
72
+ private var reportedProgress: CGFloat = -1
73
+ // The last requested position, kept so a layout pass mid-settle does not
74
+ // clip the spring's overshoot back to fully open.
75
+ private var appliedValue: CGFloat = 0
76
+ private var appliedOvershoot = false
77
+ // Running speed of the current animation, for handing off to a retarget.
78
+ private var trackedValue: CGFloat = 0
79
+ private var trackedTime: CFTimeInterval = 0
80
+ private var trackedVelocity: CGFloat = 0
81
+ private var shadowPathRadius: CGFloat = -1
82
+ // An in-flight Reduce Motion cross-fade: its one snapshot, the state it
83
+ // lands on and whether that is a user transition. Like a running settle, it
84
+ // updates `stableOpen` and reports only when it lands.
85
+ private var fadeSnapshot: UIView?
86
+ private var fadeTarget = false
87
+ private var fadeUser = false
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.
93
+ var isReduceMotionEnabled: () -> Bool = { UIAccessibility.isReduceMotionEnabled }
94
+ var makeFadeSnapshot: (UIView) -> UIView? = { $0.snapshotView(afterScreenUpdates: false) }
95
+ var playHaptic: () -> Void = { UIImpactFeedbackGenerator(style: .light).impactOccurred() }
96
+
97
+ // Measured frame by frame from the reference recording: the page travels
98
+ // 80% of the width, keeps a fixed display-like corner, washes toward the
99
+ // background linearly, and casts a soft shadow onto the menu below it (a
100
+ // hairline in dark mode).
101
+ private static let menuWidthFraction: CGFloat = 0.8
102
+ private static let pageCornerRadius: CGFloat = 62
103
+ private static let menuMinScale: CGFloat = 0.97
104
+ // Released drags settle on a slightly underdamped spring: about 2pt of
105
+ // overshoot after a fling, none after a slow release.
106
+ private static let springFrequency: CGFloat = 25.5
107
+ private static let springDamping: CGFloat = 0.84
108
+ private var menuWidth: CGFloat { bounds.width * Self.menuWidthFraction }
48
109
 
49
110
  override init(frame: CGRect) {
50
111
  super.init(frame: frame)
51
112
  clipsToBounds = true
52
113
  addSubview(menu)
53
- menuShade.backgroundColor = .black
54
- menuShade.isUserInteractionEnabled = false
55
- menu.addSubview(menuShade)
56
114
  addSubview(foreground)
57
- foreground.addSubview(foregroundContent)
58
- dim.backgroundColor = UIColor(white: 28.0 / 255.0, alpha: 1)
115
+ foreground.addSubview(pageClip)
116
+ pageClip.addSubview(foregroundContent)
59
117
  dim.accessibilityLabel = NSLocalizedString("Close menu", comment: "Close the tab drawer")
60
118
  dim.addTarget(self, action: #selector(closeFromUser), for: .touchUpInside)
61
- foreground.addSubview(dim)
62
- foreground.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
63
- foreground.layer.cornerCurve = .circular
64
- foreground.clipsToBounds = true
119
+ pageClip.addSubview(dim)
120
+ pageClip.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
121
+ pageClip.layer.cornerCurve = .circular
122
+ pageClip.clipsToBounds = true
123
+ foreground.layer.shadowColor = UIColor.black.cgColor
124
+ foreground.layer.shadowOffset = .zero
125
+ foreground.layer.shadowRadius = 18
126
+ foreground.layer.shadowOpacity = 0
65
127
  pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
66
128
  pan.maximumNumberOfTouches = 1
67
129
  pan.delegate = self
68
130
  addGestureRecognizer(pan)
131
+ applyAppearance()
69
132
  setProgress(0)
70
133
  }
71
134
 
135
+ private func applyAppearance() {
136
+ let dark: Bool
137
+ switch colorScheme {
138
+ case "dark": dark = true
139
+ case "light": dark = false
140
+ default: dark = traitCollection.userInterfaceStyle == .dark
141
+ }
142
+ // The page washes toward white in light mode and toward gray in dark mode.
143
+ dim.backgroundColor = dark ? UIColor(white: 36.0 / 255.0, alpha: 1) : .white
144
+ scrimMaxAlpha = dark ? 0.58 : 0.5
145
+ pageShadowOpacity = dark ? 0 : 0.16
146
+ pageHairline = dark
147
+ pageClip.layer.borderColor = UIColor(white: 1, alpha: 0.17).cgColor
148
+ }
149
+
150
+ override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
151
+ super.traitCollectionDidChange(previousTraitCollection)
152
+ guard traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else { return }
153
+ applyAppearance()
154
+ setProgress(appliedValue, overshoot: appliedOvershoot)
155
+ }
156
+
72
157
  required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
73
158
 
74
159
  @objc func insertChild(_ child: UIView, atIndex index: Int) {
@@ -87,13 +172,14 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
87
172
  for (index, child) in slots.enumerated() {
88
173
  (index == 0 ? menu : foregroundContent).insertSubview(child, at: 0)
89
174
  }
90
- menu.bringSubviewToFront(menuShade)
91
175
  setNeedsLayout()
92
176
  }
93
177
 
94
178
  override func layoutSubviews() {
95
179
  super.layoutSubviews()
96
180
  if previousSize != bounds.size {
181
+ // A cross-fade's image is of the old size, so it lands at once.
182
+ completeFade()
97
183
  let dragging = pan.state == .began || pan.state == .changed
98
184
  let target = displayLink != nil ? animationTarget : (dragging ? gestureReturnOpen : stableOpen)
99
185
  let user = displayLink != nil && animationUser && previousSize != .zero
@@ -104,14 +190,16 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
104
190
  }
105
191
  menu.transform = .identity
106
192
  foreground.transform = .identity
107
- menu.frame = CGRect(x: 0, y: 0, width: bounds.width * 0.78, height: bounds.height)
193
+ menu.frame = CGRect(x: 0, y: 0, width: menuWidth, height: bounds.height)
108
194
  foreground.frame = bounds
109
- foregroundContent.frame = foreground.bounds
110
- dim.frame = foreground.bounds
111
- menuShade.frame = menu.bounds
195
+ pageClip.frame = foreground.bounds
196
+ foregroundContent.frame = pageClip.bounds
197
+ dim.frame = pageClip.bounds
198
+ // Rebuilt for the new bounds by setProgress below.
199
+ shadowPathRadius = -1
112
200
  if slots.count > 0 { slots[0].frame = menu.bounds }
113
201
  if slots.count > 1 { slots[1].frame = foreground.bounds }
114
- setProgress(progress)
202
+ setProgress(appliedValue, overshoot: appliedOvershoot)
115
203
  }
116
204
 
117
205
  override func didMoveToWindow() {
@@ -119,6 +207,8 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
119
207
  if window == nil {
120
208
  interruptGesture()
121
209
  stopAnimation()
210
+ removeFadeSnapshot()
211
+ hapticTarget = nil
122
212
  setProgress(stableOpen && enabled ? 1 : 0)
123
213
  } else if enabled && stableOpen != open {
124
214
  settle(to: open, velocity: 0, user: false)
@@ -131,27 +221,46 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
131
221
  return true
132
222
  }
133
223
 
224
+ // While a cross-fade runs, the views under the snapshot already show the
225
+ // final state, so a touch would reach a control that is not where it
226
+ // appears. The host takes those touches itself (the snapshot cannot: its
227
+ // alpha is already 0 for hit-testing once the fade starts). The pan is on
228
+ // the host, so a drag still takes over the fade.
229
+ override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
230
+ let hit = super.hitTest(point, with: event)
231
+ return hit != nil && fadeSnapshot != nil ? self : hit
232
+ }
233
+
134
234
  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
135
235
  guard enabled else { return false }
136
236
  let start = touch.location(in: self)
137
237
  guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
138
238
  guard !isInsideNativeTabBar(touch.view) else { return false }
139
239
  touchTarget = touch.view
140
- if progress > 0 { return start.x >= bounds.width * 0.78 * 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() }
141
243
  return swipeEnabled && !touchPathContainsExclusiveControl()
142
244
  }
143
245
 
144
246
  override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
145
247
  guard enabled, bounds.width > 0 else { return false }
146
248
  let velocity = pan.velocity(in: self)
147
- guard abs(velocity.x) > abs(velocity.y) else { return false }
148
249
  let location = pan.location(in: self)
149
250
  let translation = pan.translation(in: self)
150
251
  let start = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
151
252
  guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
152
- if progress > 0 {
153
- return start.x >= bounds.width * 0.78 * 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
154
261
  }
262
+ guard abs(velocity.x) > abs(velocity.y) else { return false }
263
+ if progress > 0 { return true }
155
264
  return swipeEnabled && velocity.x > 0 && !touchPathCanScrollRight()
156
265
  }
157
266
 
@@ -162,7 +271,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
162
271
  guard gestureRecognizer === pan,
163
272
  let scroll = other.view as? UIScrollView,
164
273
  other === scroll.panGestureRecognizer else { return false }
165
- return scroll.isDescendant(of: foregroundContent)
274
+ return scroll.isDescendant(of: foregroundContent) || scroll.isDescendant(of: menu)
166
275
  }
167
276
 
168
277
  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
@@ -244,8 +353,19 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
244
353
  @objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
245
354
  switch recognizer.state {
246
355
  case .began:
247
- gestureReturnOpen = displayLink != nil && !animationUser ? animationTarget : stableOpen
356
+ cancelReactTouches()
357
+ // The drag takes over a settle or cross-fade in flight and drops its
358
+ // completion. Cancelling returns to a programmatic target, else to the
359
+ // stable state.
360
+ if displayLink != nil && !animationUser {
361
+ gestureReturnOpen = animationTarget
362
+ } else if fadeSnapshot != nil && !fadeUser {
363
+ gestureReturnOpen = fadeTarget
364
+ } else {
365
+ gestureReturnOpen = stableOpen
366
+ }
248
367
  stopAnimation()
368
+ removeFadeSnapshot()
249
369
  gestureStartProgress = progress
250
370
  case .changed:
251
371
  guard recognizer.numberOfTouches <= 1 else {
@@ -253,16 +373,34 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
253
373
  settle(to: gestureReturnOpen, velocity: 0, user: false)
254
374
  return
255
375
  }
256
- setProgress(gestureStartProgress + recognizer.translation(in: self).x / max(1, bounds.width * 0.78))
376
+ setProgress(gestureStartProgress + recognizer.translation(in: self).x / max(1, menuWidth))
257
377
  case .ended:
258
- let velocity = min(8, max(-8, recognizer.velocity(in: self).x / max(1, bounds.width * 0.78)))
259
- settle(to: progress + velocity * 0.12 >= 0.5, velocity: velocity, user: true)
378
+ let velocity = min(12, max(-12, recognizer.velocity(in: self).x / max(1, menuWidth)))
379
+ settle(to: progress + velocity * 0.12 >= 0.5, velocity: velocity, user: true, tween: false)
260
380
  case .cancelled, .failed:
261
381
  if !suppressCancellation { settle(to: gestureReturnOpen, velocity: 0, user: false) }
262
382
  default: break
263
383
  }
264
384
  }
265
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
+
266
404
  private func interruptGesture() {
267
405
  guard pan != nil else { return }
268
406
  suppressCancellation = true
@@ -277,49 +415,215 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
277
415
  settle(to: false, velocity: 0, user: true)
278
416
  }
279
417
 
280
- private func setProgress(_ value: CGFloat) {
281
- progress = min(1, max(0, enabled ? value : 0))
282
- foreground.transform = CGAffineTransform(translationX: bounds.width * 0.78 * progress, y: 0)
283
- foreground.layer.cornerRadius = 64 * progress
284
- dim.alpha = 0.7 * progress
285
- dim.isHidden = progress == 0
286
- foregroundContent.isUserInteractionEnabled = progress == 0
287
- foregroundContent.accessibilityElementsHidden = progress > 0
288
- menu.transform = CGAffineTransform(translationX: -0.12 * bounds.width * 0.78 * (1 - progress), y: 0)
289
- menuShade.alpha = 0.12 * (1 - progress)
290
- menu.isHidden = progress == 0
291
- menu.accessibilityElementsHidden = progress == 0
418
+ private func setProgress(_ value: CGFloat, overshoot: Bool = false) {
419
+ appliedValue = value
420
+ appliedOvershoot = overshoot
421
+ let raw = enabled ? value : 0
422
+ progress = min(1, max(0, raw))
423
+ let visible = progress > 0
424
+ // Only the settle spring may carry the page a few points past open.
425
+ let travel = overshoot ? min(1.03, max(0, raw)) : progress
426
+ foreground.transform = CGAffineTransform(translationX: menuWidth * travel, y: 0)
427
+ // Rounds in over the first 10% so screens with other corner radii never pop.
428
+ let cornerRadius = visible ? Self.pageCornerRadius * min(1, progress / 0.1) : 0
429
+ pageClip.layer.cornerRadius = cornerRadius
430
+ // The shadow follows the same corner while it rounds in.
431
+ if cornerRadius != shadowPathRadius {
432
+ shadowPathRadius = cornerRadius
433
+ foreground.layer.shadowPath = UIBezierPath(
434
+ roundedRect: foreground.bounds,
435
+ byRoundingCorners: [.topLeft, .bottomLeft],
436
+ cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
437
+ ).cgPath
438
+ }
439
+ pageClip.layer.borderWidth = visible && pageHairline ? 1 / max(1, traitCollection.displayScale) : 0
440
+ foreground.layer.shadowOpacity = visible ? pageShadowOpacity : 0
441
+ dim.alpha = scrimMaxAlpha * progress
442
+ dim.isHidden = !visible
443
+ foregroundContent.isUserInteractionEnabled = !visible
444
+ foregroundContent.accessibilityElementsHidden = visible
445
+ // The menu grows from its left-center edge and fades in over whatever is
446
+ // behind the host, fully visible by 60% open.
447
+ let minScale = isReduceMotionEnabled() ? 1 : Self.menuMinScale
448
+ let scale = minScale + (1 - minScale) * progress
449
+ menu.transform = CGAffineTransform(translationX: -(1 - scale) * menu.bounds.width / 2, y: 0)
450
+ .scaledBy(x: scale, y: scale)
451
+ menu.alpha = min(1, progress / 0.6)
452
+ menu.isHidden = !visible
453
+ menu.accessibilityElementsHidden = !visible
454
+ reportProgress()
455
+ }
456
+
457
+ /// Sends the current position again, for an event emitter attached after it was reported.
458
+ @objc func resendProgress() {
459
+ reportedProgress = -1
460
+ reportProgress()
292
461
  }
293
462
 
294
- private func settle(to target: Bool, velocity: CGFloat, user: Bool) {
463
+ private func reportProgress() {
464
+ guard progressEventsEnabled else { return }
465
+ if progress != reportedProgress && (abs(progress - reportedProgress) >= 0.002 || progress == 0 || progress == 1) {
466
+ reportedProgress = progress
467
+ onDrawerProgress?(Double(progress))
468
+ }
469
+ }
470
+
471
+ // cubic-bezier(0.55, 0.6, 0.2, 1), fitted to the reference tap open and close.
472
+ private static func pageCurve(_ x: CGFloat) -> CGFloat {
473
+ func bezier(_ t: CGFloat, _ p1: CGFloat, _ p2: CGFloat) -> CGFloat {
474
+ let u = 1 - t
475
+ return 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t
476
+ }
477
+ var low: CGFloat = 0
478
+ var high: CGFloat = 1
479
+ for _ in 0..<24 {
480
+ let mid = (low + high) / 2
481
+ if bezier(mid, 0.55, 0.2) < x { low = mid } else { high = mid }
482
+ }
483
+ return bezier((low + high) / 2, 0.6, 1)
484
+ }
485
+
486
+ // Taps and programmatic changes ease over a fixed time; a released drag
487
+ // keeps its finger velocity through a slightly underdamped spring.
488
+ private func settle(to requested: Bool, velocity: CGFloat, user: Bool, tween: Bool = true) {
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
+ }
496
+ // An animation already heading there keeps running; the newer request
497
+ // still decides whether its completion is a user event.
498
+ if displayLink != nil && animationTarget == target {
499
+ animationUser = user
500
+ return
501
+ }
502
+ // Reversing mid-flight continues from the page's on-screen position and
503
+ // speed, so the motion bends instead of stopping dead.
504
+ let retarget = displayLink != nil
505
+ let from = retarget ? appliedValue : progress
506
+ let carried = retarget ? trackedVelocity : velocity
295
507
  stopAnimation()
296
- let target = enabled && target
297
508
  guard bounds.width > 0, window != nil else {
509
+ removeFadeSnapshot()
298
510
  stableOpen = target
511
+ hapticTarget = nil
299
512
  setProgress(target ? 1 : 0)
300
513
  return
301
514
  }
302
- if progress == (target ? 1 : 0) {
515
+ if !retarget && progress == (target ? 1 : 0) {
516
+ // A cross-fade landing there keeps fading; as for a running animation,
517
+ // the newer request decides whether its completion is a user event.
518
+ if fadeSnapshot != nil {
519
+ fadeUser = user
520
+ return
521
+ }
303
522
  finish(target: target, user: user)
304
523
  return
305
524
  }
306
- animationFrom = progress
307
- animationVelocity = min(8, max(-8, velocity))
525
+ if tween && !retarget && isReduceMotionEnabled() && crossFade(to: target, user: user) {
526
+ return
527
+ }
528
+ removeFadeSnapshot()
529
+ animationFrom = from
530
+ animationVelocity = min(12, max(-12, carried))
308
531
  animationTarget = target
309
532
  animationUser = user
533
+ // Closing runs about 20% shorter than opening.
534
+ let fullDuration: CGFloat = target ? 0.36 : 0.29
535
+ animationDuration = tween && !retarget ? max(0.12, fullDuration * abs((target ? 1 : 0) - from)) : 0
310
536
  animationStart = CACurrentMediaTime()
537
+ trackedValue = from
538
+ trackedTime = animationStart
539
+ trackedVelocity = animationVelocity
311
540
  let link = CADisplayLink(target: self, selector: #selector(tick))
312
541
  link.add(to: .main, forMode: .common)
313
542
  displayLink = link
314
543
  }
315
544
 
545
+ // Reduce Motion: taps and programmatic changes cross-fade into place
546
+ // instead of sliding the whole page.
547
+ private func crossFade(to target: Bool, user: Bool) -> Bool {
548
+ // Captures what is on screen, including any fade still in progress, which
549
+ // this one then replaces: the host keeps at most one snapshot.
550
+ guard let snapshot = makeFadeSnapshot(self) else { return false }
551
+ removeFadeSnapshot()
552
+ snapshot.frame = bounds
553
+ snapshot.isUserInteractionEnabled = false
554
+ addSubview(snapshot)
555
+ fadeSnapshot = snapshot
556
+ fadeTarget = target
557
+ fadeUser = user
558
+ // The final state shows now, under the snapshot. The transition completes
559
+ // (stable state and callback) only when the fade does.
560
+ setProgress(target ? 1 : 0)
561
+ UIView.animate(withDuration: 0.2, delay: 0, options: [.curveEaseOut], animations: {
562
+ snapshot.alpha = 0
563
+ }, completion: { [weak self] _ in
564
+ snapshot.removeFromSuperview()
565
+ guard let self, self.fadeSnapshot === snapshot else { return }
566
+ self.completeFade()
567
+ })
568
+ return true
569
+ }
570
+
571
+ /// Lands an in-flight cross-fade on its target, as a settle's last frame does.
572
+ private func completeFade() {
573
+ guard fadeSnapshot != nil else { return }
574
+ let target = fadeTarget
575
+ let user = fadeUser
576
+ removeFadeSnapshot()
577
+ finish(target: target, user: user)
578
+ }
579
+
580
+ /// Drops the cross-fade without landing it (a newer transition, a drag,
581
+ /// disabling or detach), so its completion is never reported.
582
+ private func removeFadeSnapshot() {
583
+ guard let snapshot = fadeSnapshot else { return }
584
+ // Cleared first, so the completion handler that removing the animation
585
+ // triggers does not land it.
586
+ fadeSnapshot = nil
587
+ snapshot.layer.removeAllAnimations()
588
+ snapshot.removeFromSuperview()
589
+ }
590
+
316
591
  @objc private func tick() {
317
- let elapsed = CGFloat(CACurrentMediaTime() - animationStart)
592
+ let now = CACurrentMediaTime()
593
+ let elapsed = CGFloat(now - animationStart)
318
594
  let target: CGFloat = animationTarget ? 1 : 0
319
- let a = animationFrom - target
320
- let b = animationVelocity + 22 * a
321
- setProgress(target + (a + b * elapsed) * exp(-22 * elapsed))
322
- if elapsed >= 0.5 {
595
+ let value: CGFloat
596
+ let isSpring = animationDuration == 0
597
+ var tweenDone = false
598
+ if !isSpring {
599
+ let x = min(1, elapsed / CGFloat(animationDuration))
600
+ value = animationFrom + (target - animationFrom) * Self.pageCurve(x)
601
+ tweenDone = x >= 1
602
+ } else {
603
+ let w = Self.springFrequency
604
+ let a = animationFrom - target
605
+ if isReduceMotionEnabled() {
606
+ // Critically damped: same speed, no bounce.
607
+ value = target + (a + (animationVelocity + w * a) * elapsed) * exp(-w * elapsed)
608
+ } else {
609
+ let z = Self.springDamping
610
+ let wd = w * sqrt(1 - z * z)
611
+ let decay = exp(-z * w * elapsed)
612
+ value = target + decay * (a * cos(wd * elapsed) + (animationVelocity + z * w * a) / wd * sin(wd * elapsed))
613
+ }
614
+ }
615
+ if now > trackedTime {
616
+ trackedVelocity = (value - trackedValue) / CGFloat(now - trackedTime)
617
+ }
618
+ trackedValue = value
619
+ trackedTime = now
620
+ // Reduce Motion never carries the page past its target.
621
+ setProgress(value, overshoot: isSpring && !isReduceMotionEnabled())
622
+ // A spring ends once it is visually at rest, not after a fixed time.
623
+ let done = isSpring
624
+ ? (abs(value - target) < 0.0005 && abs(trackedVelocity) < 0.02) || elapsed >= 0.6
625
+ : tweenDone
626
+ if done {
323
627
  let target = animationTarget
324
628
  let user = animationUser
325
629
  stopAnimation()
@@ -330,11 +634,14 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
330
634
  private func finish(target: Bool, user: Bool) {
331
635
  let changed = stableOpen != target
332
636
  stableOpen = target
637
+ hapticTarget = nil
333
638
  setProgress(target ? 1 : 0)
334
- if user && (changed || target != open) {
335
- if hapticFeedbackEnabled && changed { UIImpactFeedbackGenerator(style: .light).impactOccurred() }
336
- onDrawerOpenChange?(target)
337
- }
639
+ if user { reportCompletion(target: target, changed: changed) }
640
+ }
641
+
642
+ private func reportCompletion(target: Bool, changed: Bool) {
643
+ guard changed || target != open else { return }
644
+ onDrawerOpenChange?(target)
338
645
  }
339
646
 
340
647
  private func stopAnimation() {