(config: MomentumConfig)
| 91 | * Calculate the full momentum result for a thumbnail swipe |
| 92 | */ |
| 93 | export function calculateMomentum(config: MomentumConfig): MomentumResult { |
| 94 | const { |
| 95 | velocity, |
| 96 | direction, |
| 97 | isVertical, |
| 98 | currentTranslate, |
| 99 | scrollSize, |
| 100 | wrapperSize, |
| 101 | slideDuration, |
| 102 | emptySpaceMargin = 0, |
| 103 | momentumMultiplier = 150, |
| 104 | } = config; |
| 105 | |
| 106 | // Calculate momentum distance and direction |
| 107 | const momentumDistance = calculateMomentumDistance( |
| 108 | velocity, |
| 109 | momentumMultiplier |
| 110 | ); |
| 111 | const directionSign = getMomentumDirection(direction, isVertical); |
| 112 | const momentum = momentumDistance * directionSign; |
| 113 | |
| 114 | // Calculate target translate with momentum |
| 115 | let targetTranslate = currentTranslate + momentum; |
| 116 | |
| 117 | // Calculate max scroll and clamp |
| 118 | const maxScroll = scrollSize - wrapperSize + emptySpaceMargin; |
| 119 | if (maxScroll > 0) { |
| 120 | targetTranslate = clampTranslate( |
| 121 | targetTranslate, |
| 122 | maxScroll, |
| 123 | emptySpaceMargin |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | // Calculate transition |
| 128 | const transitionDuration = calculateTransitionDuration( |
| 129 | velocity, |
| 130 | slideDuration |
| 131 | ); |
| 132 | const transitionStyle = `all ${transitionDuration}ms ${MOMENTUM_EASING}`; |
| 133 | |
| 134 | return { |
| 135 | targetTranslate, |
| 136 | transitionDuration, |
| 137 | transitionStyle, |
| 138 | }; |
| 139 | } |
no test coverage detected