Skip to content

Commit 8093e96

Browse files
committed
fix android floating point issues
1 parent fc1a76f commit 8093e96

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/components/bottomSheet/BottomSheet.tsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
getKeyboardAnimationConfigs,
5959
normalizeSnapPoint,
6060
print,
61+
floatingPointEquals,
6162
} from '../../utilities';
6263
import {
6364
DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
@@ -78,7 +79,7 @@ import {
7879
DEFAULT_DYNAMIC_SIZING,
7980
DEFAULT_ACCESSIBLE,
8081
DEFAULT_ACCESSIBILITY_LABEL,
81-
DEFAULT_ACCESSIBILITY_ROLE
82+
DEFAULT_ACCESSIBILITY_ROLE,
8283
} from './constants';
8384
import type { BottomSheetMethods, Insets } from '../../types';
8485
import type { BottomSheetProps, AnimateToPositionType } from './types';
@@ -328,7 +329,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
328329
// extended position = container height - sheet height
329330
const extendedPosition =
330331
animatedContainerHeight.value - animatedSheetHeight.value;
331-
if (animatedPosition.value === extendedPosition)
332+
if (floatingPointEquals(animatedPosition.value, extendedPosition))
332333
return SHEET_STATE.EXTENDED;
333334

334335
// extended position with keyboard =
@@ -344,13 +345,16 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
344345
if (
345346
keyboardBehavior === KEYBOARD_BEHAVIOR.interactive &&
346347
isInTemporaryPosition.value &&
347-
animatedPosition.value === extendedPositionWithKeyboard
348+
floatingPointEquals(
349+
animatedPosition.value,
350+
extendedPositionWithKeyboard
351+
)
348352
) {
349353
return SHEET_STATE.EXTENDED;
350354
}
351355

352356
// fill parent = 0
353-
if (animatedPosition.value === 0) {
357+
if (floatingPointEquals(animatedPosition.value, 0)) {
354358
return SHEET_STATE.FILL_PARENT;
355359
}
356360

@@ -633,7 +637,11 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
633637
[_providedOnChange, animatedCurrentIndex]
634638
);
635639
const handleOnAnimate = useCallback(
636-
function handleOnAnimate(toPoint: number, source: ANIMATION_SOURCE, snapPoints: number[]) {
640+
function handleOnAnimate(
641+
toPoint: number,
642+
source: ANIMATION_SOURCE,
643+
snapPoints: number[]
644+
) {
637645
const closedPosition = animatedClosedPosition.value;
638646
const toIndex =
639647
toPoint === closedPosition ? -1 : snapPoints.indexOf(toPoint);
@@ -700,10 +708,10 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
700708
configs?: WithTimingConfig | WithSpringConfig
701709
) {
702710
if (
703-
position === animatedPosition.value ||
711+
floatingPointEquals(position, animatedPosition.value) ||
704712
position === undefined ||
705713
(animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
706-
position === animatedNextPosition.value)
714+
floatingPointEquals(position, animatedNextPosition.value))
707715
) {
708716
return;
709717
}

src/hooks/useGestureEventsHandlersDefault.tsx

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
} from '../types';
1919
import { clamp } from '../utilities/clamp';
2020
import { snapPoint } from '../utilities/snapPoint';
21+
import { floatingPointEquals } from '../utilities';
2122

2223
type GestureEventContextType = {
2324
initialPosition: number;
@@ -136,7 +137,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
136137
if (
137138
source === GESTURE_SOURCE.CONTENT &&
138139
isScrollableRefreshable.value &&
139-
animatedPosition.value === highestSnapPoint
140+
floatingPointEquals(animatedPosition.value, highestSnapPoint)
140141
) {
141142
return;
142143
}
@@ -148,7 +149,10 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
148149
* a negative scrollable content offset when the scrollable is not locked.
149150
*/
150151
const negativeScrollableContentOffset =
151-
(context.value.initialPosition === highestSnapPoint &&
152+
(floatingPointEquals(
153+
context.value.initialPosition,
154+
highestSnapPoint
155+
) &&
152156
source === GESTURE_SOURCE.CONTENT) ||
153157
!context.value.isScrollablePositionLocked
154158
? animatedScrollableContentOffsetY.value * -1
@@ -184,7 +188,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
184188
if (
185189
context.value.isScrollablePositionLocked &&
186190
source === GESTURE_SOURCE.CONTENT &&
187-
animatedPosition.value === highestSnapPoint
191+
floatingPointEquals(animatedPosition.value, highestSnapPoint)
188192
) {
189193
context.value = {
190194
...context.value,
@@ -258,8 +262,10 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
258262
const handleOnEnd: GestureEventHandlerCallbackType = useWorkletCallback(
259263
function handleOnEnd(source, { translationY, absoluteY, velocityY }) {
260264
const highestSnapPoint = animatedHighestSnapPoint.value;
261-
const isSheetAtHighestSnapPoint =
262-
animatedPosition.value === highestSnapPoint;
265+
const isSheetAtHighestSnapPoint = floatingPointEquals(
266+
animatedPosition.value,
267+
highestSnapPoint
268+
);
263269

264270
/**
265271
* if scrollable is refreshable and sheet position at the highest
@@ -354,7 +360,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
354360
* if destination point is the same as the current position,
355361
* then no need to perform animation.
356362
*/
357-
if (destinationPoint === animatedPosition.value) {
363+
if (floatingPointEquals(destinationPoint, animatedPosition.value)) {
358364
return;
359365
}
360366

src/utilities/floatingPointEquals.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const EPSILON = 0.1;
2+
3+
export function floatingPointEquals(valueA: number, valueB: number) {
4+
'worklet';
5+
return Math.abs(valueB - valueA) < EPSILON;
6+
}

src/utilities/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { animate } from './animate';
33
export { getKeyboardAnimationConfigs } from './getKeyboardAnimationConfigs';
44
export { print } from './logger';
55
export { noop, workletNoop } from './noop';
6+
export { floatingPointEquals } from './floatingPointEquals';

0 commit comments

Comments
 (0)